Insanity or technicality?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shark

    #1

    Insanity or technicality?

    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)tol ower(c); //tried without cast too
    }
    return temp;
    }

    With a void ToLower, I tried ToLower(string_ to_convert) but
    string_to_conve rt remains the same as before ToLower.

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

  • Sumit Rajan

    #2
    Re: Insanity or technicality?


    "Shark" <cpp_shark@yaho o.com> wrote in message
    news:1134376628 .852775.84270@o 13g2000cwo.goog legroups.com...[color=blue]
    > 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++)[/color]

    i<<upper.size() ?

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

    Regards,
    Sumit.
    --
    Sumit Rajan <sumitr@msdc.hc ltech.com>


    Comment

    • Shark

      #3
      Re: Insanity or technicality?

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

      Comment

      • Sumit Rajan

        #4
        Re: Insanity or technicality?


        "Shark" <cpp_shark@yaho o.com> wrote in message
        news:1134377339 .649264.270390@ g49g2000cwa.goo glegroups.com.. .[color=blue]
        > Yea i meant i<upper.size() ;[/color]

        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 <sumitr@msdc.hc ltech.com>


        Comment

        • Howard

          #5
          Re: Insanity or technicality?


          "Shark" <cpp_shark@yaho o.com> wrote in message
          news:1134376628 .852775.84270@o 13g2000cwo.goog legroups.com...[color=blue]
          > 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)tol ower(c); //tried without cast too
          > }
          > return temp;
          > }
          >
          > With a void ToLower, I tried ToLower(string_ to_convert) but
          > string_to_conve rt remains the same as before ToLower.
          >[/color]

          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_conv ert =
          ToLower(string_ to_convert);". (And in that case, change the parameter to
          const, since you're not changing it.)
          [color=blue]
          > With other variations, the returned type is an empty string. Am I
          > insane?
          >[/color]

          Only your doctor can tell for sure. :-)

          -Howard



          Comment

          Working...