Insanity or technicality?

S

Shark

Hi, I am trying to do the following:

string ToLower(string& upper) //tried variations of return type,
including void, to modify upper itself
{
string temp="";
for(int i=0; i<<upper.size(); i++)
{
char c = upper;
temp+=(char)tolower(c); //tried without cast too
}
return temp;
}

With a void ToLower, I tried ToLower(string_to_convert) but
string_to_convert remains the same as before ToLower.

With other variations, the returned type is an empty string. Am I
insane?
 
S

Sumit Rajan

Shark said:
Hi, I am trying to do the following:

string ToLower(string& upper) //tried variations of return type,
including void, to modify upper itself
{
string temp="";
for(int i=0; i<<upper.size(); i++)

i<<upper.size()?

I think you mean "i<upper.size()"!

Regards,
Sumit.
 
S

Sumit Rajan

Shark said:
Yea i meant i<upper.size();

Since you are not changing the parameter upper, you may like to make a const
reference. This will also enable to use the function in the following
context:

std::cout << ToLower("CAPS") << std::endl;


Another option could be to define ToLower as:

//remember to include <algorithm>, <cctype>, <string>
void ToLower(string& upper)

{

transform(upper.begin(), upper.end(), upper.begin(), tolower);

}


Regards,
Sumit.
 
H

Howard

Shark said:
Hi, I am trying to do the following:

string ToLower(string& upper) //tried variations of return type,
including void, to modify upper itself
{
string temp="";
for(int i=0; i<<upper.size(); i++)
{
char c = upper;
temp+=(char)tolower(c); //tried without cast too
}
return temp;
}

With a void ToLower, I tried ToLower(string_to_convert) but
string_to_convert remains the same as before ToLower.


Aside from your mistake using << instead of <, you're getting expected
behavior. After all, you're not modifying the "upper" parameter at all in
that function, so why should it change? The _return_ value is where you
should see the result, not the string you passed as the parameter. If you
want to modify what you're passing, either operate on it directly (using
"upper" instead of "temp" in the function), or else assign the result back
to the string that you passed (something like "string_to_convert =
ToLower(string_to_convert);". (And in that case, change the parameter to
const, since you're not changing it.)
With other variations, the returned type is an empty string. Am I
insane?

Only your doctor can tell for sure. :)

-Howard
 

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

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top