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. 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
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
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?
Thank you for all your opinions! This was quite insightful!
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
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
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 )
{
|
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()
|
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.
|
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
|
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...
| |
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
|
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
|
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...
|
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...
|
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.");
|
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...
| |
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...
|
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,...
|
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...
|
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();...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |