-
Capital Letters?
My program is supposed to read in strings and compare them to words in a "dictionary" (basically a spell checker). However, if a word starts with a capital letter, I'm supposed to change that letter to a lower case letter and check the word again.
How would I change just one letter in the string? I know how to change an individual letter, but I'm a bit lost when it comes to strings...
-
One way would be to walk along the string with a pointer and check if each letter is upper or lower.
-
If char word[] is your word, then the first letter is word[0]. (The same is true even if it's char *word.)
-
So it would be something like this?
Code:
void uppercase(char* checkword)
{
char c = 'N';
if(checkword[0] == isupper(c))
tolower(c);
}
-
Well, let's step through that, shall we?
We define a variable c, which has value 'N'.
Code:
if(checkword[0] == isupper(c))
isupper(c) will be 1, since c is 'N' and isupper('N') is true. So if the first letter in the checkword string has ASCII value 1 (a/k/a smiley face), then we will do
nothing (well, we will call tolower on our temporary variable c, which will return the value of 'n', which we will then promptly ignore).
-
Well, I changed some stuff around to make a little better sense, but I'm still not getting anything to change...
I don't think I'm quite seeing what's going wrong.
Code:
void uppercase(char* checkword)
{
char c = checkword[0];
if(isupper(c))
tolower(c);
}
-
You're not storing the value returned by tolower().
There is nothing wrong with using pointers :), ie 'c' is not required.
-
Well, again, throwing the answer away is very likely going to lead to nothing changing. If you want tolower(c) to go somewhere, put it there (by assigning it to the proper place).
-
Oh! Alright, sorry about that, I get it now.
Thanks!