473,795 Members | 2,826 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with string

I did this function:

void clean(string&s)
{
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
s[i]=tolower(s[i]);
else
s.erase(i,1);
}
return;
}

I'm having a strange behavior with string like these:

"Disneyland " -> Disneyland
".disneylan d." -> .disneyland"

It seems as if there is something going on with the erase function that
i don't know, because with this:

void clean(string&s)
{
string s2;
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
{
s[i]=tolower(s[i]);
s2+=s[i];
}
}
s=s2;
return;
}

Everything goes OK.

What's wrong with the first code?

Thanks.

Jun 3 '06 #1
11 1777
Gaijinco wrote:
I did this function:

void clean(string&s)
{
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
s[i]=tolower(s[i]);
else
s.erase(i,1);
}
return;
}
A call to erase() does not just rub off the content and left a hole in
the string. It moves the subsequent content forward to replace the part
to be removed.

As a result of moving the content, your index i no longer indexes into
the character that you want.

I'm having a strange behavior with string like these:

"Disneyland " -> Disneyland
".disneylan d." -> .disneyland"

It seems as if there is something going on with the erase function that
i don't know, because with this:
....

void clean(string&s)
{
string s2;
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
{
s[i]=tolower(s[i]);
s2+=s[i];
}
}
s=s2;
return;
}

Everything goes OK.
Yep, this is the right way to do it. It is a cleaner, faster and, above
all, correct implementation.

What's wrong with the first code?
Index are much like iterators, they are "invalidate d" when you erase
something from a container.

Thanks.


Ben
Jun 3 '06 #2
Gaijinco wrote:
I did this function:

void clean(string&s)
{
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
s[i]=tolower(s[i]);
else
s.erase(i,1);
}
return;
}

I'm having a strange behavior with string like these:

"Disneyland " -> Disneyland
".disneylan d." -> .disneyland"

It seems as if there is something going on with the erase function that
i don't know, because with this:

void clean(string&s)
{
string s2;
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
{
s[i]=tolower(s[i]);
s2+=s[i];
}
}
s=s2;
return;
}

Everything goes OK.

What's wrong with the first code?

Thanks.


Well yes, erase() removes chars, so the size has changed.
Here's a quick and dirty example:

#include <iostream>
#include <string>
#include <cctype>

void clean( std::string& s )
{
std::string::it erator it;

// step thru the string using the iterator 'it'
for( it = s.begin(); it != s.end(); )
{
// if the current char is an alpha
if( isalpha(*it) )
{
// change the char to lower case
*it = tolower(*it);

// increment 'it' to the next char
++it;
}
else
{
// delete the current non-alpha char from 's'.
// resets 'it' to point to the first char after
// the one just deleted - or to s.end() if no
// more chars in 's'.
it = s.erase(it);
}
}

return;
}

int main()
{
std::string t1 = "Disneyland ";
std::string t2 = ".disneylan d.";

clean(t1);
clean(t2);

std::cout << t1 << std::endl;
std::cout << t2 << std::endl;

return 0;
}

This program outputs:

disneyland
disneyland

Regards,
Larry
Jun 3 '06 #3
dj
Larry I Smith wrote:
Gaijinco wrote:
I did this function:

void clean(string&s)
{
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
s[i]=tolower(s[i]);
else
s.erase(i,1);
}
return;
}

I'm having a strange behavior with string like these:

"Disneyland " -> Disneyland
".disneylan d." -> .disneyland"

It seems as if there is something going on with the erase function that
i don't know, because with this:

void clean(string&s)
{
string s2;
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
{
s[i]=tolower(s[i]);
s2+=s[i];
}
}
s=s2;
return;
}

Everything goes OK.

What's wrong with the first code?

Thanks.


Well yes, erase() removes chars, so the size has changed.
Here's a quick and dirty example:

#include <iostream>
#include <string>
#include <cctype>

void clean( std::string& s )
{
std::string::it erator it;

// step thru the string using the iterator 'it'
for( it = s.begin(); it != s.end(); )
{
// if the current char is an alpha
if( isalpha(*it) )
{
// change the char to lower case
*it = tolower(*it);

// increment 'it' to the next char
++it;
}
else
{
// delete the current non-alpha char from 's'.
// resets 'it' to point to the first char after
// the one just deleted - or to s.end() if no
// more chars in 's'.
it = s.erase(it);
}
}

return;
}

int main()
{
std::string t1 = "Disneyland ";
std::string t2 = ".disneylan d.";

clean(t1);
clean(t2);

std::cout << t1 << std::endl;
std::cout << t2 << std::endl;

return 0;
}

This program outputs:

disneyland
disneyland

Regards,
Larry


In cases like this, i prefer to traverse the container in reverse order.
In that case the part of the container we have already examined is
shifted and presents no problem.

Wouldn't you agree?
Jun 3 '06 #4
Thank you for all your opinions! This was quite insightful!

Jun 3 '06 #5
In article <11************ **********@u72g 2000cwu.googleg roups.com>,
"Gaijinco" <ga******@gmail .com> wrote:
I did this function:

void clean(string&s)
{
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
s[i]=tolower(s[i]);
else
s.erase(i,1);
}
return;
}

I'm having a strange behavior with string like these:

"Disneyland " -> Disneyland
".disneylan d." -> .disneyland"

It seems as if there is something going on with the erase function that
i don't know


Think about what happens when you erase an element of the container. All
the elements that come after are shifted one toward the front of the
container. So, for example, when the first quote is erased, the period
is moved to element 0, then 'i' is incremented to be equal to 1, so the
period is never checked.

There are a couple of ways to fix this bu why don't you take a stab at
it first.

--
Bene disserere est finis logices. -- Aristotle
Jun 3 '06 #6
In article <dv************ ********@news.s iol.net>,
dj <sm*******@lyco s.com> wrote:
In cases like this, i prefer to traverse the container in reverse order.
In that case the part of the container we have already examined is
shifted and presents no problem.

Wouldn't you agree?


No, still too many assignments and redundant checks when an element is
removed. Better would be:

void clean( string& s )
{
string::size_ty pe insert = 0;
for ( string::size_ty pe index = 0; index != s.size(); ++index )
if ( isalpha( s[index] ) )
s[insert++] = tolower( s[index] );
s.erase( insert, string::npos );
}

That way, each character is only moved once. (strictly speaking that
last line could be "s.erase( insert );" but that doesn't make it clear
that all elements from insert to the end of the string are being erased.)

--
Bene disserere est finis logices. -- Aristotle
Jun 3 '06 #7
In article <44************ ***********@new s.optusnet.com. au>,
benben <be******@yahoo .com.au> wrote:
void clean(string&s)
{
string s2;
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
{
s[i]=tolower(s[i]);
s2+=s[i];
}
}
s=s2;
return;
}

Everything goes OK.


Yep, this is the right way to do it. It is a cleaner, faster and, above
all, correct implementation.


It isn't faster for two reasons. (1) s2 needs to call new[], possibly
multiple times and each time it must copy the contents of the old array
into the new array (2) after s2 is filled in, it is assigned back into
's' (yet another loop.)

See my previous post for a more appropriate implementation (which is
based on the remove_copy_if algorithm.)

--
Bene disserere est finis logices. -- Aristotle
Jun 3 '06 #8
dj wrote:
Larry I Smith wrote:
Gaijinco wrote:
I did this function:

void clean(string&s)
{
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
s[i]=tolower(s[i]);
else
s.erase(i,1);
}
return;
}

I'm having a strange behavior with string like these:

"Disneyland " -> Disneyland
".disneylan d." -> .disneyland"

It seems as if there is something going on with the erase function that
i don't know, because with this:

void clean(string&s)
{
string s2;
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
{
s[i]=tolower(s[i]);
s2+=s[i];
}
}
s=s2;
return;
}
Everything goes OK.

What's wrong with the first code?

Thanks.


Well yes, erase() removes chars, so the size has changed.
Here's a quick and dirty example:

#include <iostream>
#include <string>
#include <cctype>

void clean( std::string& s )
{
std::string::it erator it;

// step thru the string using the iterator 'it'
for( it = s.begin(); it != s.end(); )
{
// if the current char is an alpha
if( isalpha(*it) )
{
// change the char to lower case
*it = tolower(*it);

// increment 'it' to the next char
++it;
}
else
{
// delete the current non-alpha char from 's'.
// resets 'it' to point to the first char after
// the one just deleted - or to s.end() if no
// more chars in 's'.
it = s.erase(it);
}
}

return;
}

int main()
{
std::string t1 = "Disneyland ";
std::string t2 = ".disneylan d.";

clean(t1);
clean(t2);

std::cout << t1 << std::endl;
std::cout << t2 << std::endl;

return 0;
}

This program outputs:

disneyland
disneyland

Regards,
Larry


In cases like this, i prefer to traverse the container in reverse order.
In that case the part of the container we have already examined is
shifted and presents no problem.

Wouldn't you agree?


I didn't intend to optimize the original 'clean()', just make
it work correctly; otherwise, I might have been tempted to use
'remove_if()' and/or 'translate()'

Regards,
Larry
Jun 3 '06 #9
dj
Larry I Smith wrote:
dj wrote:
Larry I Smith wrote:
Gaijinco wrote:
I did this function:

void clean(string&s)
{
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
s[i]=tolower(s[i]);
else
s.erase(i,1);
}
return;
}

I'm having a strange behavior with string like these:

"Disneyland " -> Disneyland
".disneylan d." -> .disneyland"

It seems as if there is something going on with the erase function that
i don't know, because with this:

void clean(string&s)
{
string s2;
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
{
s[i]=tolower(s[i]);
s2+=s[i];
}
}
s=s2;
return;
}
Everything goes OK.

What's wrong with the first code?

Thanks.

Well yes, erase() removes chars, so the size has changed.
Here's a quick and dirty example:

#include <iostream>
#include <string>
#include <cctype>

void clean( std::string& s )
{
std::string::it erator it;

// step thru the string using the iterator 'it'
for( it = s.begin(); it != s.end(); )
{
// if the current char is an alpha
if( isalpha(*it) )
{
// change the char to lower case
*it = tolower(*it);

// increment 'it' to the next char
++it;
}
else
{
// delete the current non-alpha char from 's'.
// resets 'it' to point to the first char after
// the one just deleted - or to s.end() if no
// more chars in 's'.
it = s.erase(it);
}
}

return;
}

int main()
{
std::string t1 = "Disneyland ";
std::string t2 = ".disneylan d.";

clean(t1);
clean(t2);

std::cout << t1 << std::endl;
std::cout << t2 << std::endl;

return 0;
}

This program outputs:

disneyland
disneyland

Regards,
Larry

In cases like this, i prefer to traverse the container in reverse order.
In that case the part of the container we have already examined is
shifted and presents no problem.

Wouldn't you agree?


I didn't intend to optimize the original 'clean()', just make
it work correctly; otherwise, I might have been tempted to use
'remove_if()' and/or 'translate()'

Regards,
Larry


Going reverse is by no means optimization - it is exactly what you said
you try to achieve - correctness. The only difference is in the "for" line.
Jun 3 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
1851
by: muser | last post by:
is string converted to its integer equivalent by minusing it by 48? the function is suppose to check the fifth digit of struct member using the formula contained within the function. The function isn't doing that at the moment, hence this post and the querie above. string must be of type char. bool checkdigitforcustomercode( char* string ) {
7
10629
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using namespace std; : : vector<string>* getTyphoon()
6
6556
by: lenny | last post by:
Hi, I've been trying to use a Sub or Function in VBA to connect to a database, make a query and return the recordset that results from the query. The connection to the database and the query works fine, but passing the resulting recordset back to the sub's caller is not working out.
18
6178
by: Ian Stanley | last post by:
Hi, Continuing my strcat segmentation fault posting- I have a problem which occurs when appending two sting literals using strcat. I have tried to fix it by writing my own function that does the strcat (mystract). Program below. However this appears not to have fixed the problem and I don't know why it shouldn't ? Any further help as to what else I am doing wrong will be appreciated regards
12
9646
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" & Chr(34) & PopUpWindowTitle & Chr(34) & ", " & Chr(34) & CurrentEventDetails & ")" strTemp += "<BR><A HREF='#' onClick='" & PopupLink & "'>" & EventName & "</A><BR>" The problem I have is that when the string variables or contain a string with an...
7
5231
by: Ankit Aneja | last post by:
I put the code for url rewrite in my Application_BeginRequest on global.ascx some .aspx pages are in root ,some in folder named admin and some in folder named user aspx pages which are in user folder are using this code of url rewrite project is running completely fine on localhost but after uploading first page (http://emailware.net.temporary.domain.name/user/index.aspx) is fine but as i click 123 Easy-CD Ripper
4
8011
by: David Scemama | last post by:
Hi, I'm trying to read a database file written from a turbo Pascal program. I've set a structure to map the records in the file, but I have problem reading the file when I use VBFixedArray in my structure instead of VBFixedString. Here is the original code that works: Structure CCat
16
4930
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by Microsoft must be installed on their servers. Now german Umlaute (ä, ü, ö) and quotes are returned incorrectly in SOAP fault responses. This can be easily verified: Implement the following in a web service method (just raises a SOAPException with a...
5
19600
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was having a problem in the calling program. I searched online and found suggestions that I return an Array instead so I modified my code (below) to return an Array instead of an ArrayList. Now I get the message when I try to run just my webservice...
8
1280
by: Rinaldo | last post by:
Hi, When I start my program in the debugger, there is no problem, but when not I get an exception. It appears in: private void Upload(string filename, string FTnaam) { MessageBox.Show("in upload.");
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10214
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.