By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Insanity or technicality?

P: n/a
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[i];
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?

Dec 12 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Shark" <cp*******@yahoo.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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.
--
Sumit Rajan <su****@msdc.hcltech.com>
Dec 12 '05 #2

P: n/a
Yea i meant i<upper.size();
Ok, i was insane. Thanks :)

Dec 12 '05 #3

P: n/a

"Shark" <cp*******@yahoo.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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.
--
Sumit Rajan <su****@msdc.hcltech.com>
Dec 12 '05 #4

P: n/a

"Shark" <cp*******@yahoo.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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[i];
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

Dec 12 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.