Thread: string function

    #1
  1. Normal Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Malaysia
    Posts
    14
    Rep Power
    0

    string function


    who can teach me wat is the function for accept a string with igone case ?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    68
    Rep Power
    11
    Huh? Can you be more specific?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    55
    Rep Power
    11
    He might mean he wants to convert a string which contains capitals to lower case, which is sort of ignoring case specified.

    Here is a basic example for ASCII to convert higher case letters to lower case...

    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
      char str[] = "HihihihiHihihiHiHHH", *ptr;
     
      for(ptr = str; ptr < str+sizeof(str); ptr++)
      {
        if(*ptr >= 'A' && *ptr <= 'Z')
        {
          *ptr += 32;
        }
      }
      
      printf("%s\n", str);
      
      return 0;
    }
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    ctype.h has a function to convert characters to lower case. You can use it to write a function to convert a whole string.

    Code:
    #include <ctype.h>
    
    char* strtolower( char* string )
    {
        int i = 0 ;
        while( string[i] != '\0' )
        {
            string[i] = tolower( string[i] ) ;
        }
    
        return( string ) ;
    }
    The string is converted in place, the returned pointer is for convenience in experssions such as:

    Code:
    if( strcmp( strtolower( input_str ), "quit" ) == 0 ) ...
    Although I do not recomment that coding style!:eek:

    Clifford
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Second thought. Most compiler libraries have a case-insensitive version of strcmp(). It is not standard, so its name varies from compiler to compiler.

    stricmp()
    strcmpi()

    are common names for such a function.

    Clifford
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    55
    Rep Power
    11
    strcasecmp() is also available in strings.h on my box.
    I just included the code to convert case manually so he fully understands it, not that it's complex in any way. I think people should fully understand every function they use.
  12. #7
  13. Normal Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Malaysia
    Posts
    14
    Rep Power
    0
    oh sorry i forgot i need the function is in c++ not in c
  14. #8
  15. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    I assume therefore that you are using the C++ string class rather than C style strings? If not then it does not matter since C is a subset of C++.

    Example from http://www.msoe.edu/eecs/cese/resources/stl/string.htm

    Code:
    string str22 = "This IS a MiXed CaSE stRINg";
    transform (str22.begin(),str22.end(), str22.begin(), tolower);
    cout << "[" << str22 << "]" << endl; // [this is a mixed case string]
    Note that it uses the C tolower function as a pointer-to-function parameter in the transform() function.

    transform() is in <algorithm>. You'll need to include <algorithm>, <string>, and <cctype> (or <ctype.h>).

    Example function(untested, fix it yourself if broken!)

    Code:
    #include <algorithm> ;
    #include <string> ;
    #include <cctype> ;
    
    string& string_to_lower( string& convert )
    {
        transform (convert.begin(), convert.end(), convert.begin(), tolower ) ;
    
        return( convert ) ;
    }
    Clifford

IMN logo majestic logo threadwatch logo seochat tools logo