473,396 Members | 1,812 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,396 software developers and data experts.

Function to capitalize string

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[0] = toupper(name[0]);
for(int i = 0; i < len; i++)
name[i] = tolower(name[i]);
return name;
}

Jul 19 '05 #1
5 20043
Rick wrote:
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[0] = toupper(name[0]);
for(int i = 0; i < len; i++)
name[i] = tolower(name[i]);
return name;
}


string convname(string name)
{
int len;
len = name.length();
if ( len > 0 )
{
name[0] = toupper(name[0]);
for(int i = 1; i < len; i++)
name[i] = tolower(name[i]);
}
return name;
}

issues:

a) you referenced name[0] even though the string may be 0 length

b) the loop started at position 0 - overwriting the first toupper
assignment.

Jul 19 '05 #2

"Rick" <rf*****@NOSPAMcox.net> wrote in message
news:ZPQtb.3364$Ue4.1003@fed1read01...
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[0] = toupper(name[0]);
for(int i = 0; i < len; i++)
name[i] = tolower(name[i]);
return name;
}

for(int i = 1; i < len; i++)

Notice the 1 instead of zero.

Regards, Ron AF Greve.

Jul 19 '05 #3
Gianni Mariani wrote:
Rick wrote:
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[0] = toupper(name[0]);
for(int i = 0; i < len; i++)
name[i] = tolower(name[i]);
return name;
}


string convname(string name)
{
int len;
len = name.length();
if ( len > 0 )
{
name[0] = toupper(name[0]);
for(int i = 1; i < len; i++)
name[i] = tolower(name[i]);
}
return name;
}

issues:

a) you referenced name[0] even though the string may be 0 length

b) the loop started at position 0 - overwriting the first toupper
assignment.


Thanks. Guess I overlooked small details.

Jul 19 '05 #4

"Rick" <rf*****@NOSPAMcox.net> wrote in message
news:ZPQtb.3364$Ue4.1003@fed1read01...
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[0] = toupper(name[0]);
for(int i = 0; i < len; i++)
name[i] = tolower(name[i]);
return name;
}


the for loop needs to start at 1, not zero
Jul 19 '05 #5
Gianni Mariani wrote:
Rick wrote:
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[0] = toupper(name[0]);
for(int i = 0; i < len; i++)
name[i] = tolower(name[i]);
return name;
}


string convname(string name)
{
int len;
len = name.length();
if ( len > 0 )
{
name[0] = toupper(name[0]);
for(int i = 1; i < len; i++)
name[i] = tolower(name[i]);
}
return name;
}

issues:

a) you referenced name[0] even though the string may be 0 length

b) the loop started at position 0 - overwriting the first toupper
assignment.


Another issue that might be visible or not on your system, depending on
the signedness of char: touppoer and tolower want their parameter
converted to an unsigned char. So actually has to be:
string convname(string name)
{
int len;
len = name.length();
if ( len > 0 )
{
name[0] = toupper((unsigned char)name[0]);
for(int i = 1; i < len; i++)
name[i] = tolower((unsigned char)name[i]);
}
return name;
}

Oh, and calling length() on a string and then using that for a loop to
iterate through it can be quite inefficient on some implementations
(just the same as strlen for C style strings). You can work around that
by using iterators:

string convname(string name)
{
string::iterator it(name.begin());

if (it != name.end())
name[0] = toupper((unsigned char)name[0]);

while(++it != name.end())
{
*it = tolower((unsigned char)*it);
}
return name;
}
All code untested, but should mostly work.

Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Nige | last post by:
Repost - as some readers took got sidetracked. To save me re-inventing the wheel, does anyone have a function to capitalise each word in form data? I was thinking along the lines of capitalise...
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,...
9
by: drhowarddrfinedrhoward | last post by:
I see a number of pages with functions like MM_somefunction(). Where does the MM_ come from? I don't see it in any books I'm studying.
5
by: DDK | last post by:
What is the best way to capitalize the first letter of a string? Thanks for any help, d.
7
by: tsnyder | last post by:
I need to define a field so that when a lowercase letter is put in the field it turns to an uppercase letter. What would the expression for the field be? My field name is site_id.
1
by: mens libertina | last post by:
Hello, I am programming a little game, and I want the focus to move from one input box to the next as the user types. I have image-input pairs so the user is typing a letter under a picture. ...
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: Robert Johnson | last post by:
Hi all. Checked the docs and all I can find is UCase but that will convert everything to upper. What I want is when my users enter say a name into a text box I want to just upper the first char for...
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...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.