User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 392,009 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,269 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1444 | Replies: 2
Reply
Join Date: Dec 2007
Posts: 1
Reputation: roxanne14 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
roxanne14 roxanne14 is offline Offline
Newbie Poster

Capitalize first letter of word

  #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:

#include <iostream>
#include <iomanip>
#include <cctype>
#include <cstring>

using namespace std;

void read(char word[]);

int main ()
{
           cout << "Enter address: \n";
           char word[40];

           do {
                   read(word);
            } while(strcmp(word, "\n") != 0);

            return 0;
}

void read(char word[])
{
            cin >> word;
            if(strcmp(word, "#") != 0)
                     cout << word << " ";
            else
                     cout << endl;
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Location: UK
Posts: 468
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Rep Power: 5
Solved Threads: 42
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Capitalize first letter of word

  #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  
Join Date: Feb 2005
Location: 55 26'N 118 46'W
Posts: 181
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 13
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: Capitalize first letter of word

  #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
char word [80];
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
int pntr;
for (pntr = 0; pntr < strlen (word); pntr++) 
     .... conditional code here
        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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 10:30 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC