Link to home
Start Free TrialLog in
Avatar of AntoniRyszard656
AntoniRyszard656

asked on

Testing strings?

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?
Avatar of Member_2_1001466
Member_2_1001466

Use the indexing operator to get a char of a string.

if(str[str.length()-1] == '#'){
//some action
}
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)
Avatar of AntoniRyszard656

ASKER

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;  
}
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;
}

Could I use getchar()?
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
Thanks

Do you think changing the string input to a char * could make the end character easier to test?
ASKER CERTIFIED SOLUTION
Avatar of stefan73
stefan73
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
> 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:

https://www.experts-exchange.com/questions/20180499/getline.html

--efn
Thanks I will try this, I am using Dev-C++ which I believe uses the latest compile.