943,815 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7893
  • C++ RSS
Dec 8th, 2007
0

Capitalize first letter of word

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
roxanne14 is offline Offline
1 posts
since Dec 2007
Dec 8th, 2007
0

Re: Capitalize first letter of word

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).

CPP Syntax (Toggle Plain Text)
  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.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Dec 8th, 2007
0

Re: Capitalize first letter of word

Click to Expand / Collapse  Quote originally posted by roxanne14 ...
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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 47
Solved Threads: 17
Posting Whiz in Training
Tight_Coder_Ex is offline Offline
215 posts
since Feb 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: graphics programing in C & c++
Next Thread in C++ Forum Timeline: Help needed in designing a c++ program using hashing and linear probing!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC