473,399 Members | 3,888 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,399 software developers and data experts.

Capitalize first letter of word

3
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];
  5.  
  6.            do {
  7.                    read(word);
  8.             } while(strcmp(word, "\n") != 0);
  9.  
  10.             return 0;
  11. }
  12.  
  13. void read(char word[])
  14. {
  15.             cin >> word;
  16.             if(strcmp(word, "#") != 0)
  17.                      cout << word << " ";
  18.             else
  19.                      cout << endl;
  20. }
  21.  
Dec 9 '07 #1
8 4010
Laharl
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
romo14
3
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
Laharl
849 Expert 512MB
word[i]=toupper(word[i]);

As for using it, which letters in each token (besides P.O, that one's the exception) must be capitalized?
Dec 10 '07 #4
romo14
3
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
sicarie
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
joemc
3
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;
  3.  
  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"
  10.  
  11.   while(1){}
  12. return 0;
  13. }
  14.  
output:
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);
edit:
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
Laharl
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
joemc
3
<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

3
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%;
10
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,...
3
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...
1
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
5
by: DDK | last post by:
What is the best way to capitalize the first letter of a string? Thanks for any help, d.
1
blyxx86
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: ...
7
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...
12
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"
12
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.