There is a standard function called tolower() (and a companion function called toupper()) that can be used to evaluate each char of a string one at a time and change it to the appropriate case as needed. So to use it, look up tolower() in your compilers help section and make sure you include any necessary standard headers. Then obtain input string and determine it's length. Then loop through the string looking at each character one at a time, passing each char to tolower() as you go
inputString[i] = tolower(inputString[i]);
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
If you say so. But you wouldn't give a newbie that.
>Actually, strlen(str.c_str()) could be replaced with, say "str.length" perhaps.
Try reading my link again. Then you might realise what you're doing wrong. :)
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
str.length() <-- Brackets
:lol:
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
str.lenght() <-- Enclosing Parenthesis
Er not were I'm from. All I was pointing out was you forgot them when u said use str.length.
The main thing is you have learnt why it is a bad idea to use strlen() in a for loop and why it is bad to give newbies non-intuitive code as examples. So overall, everyone is happy. Yay.
:lol:
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
I have written pages for my site regarding that issue. To you it is not intuitive. Unless the newbie has a very large string, using strlen isn't a truely bad thing to do; because it will not have to loop many times through each element to get the legnth each time the for loop, loops.
Programmer's don't take anything for granted. The usual "the string can't be that long" thing just doesn't cut ice in a practical setting. A piece of code which makes repeated calls to a function inside a loop will always perform worse than a function which doesn't, no matter how forgiving the compiler is.I was just being a smartass as you were being - so it seemed.*ahem*
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
In that case, it would be a good idea to mention at the beginning that the given method is not the best and there are better alternatives since beginners are known to grasp both good and bad equally well.... ;)
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
I like this part: tolower(i[q]);
I've always wanted "to lower an IQ..." :mrgreen:
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396