473,765 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

5 New Member
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 16129
JosAH
11,448 Recognized Expert MVP
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
dizzylizzyd514
5 New Member
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 Recognized Expert Top Contributor
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 Contributor
.......somethin g 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 Recognized Expert Moderator Expert
.......somethin g 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 Contributor
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 New Member
Use the Shift Key.

;)
Jul 12 '08 #8

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

Similar topics

5
20065
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; len = name.length(); name = toupper(name); for(int i = 0; i < len; i++)
3
23201
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
2440
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, Aaron
3
5984
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 Bezel and (Black), but it is only finding the bezel and leaving the (black) in lower case. Here is the script below. Any help would be greatly appreciated. <SCRIPT LANGUAGE="JAVASCRIPT"> <!--
5
8628
by: DDK | last post by:
What is the best way to capitalize the first letter of a string? Thanks for any help, d.
1
1595
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
6281
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: Private Sub Serial_AfterUpdate() 'Makes serial field input all caps into database no matter what. Me.Serial = StrConv(Me.Serial, vbUpperCase) End Sub
12
4001
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
12412
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 another way?
0
9399
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10161
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9833
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7378
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.