Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Function to capitalize string

Reply
Thread Tools

Function to capitalize string

 
 
Rick
Guest
Posts: n/a
 
      11-16-2003
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;
}

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      11-16-2003
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.

 
Reply With Quote
 
 
 
 
Rick
Guest
Posts: n/a
 
      11-16-2003
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.

 
Reply With Quote
 
Moonlit
Guest
Posts: n/a
 
      11-16-2003

"Rick" <(E-Mail Removed)> 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.



 
Reply With Quote
 
Thomas Wintschel
Guest
Posts: n/a
 
      11-16-2003

"Rick" <(E-Mail Removed)> 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


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      11-16-2003
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.

 
Reply With Quote
 
sunshine82 sunshine82 is offline
Junior Member
Join Date: Nov 2007
Posts: 2
 
      11-11-2007
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!!!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to capitalize first word of a string? Rails List Ruby 4 06-05-2009 05:37 PM
String#capitalize more complex IƱaki Baz Castillo Ruby 5 04-02-2009 06:29 PM
Re: string capitalize sentence Skip Montanaro Python 0 06-23-2005 11:07 PM
Re: string capitalize sentence Konstantin Veretennicov Python 0 06-23-2005 11:07 PM
Regex to capitalize first letter of string jay Java 1 02-25-2004 08:41 AM



Advertisments