1) how do i convert C++ strings to upper case/lower case?
2) say if i got two strings extracted from a paragraph,
pepper?
pepper.
how do i tell my program this 2 are the same strings?
1) how do i convert C++ strings to upper case/lower case?
2) say if i got two strings extracted from a paragraph,
pepper?
pepper.
how do i tell my program this 2 are the same strings?
1) you have to change each char in the string to upper or lower case one at a time There is a thread about this topic on page two of the board started by Asjan. Preludes post at the bottom of the thread gives one version of how to do this if the strings are STL string objects. The other posts offer other ways to do it, too. I think the link below will get you there.
http://cboard.cprogramming.com/showthread.php?t=51230
2) you need to compare the strings. If they are C-type null terminated char array strings then you use strcmp() function from the cstring header file (or string.h header file if your compiler isn't up to date). If the strings are STL string class objects, then you can use the equals operator.
the problem is when i use the = sign, they treat the two strings as different
eg. peter pepper pick a peck of pepper.
the program will extract out
pepper
pepper. <- notice with a fullstop here.
how do i tell the program these tweo strings are same
If you are concerned about the '.' character making your two strings unequal, then it should be easy to remove any punctuation or just specific types of punctuation prior to the comparison in order to make the comparison work as you want it to. You can use the remove to remove just a single type of punctuation or if you want to eliminate every form of punctuation then use remove_if.
Code:#include <algorithm> #include <string> #include <cctype> // For 'ispunct' using namespace std; ... string str; ... // Either this to remove just '.' for example... str.erase(remove(str.begin(),str.end(),'.'),str.end()); // Or this to remove all punctuation from the string... str.erase(remove_if(str.begin(),str.end(),ispunct),str.end());
Last edited by hk_mp5kpdw; 04-01-2004 at 12:26 PM.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
beware, the = sign is the assignment operator, if the lhs has already been declared and the intialization operator if this is the declaration statement, and not the equals operator, which is ==. You can not use either the assignment operator or the equals operator for C-style strings, but you can use both for STL strings.
If you are using C-style strings rather than STL strings, then you can use isalpha() or strtok() to help strip away any punction marks from the strings before comparison. I'm not sure, but I think the remove() functions only work for STL strings.
I've tried it with straight c-style arrays and it works:Originally Posted by elad
Outputs:Code:char array[] = "H.!ell,o' ;Wor?ld:"; // 19 chars long including NULL cout << array << endl; remove_if(array,array+19,ispunct); cout << array << endl;
sizeof(array) is still 19 after the remove_if operation but the NULL gets copied to the new effective end of the punctuation free portion of the string... i.e. if you tried to do this:Code:H.!ell,o' ;Wor?ld: Hello World
Then you would get the rest of the array:Code:cout << array+12 << endl;
Code:or?ld:
Last edited by hk_mp5kpdw; 04-01-2004 at 02:30 PM.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
And some more, probably, as the terminator is missing.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
thanks, hk_mp5kpdw. I don't use any of the STL algorithm functions yet. Time to get on the ball I guess.