Truncating numbers at end of string

A

Alex Leach

I need to check whether a string of letters contains any numbers at
the end of the string, and if so, remove all numbers. There won't ever
be numbers at the beginning or middle of the string. Any ideas on how
to go about this? It's a strange requirement for a program but i can't
get around it. Thanks.
 
V

Victor Bazarov

Alex said:
I need to check whether a string of letters contains any numbers at
the end of the string, and if so, remove all numbers. There won't ever
be numbers at the beginning or middle of the string. Any ideas on how
to go about this? It's a strange requirement for a program but i can't
get around it. Thanks.

Simple (and slow):
While String has characters and the last characer is a number
Erase the last character from the string

A bit more complicated (but faster):
Find the last character in the string that is not a number
If found,
Erase all characters after the found one

That's pseudo-code that you can simply convert to C++ if you know
simple things like 'while', 'isdigit', 'std::string' and its members..

V
 
K

Kai-Uwe Bux

Alex said:
I need to check whether a string of letters contains any numbers at
the end of the string, and if so, remove all numbers. There won't ever
be numbers at the beginning or middle of the string. Any ideas on how
to go about this? It's a strange requirement for a program but i can't
get around it. Thanks.

You could find the last non-digit in the string and cut it off after that
position. There is a function There is a member function
find_last_not_of() that might help.


Best

Kai-Uwe Bux
 
D

dragoncoder

I need to check whether a string of letters contains any numbers at
the end of the string, and if so, remove all numbers. There won't ever
be numbers at the beginning or middle of the string. Any ideas on how
to go about this? It's a strange requirement for a program but i can't
get around it. Thanks.


=> cat trunc.cxx
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

using namespace std;

bool ifNumber ( char c )
{
if (isdigit(c))
return true;
return false;
}

int main()
{
string s = "pankaj123";
cout << "Before erase: " << s << endl;
s.erase(find_if(s.begin(), s.end(), ifNumber), s.end());
cout << "After erase: " << s << endl;
return 0;
}

=> g++ trunc.cxx
=> ./a.out
Before erase: pankaj123
After erase: pankaj

/P
 
R

red floyd

Alex said:
I need to check whether a string of letters contains any numbers at
the end of the string, and if so, remove all numbers. There won't ever
be numbers at the beginning or middle of the string. Any ideas on how
to go about this? It's a strange requirement for a program but i can't
get around it. Thanks.

how about using std::string::find_last_not_of()?
 
M

Michael DOUBEZ

Alex Leach a écrit :
I need to check whether a string of letters contains any numbers at
the end of the string, and if so, remove all numbers. There won't ever
be numbers at the beginning or middle of the string. Any ideas on how
to go about this? It's a strange requirement for a program but i can't
get around it. Thanks.
Use the string::find_last_not_of() method and erase character if return
value is not npos.
size_t pos=str.find_last_not_of("0123456789");
if(pos!=std::string::npos)
{
str.erase(pos+1);
}

Or use the find algorithm with the erase function of string in the same
line (a little more obfuscating)
str.erase( find_if( str.begin(),
str.end(),
ptr_fun(isdigit) ) );

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top