470,859 Members | 1,579 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,859 developers. It's quick & easy.

Capitalize first letter of word

I'm working on a program that will take an address in on all one line separated by pounds (eg karen smith # p.o. box 123 # new york, new york #) and outputs it in user-friendly format. So far my program outputs:
karen smith
p.o. box 123
new york, new york

The only thing I can't figure out how to do is to capitalize the words that need capitalization. I know I need to use the toupper function and I think a for loop, but I don't know how to make it work. So far I have this:

Expand|Select|Wrap|Line Numbers
  1. int main ()
  2. {
  3.            cout << "Enter address: \n";
  4.            char word[40];
  6.            do {
  7.                    read(word);
  8.             } while(strcmp(word, "\n") != 0);
  10.             return 0;
  11. }
  13. void read(char word[])
  14. {
  15.             cin >> word;
  16.             if(strcmp(word, "#") != 0)
  17.                      cout << word << " ";
  18.             else
  19.                      cout << endl;
  20. }
Dec 9 '07 #1
8 3801
849 Expert 512MB
Well, you'll probably need some specialized code for P.O., as it works differently from the others. Otherwise, you can toupper the appropriate letter as each 'word' will have the same general structure that needs capitalization.
Dec 9 '07 #2
How exactly would I use toupper in this case? I've looked all over the web but I can't figure out how to make it work properly.
Dec 10 '07 #3
849 Expert 512MB

As for using it, which letters in each token (besides P.O, that one's the exception) must be capitalized?
Dec 10 '07 #4
It should output like:
Karen Smith
P.O. Box 123
New York, New York

Since it's the first letter of every word, I know there is a way to get the first character after whitespace and capitalize that letter, I'm just not sure what it is.

I tried cin.get(word); and then for(int i=0;i<strlen(word);++i) word[i]=toupper(word[i]) but I it wouldn't compile so I'm pretty certain I'm doing something wrong.
Dec 10 '07 #5
4,677 Expert Mod 4TB
Can you post the errors you are getting? As well as the lines of code the errors are pointing to (they are specified in the error message).
Dec 10 '07 #6
My grammar, spelling, and code may all be wrong!

If this were my problem i would start by writing the program below
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>                 
  2. using namespace std;
  4. int main ()
  5. {
  6.   cout<<"a:"<<(int)'a'<<"\n"
  7.       <<"z:"<<(int)'z'<<"\n"
  8.       <<"A:"<<(int)'A'<<"\n"
  9.       <<"Z:"<<(int)'Z'<<"\n"
  11.   while(1){}
  12. return 0;
  13. }
Expand|Select|Wrap|Line Numbers
  1. a:97    
  2. z:122
  3. A:65
  4. Z:90
so we could assume
if a character were between 97 and 122 it
is lower case and if it were between
65 and 90 it upper case.

and looking at the numbers above it seems that
the following is true
(97-65 == 32) && (122-90 == 32)

so we can assume subtracting 32 would make a lowercase character a capital one
try out
Expand|Select|Wrap|Line Numbers
  1.   cout<<"A:"<< (char)((int)'a'-32);
i wanted to add a question for you;
is there a reason you are using c style strings? i would find it much easier to work with c++ style strings and maybe even a string stream
another edit:
i was just reading the original post you made. I had another suggestion. and that is to use some character other than '#' as a delimiter. Some people writing P.O. BOX # 120 or APT #2. Seems like pound might occur in the strings you are parsing. why not '|' or maybe '^' you have many choices.
Dec 11 '07 #7
849 Expert 512MB
<cctype> has functions for that, isupper() and islower(), that check if a character is an upper/lowercase letter.
Dec 11 '07 #8
<cctype> has functions for that, isupper() and islower(), that check if a character is an upper/lowercase letter.
i missed what he was actualy asking sorry.
Dec 11 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by news.individual.net | last post: by
10 posts views Thread by Aaron | last post: by
1 post views Thread by Dwight Shubert | last post: by
12 posts views Thread by jackson.rayne | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.