|
From: Schedler, K. <Kur...@hi...> - 2001-05-01 21:45:28
|
bool String::deleteChars (int beg, int end)
{
// do range checking on arguments
...
// assuming a null terminated string...
// overwrite the characters from beg onward
// with the characters from end onward
while ( sequence[end] != '\0' )
{
sequence[beg] = sequence[end];
beg++;
end++;
}
sequence[beg] = '\0';
current_length = beg;
return true;
}
-----Original Message-----
From: Linda Hohenstein [mailto:lz...@sp...]
Sent: Tuesday, May 01, 2001 1:28 PM
To: Tutoraid; C++ List
Subject: [Dev-C++] String Functions
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 <file://#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
"The sender believes that this E-mail and any attachments were free of any
virus, worm, Trojan horse, and/or malicious code when sent. This message
and its attachments could have been infected during transmission. By
reading the message and opening any attachments, the recipient accepts full
responsibility for taking protective and remedial action about viruses and
other defects. The sender's employer is not liable for any loss or damage
arising in any way from this message or its attachments."
|