C++ string to lowercase method?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bjwillykajilly
    New Member
    • Nov 2006
    • 12

    C++ string to lowercase method?

    hello, what i'm wanting to do is take a month input(january february, etc) turn it to a 3 char string(jan, feb, mar) which i have, and then turn them to all lowercase, so as i don't haev to have 8 if statements(Jan, JAn, JaN, JAN, jAN, jaN, jAn, jan) to set up(therefore makeing 84 extra ifstatements, 12 months)

    I've tried all sorts of variations, to no avail.(this is my first class in c++, 3rd in comp programing, so very new)

    so please make the explanation fairly dumby proof if you can, if not any help is greatly appreciated. thank you.

    the variable name is package

    string month;
    ...(extra variables and shit)
    getline(cin, month);
    ...(extra input from user)
    month = month.substr(0, 3);

    or, another solution to this would be to use something like java's string.equalsIg noreCase()

    ie: if(month.equals IgnoreCase("jan "))
    {
    ...
    }
  • dtimes6
    New Member
    • Oct 2006
    • 73

    #2

    Comment

    • bjwillykajilly
      New Member
      • Nov 2006
      • 12

      #3
      here's the problem, as i've said before, i'm new to c++, in my opinion its 10 times worse than java, its so much harder to do anything, like int his case lowercase methods, ignorecase methods, many other comparison methods and such, but heres my point, this is a quote from another discussion.

      Vadim wrote:[color=blue]
      > Hi!
      >
      > I have a STL string, which I need to convert to low case.
      > In VC 6 I used:
      > string sInputBuffer = _strlwr((char*) sBuffer.c_str() );[/color]

      Try this maybe (std C++ - don't know about MVC):

      std::transform( sBuffer.begin() , sBuffer.end(), sBuffer.begin() ,
      ::tolower );

      Cheers,
      Andre
      what's sBuffer.begin, where do i put in the string that i wan't to modify, its not a char array, its a string that i'm modifying. please, if you offer help, tell me how its supposed to help me, that whole discussion turns into a bunch of ppl arguing like schoolgirls. I need an example code to look at, not a generic one where i'm supposed to know what all that shit means. like string.substr(i nt, int);
      granted, that is an easy one to dicipher, please say for example:

      string s1 = "abc de fgh";
      s1 = s1.substr(0, 5); //strings start at 0, not 1

      thanks for help.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Try this:

        Code:
        #include <cctype>
        
        // cctype.h includes the method tolower(int), which will return the
        // lowercase number value of the characters (represented by int).
        // For example, if 'A' is 36 and 'a' is 65 (Random numbers, do not
        // use these!), then tolower('A') will return 65.
        
        char ch = static_cast<char>(tolower(otherCh));

        Comment

        • bjwillykajilly
          New Member
          • Nov 2006
          • 12

          #5
          ok, well this is a string that i'm working with, not individual char's, i don't know how to seperate strings into individual char's, and i tried imputing month.substr(0, 1) into it and it wouldn't let me. is there no way to manipulate strings like you can in java? thanks for the help

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by bjwillykajilly
            ok, well this is a string that i'm working with, not individual char's, i don't know how to seperate strings into individual char's, and i tried imputing month.substr(0, 1) into it and it wouldn't let me. is there no way to manipulate strings like you can in java? thanks for the help
            String manipulation in C/C++ is more basic. Strings are defined as arrays of characters, so logically, it is arrayOfChars[0-> ( 1 + #of_letters_in_ string)] because strings are terminated by having a '\0' character at the end of the array. A string can be identified by the memory location (pointer) to the first element in the array (arrayOfChars[0]). The compiler will take that first character, and continue until it reaches the '\0' element.

            This should give you a good overview of strings

            ANSI C++ GNU String class tutorial and examples. YoLinux: Linux Information Portal includes informative tutorials and links to many Linux sites.

            Comment

            • jerger
              New Member
              • May 2007
              • 77

              #7
              ganon if i have a string "In" how would i apply that code then? after cin>> In;

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                hello, what i'm wanting to do is take a month input(january february, etc) turn it to a 3 char string(jan, feb, mar) which i have, and then turn them to all lowercase, so as i don't haev to have 8 if statements(Jan, JAn, JaN, JAN, jAN, jaN, jAn, jan) to set up(therefore makeing 84 extra ifstatements, 12 months)
                This is the original problem.

                A string object has individual characters. If the object name is str, then the first character is str[0]. the second is str[1], etc. You call the size() method to get the number of characters in the string.

                Next, conversion to lowercase can use a function called tolower. It converts uppercase to lower case and does nothing to characters that aren't uppercase.

                So, write a loop:
                [code=cpp]
                string str("JAN");
                int length = str.size();
                for (int i = 0; i < length; ++i)
                {
                str[i] = tolower(str[i]);
                }
                [/code]
                There are other more C++-like ways to do this but this is OK for starters.

                Comment

                • jerger
                  New Member
                  • May 2007
                  • 77

                  #9
                  err... the problem now is that the type of input i have is a char [999], so i can't run alot of common solutions... because i cannot convert const char to char? some error like that. !!!

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Where does this occur?
                    Originally posted by jerger
                    so i can't run alot of common solutions... because i cannot convert const char to char? some error like that. !!!

                    Comment

                    • jerger
                      New Member
                      • May 2007
                      • 77

                      #11
                      i don't have permission to post the code so i can post parts...

                      char In[999];
                      CString in;

                      cin >> In;
                      in = In;

                      then in is sent through the dictionary. it gets it from the input In.... (in does)

                      so... if i do in = "love", it will always search love... therefore in depends on In, for what to input.

                      i never used "cstring" before, so maybe there is a way i can simply check the last letter or each input as it comes from In, for ! or commas and if found, remove them... if not found in = In?

                      related thread:



                      update: i tried this, i get some errors that are similar from different functions i tried on my own.


                      length = in.size();

                      for (int j = 0; i < length; ++j)
                      {
                      in[j] = tolower(in[j]);

                      }
                      error:
                      Translator.cpp( 142) : error C2106: '=' : left operand must be l-value

                      Comment

                      • weaknessforcats
                        Recognized Expert Expert
                        • Mar 2007
                        • 9214

                        #12
                        Actually, CString is not a C++ string. It is a Microsoft string class that escaped from MFC and found it's way in various pieces of code.

                        The C++ string class is basic_string. This is a template, which if specialized with a char becomes basic_string<ch ar>. This has been typedef'd to string.
                        Now you can use string as a type.

                        By changing your CString to a string, your code compiles.

                        Comment

                        • jerger
                          New Member
                          • May 2007
                          • 77

                          #13
                          since in is cstring... i had to store another cstring as a temp

                          so i have cstring temp

                          so

                          temp = In // In is my real input complex [999]

                          in = temp.MakeLower( );

                          then it works! check out my other threads if you need help with cstrings and removing punctuation

                          Comment

                          • weaknessforcats
                            Recognized Expert Expert
                            • Mar 2007
                            • 9214

                            #14
                            This:

                            Originally posted by jerger
                            in = temp.MakeLower( );
                            is not the same as this:
                            Originally posted by jerger
                            in[j] = tolower(in[j]);
                            In the first case you are using CString::operat or=(). In the second case you are using the C++ operator= that assigns one char to another.

                            My guess is that CString::operat or[] returns a const char& making it impossible to assign to it.

                            Just check your CString class.

                            Comment

                            • jerger
                              New Member
                              • May 2007
                              • 77

                              #15
                              oh, umm... well this works now hehe. so thats good, the last post is what i had to do to make it work...

                              but your probably right too..

                              i just need to find a way to not crash now if the user types ............... .....

                              if there is more then one symbol in a row of the same type it crashes, which bewilders me. i posted my code so please mods dont get mad if i post it again, so i was thinking maybe i should make this a case statement?

                              //punctuation removal
                              //while loop checks several times to catch all punctuation

                              while (punk < 10){
                              len = strlen(in);
                              if (in.Left(1) == '"')
                              in = in.TrimLeft('"' );

                              if (in[len-1] == '.')
                              in = in.TrimRight('. ');

                              if (in[len-1] == '"')
                              in = in.TrimRight('" ');

                              if (in[len-1] == '!')
                              in = in.TrimRight('! ');

                              if (in[len-1] == ',')
                              in = in.TrimRight(', ');

                              if (in[len-1] == '?')
                              in = in.TrimRight('? ');

                              if (in[len-1] == ':')
                              in = in.TrimRight(': ');

                              if (in[len-1] == ';')
                              in = in.TrimRight('; ');

                              punk++;
                              }
                              punk = 0;
                              //end of punctuation check

                              Comment

                              Working...