473,378 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 4006
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

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

Similar topics

by: news.individual.net | last post by:
Hi! Can I capitalize the first letter of the first paragraph without using a special class for that <p> ? I tried it with this: body > p:first-child:first-letter { font-size: 270%;
by: Aaron | last post by:
string a = "i have an apple"; i want the output to be "I Have An Apple"; i was able to capitalize the first word but can't figure out how to capitalize every word in that string Thanks,...
by: gil | last post by:
I have a script that will capitalize the first letter of every word in my descriptions. But some of my descriptions contain ( ) and /. For example bezel (black). I need the script to capitalize...
by: Dwight Shubert | last post by:
Hi all, This is my first try using Access. I understand how to format a text field for uppercase and lowercase, but how do you capitalize only the first letter of each word? tia Dwight
by: DDK | last post by:
What is the best way to capitalize the first letter of a string? Thanks for any help, d.
by: blyxx86 | last post by:
I have the code to capitalize an entire string when it is input, but would like to capitalize just the first letter of a box, or match the case of a drop down list.. My current code is thus: ...
by: dizzylizzyd514 | last post by:
I need to make this: introductoRy speEch ==> Introductory Speech pRecalCulus 1 and 2 ==> Precalculus 1 And 2 I know how to capitalize the first letter of a word, but not multiple words in a...
by: jackson.rayne | last post by:
Hello, I am a javascript newbie and I'm stick at one place. I have a requirement where I will get a sentence in a variable example var v1 ="This is a sentence"
by: dissectcode | last post by:
I know about toupper() and tolower() but I need to capitalize (or de-capitalize)the first letter of a word in a header file. I know there are no preprocessor functions that do that but is there...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.