424,306 Members | 905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,306 IT Pros & Developers. It's quick & easy.

Function to capitalize string

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.