Sunday, July 12, 2009

How to clean strings from specified characters in c++ ?

I'm including strings.h


then I have a string value stored


then I want this value cleaned from letters I don't want like ( , " [ ] { } )


for example if value="Test]"


I want it to get out of the function value="Test"

How to clean strings from specified characters in c++ ?
Maybe you could make a method to remove certain characters from a string like string removeChars( string in, string chars )





The first param is the string you want to change and the second is the characters that you want to remove from the string. The method would use a for loop that loops through the chars string and in the for loop you would have a while loop that keeps on looping while the input string contains the character in the chars string at the index of the for loop. I don't think I said that very well, but the code might look like this:





string removeChars( string in, string chars );


{


for ( int i = 0; i %26lt; chars.length(); i++ )


{


char theChar = (chars.c_str())[ i ];


int f = in.find_first_of( theChar );


while ( f != -1 )


{


in.replace( f, 1, "" );


}


}





return in;


}





Then, somewhere else in the program, you could call it like this: string cleanString = removeChars( "(in}[)]", "(){}[]" ); Then cleanString would equal "in".
Reply:Include ctype.h and use the function isalpha(int a). it takes integer as an input so it would be better if u input the ascii value of that character. there are more functions like isalnum(int a) if you want chars and numbers only!


check it on each character of the string and remove those who doesnt matches your needs.


go to help%26gt;index%26gt;isalpha() to know more or to ctype.h.


and if you want me to make that program for you then mail me! okay! happy programming! byebye!!


No comments:

Post a Comment