473,414 Members | 1,964 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,414 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 20044
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: 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
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
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
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...
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,...
0
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...
0
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...

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.