| | |
How does one tolower an entire string?
Thread Solved
![]() |
You see, I am writing a program that uses strings for input to determine things, and I don't want it to be case sensitive.
So, I know you can go through a string and go char by char to store each char,
Any help is appreciated!
So, I know you can go through a string and go char by char to store each char,
tolower()'d into another string while in a for loop, but I don't know how to get the string's length or how to get char positions in strings anymore (it's been a while).Any help is appreciated!
Damn computer! It ate everything!
kk, if this is C then you use a test against '\0' to look for the end and do sumthin' like this.
One of the few things a cutie like me is anal about is avoiding pointers when they aren't needed. Employers like to think that this is a better solution. I haven't figured out why yet.
In fact, at my last interview, the dude's insistence on using pointers for something stupid was the deal breaker for an otherwise sweet gig. That's how silly I am about it. 
But! Back to the present, the first loopy makes it clear that you're working with a string. Not just a string, a string that lives in an array. In other words, you can change it! If it's a pointer, you jus' can't be sure, can ya? Sure, sure, if it's a function parameter then the point is moot because you've got a pointer whether you like it or not. But at the very least, the array notation is a cool little mnemonic for those of us that just aren't that bright. Words of wisdom (ha!) from a grunt in the trenches, yo.
If this is C++ then you've got options. And do I mean options! It's not C++ if you don't complicate the world, right?
transform comes from the algorithm header, and tolower is in cctype. You can also use tolower directly, but it's troublesome.
The biggest trouble is that both iostream and cctype declare a tolower, and there'd be an ambigimuity. :p But people look at me funny if I don't abusively use the STL in my C++ codage. Hmm, then again, they may look at me funny because I do...
C++ Syntax (Toggle Plain Text)
int i; for (i = 0; str[i] != '\0'; i++) str[i] = (char)tolower(str[i]);
C++ Syntax (Toggle Plain Text)
char *p; for (p = str; *p != '\0'; p++) *p = (char)tolower(*p);

But! Back to the present, the first loopy makes it clear that you're working with a string. Not just a string, a string that lives in an array. In other words, you can change it! If it's a pointer, you jus' can't be sure, can ya? Sure, sure, if it's a function parameter then the point is moot because you've got a pointer whether you like it or not. But at the very least, the array notation is a cool little mnemonic for those of us that just aren't that bright. Words of wisdom (ha!) from a grunt in the trenches, yo.
C++ Syntax (Toggle Plain Text)
void StrLower(char str[]) { int i; for (i = 0; str[i] != '\0'; i++) str[i] = (char)tolower(str[i]); }
C++ Syntax (Toggle Plain Text)
struct Lowey { int operator()(int c) { return std::tolower(c); } }; std::transform(str.begin(), str.end(), str.begin(), Lowey());
C++ Syntax (Toggle Plain Text)
std::transform(str.begin(), str.end(), str.begin(), std::tolower);
Oh yeah, this is C++, btw, sorry not to mention that.
I didn't know about the ambiguity with cctype and iostream, that's interesting...
And I was planning on using something similar to your first C example in this case, but I was going to have a get char type thing that would store into a temp string, but I guess I forgot how to get the length of a string.
So, how do I get the length of a string in C++?
I didn't know about the ambiguity with cctype and iostream, that's interesting...
And I was planning on using something similar to your first C example in this case, but I was going to have a get char type thing that would store into a temp string, but I guess I forgot how to get the length of a string.
So, how do I get the length of a string in C++?
Last edited by FireSBurnsmuP; Oct 7th, 2006 at 2:15 am.
Damn computer! It ate everything!
•
•
Join Date: Jul 2006
Posts: 65
Reputation:
Solved Threads: 14
Do you mean the length of a c-style string (char array) or the length of a std::string? You could use strlen for the former, and the length() member function for the latter
Last edited by GloriousEremite; Oct 7th, 2006 at 3:18 am.
"What are the roots that clutch, what branches grow
out of this stony rubbish?"
out of this stony rubbish?"
Here is a mildly more modern way:
I know you are looking also at Python:
Or as a one-liner:
C++ Syntax (Toggle Plain Text)
// convert string to all lower case #include <algorithm> #include <cctype> #include <iostream> #include <string> using namespace std; int lower_case ( int c ) { return tolower ( c ); } int main() { string s = "THIS IS A TEST"; transform( s.begin(), s.end(), s.begin(), lower_case ); cout << s << endl; }
C++ Syntax (Toggle Plain Text)
str1 = "THIS IS A TEST" str2 = str1.lower() print str2
C++ Syntax (Toggle Plain Text)
print "THIS IS A TEST".lower()
Last edited by Ene Uran; Oct 7th, 2006 at 5:16 pm.
drink her pretty
![]() |
Similar Threads
- Creating a Basic String Database (C++)
- String maniputlation runtime error (C++)
- Removing characters from a string (C)
Other Threads in the C++ Forum
- Previous Thread: homework
- Next Thread: Input files with multiple columns
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes code coding compaitibility compile console conversion count delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error factorial file floatingpoint forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple net news node number numbertoword output parameter payment pointer problem program programming project projectassignmenthelp protection python random rank read recursion reference rpg skills string strings temperature template test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






