From: Linda H. <lz...@sp...> - 2001-05-01 23:01:50
|
Thanks Michael and Kurt! Michael, is this: "You also don't handle the case where the character does not exist." left to the user to take care of? TIA, Linda ----------------------------------------- ----- Original Message ----- From: "Michael Potter" <mdp...@ya...> To: <dev...@li...> Sent: Tuesday, May 01, 2001 5:02 PM Subject: Re: [Dev-C++] String Functions > Sorry, none of this was tested but, it may give you a > hand. > > 1) You do not terminate the loop once you find the > first instance. You also don't handle the case where > the character does not exist. TRY: > > int String::firstcharfind (const char& charfind) const > //#2 > { > int i; > for (i = 0; i < current_length; i++) > { > if (sequence[i] == charfind) > return i; > } > return -1; > } > > 2) I am assuming your string arrays are NULL > terminiated. > TRY: > > bool String::charsdelete (int begin, int end) //#4 > { > if ((begin < 0) || > (begin >= current_length) || > (end >= current_length) || > (begin > end) > return false; > > int lengthToMove = (current_length - end); > > > memcpy(&sequence[begin],&sequence[end+1],lengthToMove); > > current_length = current_length - ((end - begin) + > 1); > > return true; > } > > > > > > > --- Linda Hohenstein <lz...@sp...> wrote: > > Hi All, > > > > Can anyone help me with a couple of functions I am > > having problems with in the String class I am > > writing? (It runs.) > > > > The following code is supposed to return the first > > instance of a character, but is returning the last > > (11): > > > > int String::firstcharfind (const char& charfind) > > const //#2 > > { size_t i; > > int position; > > for (i = 0; i < current_length; i++) > > { > > if (sequence[i] == charfind) > > position = i; > > } > > return position; > > } > > > > This is called by: > > > > //#2 > > char testing [28] = "Delete the extra word > > word."; > > String demostring (testing); > > cout << demostring.firstcharfind ('e') << endl; > > > > The second function is supposed to delete one of the > > "word"s in the above function: > > > > bool String::charsdelete (int begin, int end) //#4 > > { > > int i; > > if ((begin < 0) || (end > current_length) || > > (begin > end)) > > return false; > > int partdeleted = (end - begin) + 1; > > for (i = begin; i <= current_length - partdeleted > > ; i++) > > { > > sequence[i] = sequence[partdeleted + 1]; > > } > > current_length = current_length - partdeleted; > > allocated = allocated - partdeleted; > > return true; > > } > > > > It's called by: > > > > //#4 > > demostring.charsdelete (18, 22); > > cout << demostring << endl; > > > > The output is: > > > > Delete the extra w ord. > > > > I'd appreciate any help anyone can give - it's the > > last class for my Data Structures class. > > > > TIA, > > > > Linda > > > > > > > > > > > > > __________________________________________________ > Do You Yahoo!? > Yahoo! Auctions - buy the things you want at great prices > http://auctions.yahoo.com/ > > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > http://lists.sourceforge.net/lists/listinfo/dev-cpp-users |