473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Truncating spaces off of the end of C++ style strings

OK, so I have a function that deals with strings, fixing them so that I can handle them easier. Here is the function:
Expand|Select|Wrap|Line Numbers
  1. string fix(string str)
  2. {
  3.        int length = str.size();
  4.        for (int i = 0; i < length; ++i)
  5.        {
  6.            str[i] = tolower(str[i]);
  7.        }
  8.  
  9.        for (int i = length; str[i] == " " || str[i]==0; --i)
  10.        {
  11.            str.erase(i);
  12.        }
  13.        return str;
  14. }
  15.  
the problem is, that it doesn't like the part where I truncate off the whitespace (or at least I try to, the method I am using is probably crap), it wont compile the second for loop, because
ISO C++ forbids comparison between pointer and integer
BTW, I am using Dev-C++ if that has anything to do with it.

So my question is, how would I do this? The reason I have to truncate the spaces is I am using getline, and have functions later on that count the length of the string...
Oct 18 '07 #1
2 1992
Ganon11
3,652 Expert 2GB
While the last character is a ' ' (space) character, use the .substr() method to get the portion before the last character.

It's not efficient at all, but it's what I use.
Oct 18 '07 #2
yeah, but how would I do that?
Expand|Select|Wrap|Line Numbers
  1. while(str[str.length()-1]==' ')
  2. {
  3.     str = str.substr(0,str.length()-1);
  4. }
  5.  
it tells me I cannot do that, but will let me do this:
Expand|Select|Wrap|Line Numbers
  1. while(str.substr(str.length()-1,1)==' ')
  2. {
  3.     str = str.substr(0,str.length()-1);
  4. }
  5.  
Can anyone tell me why?

But my original question has been answered, thank you.
Oct 18 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Hal Vaughan | last post by:
I have a list of variables and values (merge fields for a merge program) and I want to put them in a list in a <SELECT> object. I'd like them to be lined up, with an "=" midway between the field...
15
by: Randall Parker | last post by:
I've noticed when exporting from Microsoft Word XP into an HTML file that Word uses a span style of mso-spacerun: yes. This has the effect of making there be about 2 spaces between sentences. So...
4
by: rajdb2 | last post by:
Hi, I am using the following sql statement SELECT rtrim(rtrim(coalesce(substr(char(v.creationdate),1,4) || '-' || substr(char(v.creationdate),6,2) || '-' || substr(char(v.creationdate),9,2) ||...
1
by: Jitesh Sinha | last post by:
Hi, I am running Windows 2003/ IIS 6.0. I was stuck with rather a abnormal behaviour of System.Web.mail class. It was truncating the message body after 3,071 character. The code i was testing...
2
by: Chris Kratz | last post by:
We are having a weird problem that we ran into recently. If I use the following statements to create a test table and then run the select statement at the end, we get a very strange sort order. ...
17
by: tommy | last post by:
Hi all, I' m adding strings to some fields in my table via Access. The strings sometimes have trailing spaces and I really need to have it that way, but Access truncates trailing spaces. How can...
135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
40
by: raphfrk | last post by:
I have a program which reads in 3 filenames from the command line prog filename1 filename2 filename3 However, it doesn't work when one of the filenames has spaces in it (due to a directory...
3
by: Francois | last post by:
Hello, How can I size my text to fit in a datagrid column ? I do not want the datagrid go multiline. I am using WEB Forms, not windows form, so CreateGraphics ans MesasureString do not work....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.