Tolower function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • beck2
    New Member
    • Jan 2007
    • 2

    #1

    Tolower function

    Hello everyone its Angela again. Can anyone help me please?
    i want to convert uppercase characters to lower case using the <stdlib.h>
    and the function tolower. for example i want to display "HELLO" to lower.

    my code is:

    #include<iostre am.h>
    #include<stdlib .h>

    void main() {

    cout<<tolower(" HELLO");


    }

    i get the error : implicit declaration of function int to lower(.....)


    what am i doing wrong?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Not including ctype.h

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      As indicated by Banfa you need to include the appropriate header but also the function tolower() takes a single character (Iint) as a parameter and returns the corresponding lowercase letter (int), i.e.
      Code:
           int tolower(int c);
      so you have to have a loop changing each character in turn, e.g.
      Code:
      #include<iostream>
      #include<cctype>        // for tolower()
      using namespace std;
      
      int main() {
          char upper[]="HELLO", lower[10]={0};
          int i;
          for(i=0; i<strlen(upper); i++)
             lower[i] = tolower(upper[i]);  // convert each character
          cout<< lower << endl;
          cin.get();
      }
      if you are using a string you can use the transform() function


      e.g.
      Code:
      #include<iostream>
      #include<cctype>        // for tolower()
      #include <algorithm>
      using namespace std;
      
      int main() {
          string s("HELLO");
          transform(s.begin(), s.end(), s.begin(), (int(*)(int))std::tolower);
          cout<< s << endl;
          cin.get();
      }

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Originally posted by horace1
        As indicated by Banfa you need to include the appropriate header but also the function tolower() takes a single character (Iint) as a parameter and returns the corresponding lowercase letter (int), i.e.
        Code:
             int tolower(int c);
        so you have to have a loop changing each character in turn, e.g.
        Code:
        #include<iostream>
        #include<cctype>        // for tolower()
        using namespace std;
        
        int main() {
            char upper[]="HELLO", lower[10]={0};
            int i;
            for(i=0; i<strlen(upper); i++)
               lower[i] = tolower(upper[i]);  // convert each character
            cout<< lower << endl;
            cin.get();
        }
        I am fairly certain that you will have to use a static_cast statement when assigning values to lower. Since tolower(int) returns an integer, it must be cast to a character value. The statement

        Code:
        lower[i] = tolower(upper[i]);
        should be changed to

        Code:
        lower[i] = static_cast<char>(tolower(upper[i]));

        Comment

        Working...