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

How do I capitalize first letter of all words in a string?

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 string like the examples shown.

Any ideas????
Jul 6 '08 #1
7 16115
JosAH
11,448 Expert 8TB
Capitalize the first character of the string as well as all non white space characters
that follow a white space character. Every other character should be changed to
its lowercase form whereever applicable,

kind regards,

Jos
Jul 6 '08 #2
void reformatcourse (string& course)
//Given the course name (string data type), reformatcourse will capitalize the
//first letter of the course name and make the remaining letters of the course
//name lowercase. The updated coursename is then passed back.
{
int length;
length = course.length();
course [0] = toupper(course [0]);
for (int i=1; i<length; i++)
{ if (course[i] != ' ')
{course[i] = tolower(course[i]);
}
if (course[i] = ' ')
{course[i+1] = toupper(course[i+1]);
i++;
}

}

}


This is what i have and it's not working. Do you notice any errors I could fix. I've been working on this since last night and I just can't figure it out.
Jul 6 '08 #3
gpraghuram
1,275 Expert 1GB
Hi,
The problem is in the line
Expand|Select|Wrap|Line Numbers
  1. //if (course[i] = ' ')
  2. //You have used assignment operator instead of comparison operator.
  3. //change that to
  4. if (course[i] == ' ')
  5.  
And everything is fine

Raghu
Jul 7 '08 #4
whodgson
542 512MB
.......something like this will do it to.
Expand|Select|Wrap|Line Numbers
  1. int l= s.length();
  2.     s[0]=toupper(s[0]);              //capitalizes 
  3.     cout<<s[0];                        //and prints first letter
  4.                for(int i=1;i<l;i++)     //now starting with s[1]
  5.                {
  6.                if(s[i]!=' ')                  //prints next char providing not a space
  7.                {cout<<s[i];
  8.                continue;}                //repeat for all following lower case
  9.                   if( s[i]=' ')              //if a space print it
  10.                   {cout<<' ';
  11.                   i++;                       //and increment
  12.                   s[i]=toupper(s[i]);   //capitalize letter after space
  13.                   }
  14.                   cout<<s[i];           //and print it
  15.  
  16.               }
Jul 10 '08 #5
Banfa
9,065 Expert Mod 8TB
.......something like this will do it to.
Expand|Select|Wrap|Line Numbers
  1. int l= s.length();
  2. <snipped>                  cout<<s[i];           //and print it
  3.  
  4.               }
No it wont, it doesn't lower case letters that are upper case and in the middle of a word.
Jul 10 '08 #6
whodgson
542 512MB
Quite so Banfa, I wrongly assumed they were all in lower case to start with despite the example.
So add a for loop which does that first.
e.g.
Expand|Select|Wrap|Line Numbers
  1. for(int i=0;i<l;i++)
  2.   s[i]=tolower(s[i]);
Jul 11 '08 #7
hdanw
61
Use the Shift Key.

;)
Jul 12 '08 #8

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

Similar topics

5
by: Rick | last post by:
For some reason my function to capitalize the first letter in a string and keep all the other letters lowercase isn't working. This is what I have. string convname(string name) { int len;...
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...
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
by: Laurence Nuttall | last post by:
In vb 6 when I typed in if then else endif vb 6 would automatically capitalize the if then else and endif words, ..net does not How can i get .net to capitalize keywords?
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: ...
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: 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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.