How does one tolower an entire string?

Thread Solved
Reply

Join Date: Sep 2006
Posts: 234
Reputation: FireSBurnsmuP is an unknown quantity at this point 
Solved Threads: 1
FireSBurnsmuP's Avatar
FireSBurnsmuP FireSBurnsmuP is offline Offline
Posting Whiz in Training

How does one tolower an entire string?

 
0
  #1
Oct 6th, 2006
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, 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!
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 90
Reputation: Inanna is on a distinguished road 
Solved Threads: 6
Inanna's Avatar
Inanna Inanna is offline Offline
Junior Poster in Training

Re: How does one tolower an entire string?

 
0
  #2
Oct 6th, 2006
kk, if this is C then you use a test against '\0' to look for the end and do sumthin' like this.
  1. int i;
  2.  
  3. for (i = 0; str[i] != '\0'; i++)
  4. str[i] = (char)tolower(str[i]);
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.
  1. char *p;
  2.  
  3. for (p = str; *p != '\0'; p++)
  4. *p = (char)tolower(*p);
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.
  1. void StrLower(char str[])
  2. {
  3. int i;
  4.  
  5. for (i = 0; str[i] != '\0'; i++)
  6. str[i] = (char)tolower(str[i]);
  7. }
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?
  1. struct Lowey {
  2. int operator()(int c)
  3. {
  4. return std::tolower(c);
  5. }
  6. };
  7.  
  8. std::transform(str.begin(), str.end(), str.begin(), Lowey());
transform comes from the algorithm header, and tolower is in cctype. You can also use tolower directly, but it's troublesome.
  1. std::transform(str.begin(), str.end(), str.begin(), std::tolower);
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...
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 234
Reputation: FireSBurnsmuP is an unknown quantity at this point 
Solved Threads: 1
FireSBurnsmuP's Avatar
FireSBurnsmuP FireSBurnsmuP is offline Offline
Posting Whiz in Training

Re: How does one tolower an entire string?

 
0
  #3
Oct 7th, 2006
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++?
Last edited by FireSBurnsmuP; Oct 7th, 2006 at 2:15 am.
Damn computer! It ate everything!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 65
Reputation: GloriousEremite will become famous soon enough GloriousEremite will become famous soon enough 
Solved Threads: 14
GloriousEremite GloriousEremite is offline Offline
Junior Poster in Training

Re: How does one tolower an entire string?

 
0
  #4
Oct 7th, 2006
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?"
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: How does one tolower an entire string?

 
0
  #5
Oct 7th, 2006
Originally Posted by FireSBurnsmuP View Post
So, how do I get the length of a string in C++?
stringvariable.length()
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,514
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 168
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: How does one tolower an entire string?

 
0
  #6
Oct 7th, 2006
Here is a mildly more modern way:
  1. // convert string to all lower case
  2.  
  3. #include <algorithm>
  4. #include <cctype>
  5. #include <iostream>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. int lower_case ( int c )
  11. {
  12. return tolower ( c );
  13. }
  14.  
  15. int main()
  16. {
  17. string s = "THIS IS A TEST";
  18.  
  19. transform( s.begin(), s.end(), s.begin(), lower_case );
  20. cout << s << endl;
  21. }
I know you are looking also at Python:
  1. str1 = "THIS IS A TEST"
  2. str2 = str1.lower()
  3. print str2
Or as a one-liner:
  1. print "THIS IS A TEST".lower()
Last edited by Ene Uran; Oct 7th, 2006 at 5:16 pm.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 234
Reputation: FireSBurnsmuP is an unknown quantity at this point 
Solved Threads: 1
FireSBurnsmuP's Avatar
FireSBurnsmuP FireSBurnsmuP is offline Offline
Posting Whiz in Training

Re: How does one tolower an entire string?

 
0
  #7
Oct 8th, 2006
Alrighty, got the function written, thanks!
Damn computer! It ate everything!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: geinjo is an unknown quantity at this point 
Solved Threads: 0
geinjo geinjo is offline Offline
Newbie Poster
 
0
  #8
23 Days Ago
std::transform(str.begin(), str.end(), str.begin(), ::tolower);

this is the solution, note the lack of std before ::tolower.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC