Capitalize first letter of word

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2007
Posts: 1
Reputation: roxanne14 is an unknown quantity at this point 
Solved Threads: 0
roxanne14 roxanne14 is offline Offline
Newbie Poster

Capitalize first letter of word

 
0
  #1
Dec 8th, 2007
My assignment says I'm supposed to read in an address all on one line separated by pound signs (eg jane doe # p.o. box 123 # new york, new york 97229 #) and output it with correct capitalization and in proper address format like:

Jane Doe
P.O. Box 123
New York, New York 97229

Also we have to use character arrays, no strings, and cannot use global variables. I've gotten it to output in address format, but I have no idea how to capitalize all the letters that need to be uppercase. This is what I have so far:

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cctype>
  4. #include <cstring>
  5.  
  6. using namespace std;
  7.  
  8. void read(char word[]);
  9.  
  10. int main ()
  11. {
  12. cout << "Enter address: \n";
  13. char word[40];
  14.  
  15. do {
  16. read(word);
  17. } while(strcmp(word, "\n") != 0);
  18.  
  19. return 0;
  20. }
  21.  
  22. void read(char word[])
  23. {
  24. cin >> word;
  25. if(strcmp(word, "#") != 0)
  26. cout << word << " ";
  27. else
  28. cout << endl;
  29. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 488
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Capitalize first letter of word

 
0
  #2
Dec 8th, 2007
If you're able to split each 'word' into separate strings, then you're already half way there

use the toupper function on the first character of that word, to generate the uppercase equivalent of that character (If an uppercase equivalent is available).

  1. word[0] = toupper( word[0] );

This won't solve the problem of converting all letters in acronyms to uppercase; eg, a string of "p.o. box" will only be converted to "P.o. Box", since there is no whitespace character between the 'p' and the 'o'.

- Depending on the exact requirements of your assignment, you may need to do some additional string parsing for characters which follow punctuation.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: Capitalize first letter of word

 
0
  #3
Dec 8th, 2007
Originally Posted by roxanne14 View Post
I've gotten it to output in address format
Your computer must run quite a bit different than mine then.

Once you've entered your string
  1. char word [80];
  2. cin >> word;
or whatever size you think you need, then just cycle through all the characters changing # to carriage returns and anything after spaces and periods to upper case
  1. int pntr;
  2. for (pntr = 0; pntr < strlen (word); pntr++)
  3. .... conditional code here
  4. word [pntr] ^= 0x20;
Invent whatever looping operation you like with DO, FOR or WHILE with either conditionals or SWITCH.

If your code would have at least output in the proper format I would have given you the solution, but it doesn't come anywhere near that.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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