|
From: Linda H. <lz...@sp...> - 2001-05-01 20:28:39
|
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 =3D 0; i < current_length; i++)
{
if (sequence[i] =3D=3D charfind)
position =3D i;
}
return position;
}
This is called by:=20
//#2
char testing [28] =3D "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 =3D (end - begin) + 1;
for (i =3D begin; i <=3D current_length - partdeleted ; i++)
{
sequence[i] =3D sequence[partdeleted + 1];
}
current_length =3D current_length - partdeleted;
allocated =3D 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
|
|
From: Michael P. <mdp...@ya...> - 2001-05-01 21:02:38
|
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/
|
|
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
|
|
From: Michael P. <mdp...@ya...> - 2001-05-02 13:53:56
|
The caller should have some way to detect the case. My
function returned a '-1' in this case. The caller
should test for this result.
--- Linda Hohenstein <lz...@sp...> wrote:
> 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
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
>
http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/
|
|
From: Luigi S. <gi...@li...> - 2001-05-05 22:43:32
|
Try this version of "charsdelete".
I assume: i) sequence is null terminated, ii) current_length is the
number of chars in sequence (i.e. if sequence is "delete the extra
word word." current_length is 27).
bool String::charsdelete (int begin, int end)
{
if (begin < 0 || begin >= current_length || end >= current_length
|| begin > end)
return false;
int partdeleted = end - begin + 1;
while (end < current_length) // moves also the trailing '\0'
sequence[begin++] = sequence[++end];
current_length -= partdeleted;
allocated -= partdeleted; // I wonder why you keep track of
allocated memory
return true;
}
gisan
----- Original Message -----
From: "Michael Potter" <mdp...@ya...>
To: <dev...@li...>
Sent: Wednesday, May 02, 2001 3:53 PM
Subject: Re: [Dev-C++] String Functions
The caller should have some way to detect the case. My
function returned a '-1' in this case. The caller
should test for this result.
--- Linda Hohenstein <lz...@sp...> wrote:
> 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
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
>
http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
__________________________________________________
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
|