I have a required where i need to extract a number from a string in the needed format like :
- f001–>100
- f100–>1
- 2030–>302
- 0203–>3020
- 2031–>1302
so the operation above is :
- remove any f character if it is present
- reverse the string
- remove leading zeros from the string
I have written a code which is working fine in c++:
int main(int argc,char* argv[])
{
string str1(argv[argc-1]);
reverse(str1.begin(),str1.end());
str1.erase(remove(str1.begin(),str1.end(),'f'),str1.end());
str1.erase(0,str1.find_first_not_of('0',0));
cout <<str1<<endl;
return 0;
}
is there any better way of doing the same thing?
I Guess, a simple function as below will do the job
NOTE:- It is not a complete program. Just a flow… It will parse the string once in stead of 3 times as in your case. I definitely hope there are better C++ style approach, and looking forward to the same.
foo(char* str) {
int state=0;
int len=strlen(str);
for(i=len-1; i>=0; i++) {
}
}