Solved

Testing strings?

Posted on 2004-09-08
11
211 Views
Last Modified: 2010-04-01
Hello,

I was trying to test if the far right character in a string equalled #.

And thought it would be a good idea to trim the right of string to move any spaces.

I found this code on the web:

string rtrim(const string &str){
   int idx = str.find_last_not_of("\t ");
   
   if(idx != string::npos)  
   return str.substr(0,idx+1);
   
   return str;  
}

But, when I tested the string using:

if(string.length()-1 == '#'){
//some action
}

All tested strings seemed to be regarded as if the last character equalled #, even when the # was at the start of the string rather than on the right.

Any thoughts?
0
Comment
Question by:AntoniRyszard656
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 12007206
Use the indexing operator to get a char of a string.

if(str[str.length()-1] == '#'){
//some action
}
0
 
LVL 13

Expert Comment

by:SteH
ID: 12007233
Your comparison was
str.length () -1 == (int) ('#') = 35
so the if part would only be executed ifthe length of the string was 36 (-1 = 35)
0
 

Author Comment

by:AntoniRyszard656
ID: 12007967
Thanks

Would you recommend using this method to remove the empty spaces from the right of a string or is there some better approach?

string rtrim(const string &str){
   int idx = str.find_last_not_of("\t ");
   
   if(idx != string::npos)  
   return str.substr(0,idx+1);
   
   return str;  
}
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:AntoniRyszard656
ID: 12008595
Hello,

I tried using the indexing operator, in this example. But found when I entered:

# test

The if statement was entered, and "Working" displayed. I was hoping only when the string ended with # would the program enter the if statement.

Could anyone see the problem?

Thanks

#include <iostream>
#include <conio.h>
 
 using namespace std;
       
 int main(int argc, char *argv[]){  
    string input;      
   
    while(input!="exit"){
       cout << "Enter text: ";
       cin >> input;            
                         
       if(input[input.length()-1] == '#'){
          cout << endl<< "Working";
          getch();
       }
       
       system("cls");
    }
     
    return 0;
}
0
 

Author Comment

by:AntoniRyszard656
ID: 12008665

Could I use getchar()?
0
 
LVL 15

Expert Comment

by:efn
ID: 12012621
When you read into a string from a stream, you get a word, not a line.  In your test case, because there was a space after the '#', the string was set to the first word only:  "#".  So the program correctly reported that the last character of the string was '#'.

You could try the getline function to read a whole line into the string.

getline(cin, input);

This should work, but when I tried it, I had to hit Enter twice to get the program to accept my input.

--efn
0
 

Author Comment

by:AntoniRyszard656
ID: 12014585
Thanks

Do you think changing the string input to a char * could make the end character easier to test?
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 30 total points
ID: 12015184
Hi AntoniRyszard656,
> Do you think changing the string input to a char * could make the end
> character easier to test?

You'll get "C'ish" code. Sure, you can do:

bool endsWithSharp(char* s){
    if(*s && s[strlen(s)-1] == '#')
        return true;
    else
        return false;
}

...but that's not really C++ :-)

Cheers!

Stefan
0
 

Author Comment

by:AntoniRyszard656
ID: 12015636
Thanks,

Could you also demonstrate the alternative use of getline.

And is there any way to stop the need to press Enter twice, that (efn) mentioned?
0
 
LVL 15

Expert Comment

by:efn
ID: 12023067
> And is there any way to stop the need to press Enter twice, that (efn) mentioned?

Yes, this is a known bug in the Microsoft Visual C++ 6.0 library.  If you don't have that compiler, you may not have the bug.  The fix is here:

http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q240/0/15.ASP&NoWebContent=1

Courtesy of DanRollins in this PAQ:

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20180499.html

--efn
0
 

Author Comment

by:AntoniRyszard656
ID: 12025969
Thanks I will try this, I am using Dev-C++ which I believe uses the latest compile.

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 128
Need some help with Microsoft Visual Studio C++ 2003 5 66
Least Squares Curve Fitting 4 99
Which Linux flavors will this run on? 6 96
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question