Function to capitalize string

R

Rick

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 = tolower(name);
return name;
}
 
G

Gianni Mariani

Rick said:
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 = tolower(name);
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 = tolower(name);
}
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.
 
R

Rick

Gianni said:
Rick said:
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 = tolower(name);
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 = tolower(name);
}
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.
 
M

Moonlit

Rick said:
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 = tolower(name);
return name;
}

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

Notice the 1 instead of zero.

Regards, Ron AF Greve.
 
T

Thomas Wintschel

Rick said:
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 = tolower(name);
return name;
}


the for loop needs to start at 1, not zero
 
R

Rolf Magnus

Gianni said:
Rick said:
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 = tolower(name);
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 = tolower(name);
}
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 = tolower((unsigned char)name);
}
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.
 
Joined
Nov 11, 2007
Messages
2
Reaction score
0
How can I write this program using printf and scanf???

I am taking a c++ course.I want to write this program:
capitalize first letter of words
for examlpe:jason go to school
Jason Go To School
all the first letters will be capital letter.
I see a program this forum but the teacher didn't teach touper,tolower statements so I want to to write this program using printf and scanf like this ok???
please help me immediately ??? I will have an exam on tuesday!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top