Connecting Tech Pros Worldwide Forums | Help | Site Map

Truncating numbers at end of string

Alex Leach
Guest
 
Posts: n/a
#1: Feb 7 '07
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.


Victor Bazarov
Guest
 
Posts: n/a
#2: Feb 7 '07

re: Truncating numbers at end of string


Alex Leach wrote:
Quote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


dragoncoder
Guest
 
Posts: n/a
#3: Feb 7 '07

re: Truncating numbers at end of string


On Feb 7, 3:42 pm, "Alex Leach" <ajle...@gmail.comwrote:
Quote:
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

Kai-Uwe Bux
Guest
 
Posts: n/a
#4: Feb 7 '07

re: Truncating numbers at end of string


Alex Leach wrote:
Quote:
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
red floyd
Guest
 
Posts: n/a
#5: Feb 7 '07

re: Truncating numbers at end of string


Alex Leach wrote:
Quote:
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()?

Michael DOUBEZ
Guest
 
Posts: n/a
#6: Feb 7 '07

re: Truncating numbers at end of string


Alex Leach a écrit :
Quote:
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
Closed Thread


Similar C / C++ bytes