Thread: Newbie - tolower()

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    36

    Newbie - tolower()

    I'm having problems with using the "tolower".

    I made a very small program to test:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    int main()
    {
    
    char ch;
    
    printf("Enter A Letter\n");
    scanf("%c", ch);
    ch = tolower(ch);
    printf("%c", ch);
    return 0;
    }

    This is what happens when I run the program:
    The last line is the error. What am I doing wrong!?

    Enter A Letter
    G
    Segmentation fault

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Use
    scanf("%c", &ch);
    But because tolower and toupper functions take and return int, I suggest you to use
    Code:
    int ch;
    ...
    ch = getchar();
    ch = tolower(ch);
    ...
    Last edited by Micko; 04-13-2005 at 12:22 PM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually that's not it at all. The real problem is that your scanf call is wrong:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    int main()
    {
    
    char ch;
    
    printf("Enter A Letter\n");
    scanf("%c", ch);
    ch = tolower(ch);
    printf("%c", ch);
    return 0;
    }
    You forgot the address-of operator:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    int main()
    {
    
        char ch;
    
        printf("Enter A Letter\n");
        scanf("%c", &ch );
        ch = tolower(ch);
        printf("%c", ch);
        return 0;
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by quzah
    Actually that's not it at all
    Really?
    What about second line in my post?
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  5. #5
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    What about second line in my post?
    I think he was talking about 98dodgeneondohc's post, not yours

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    ok thanks for the help people! I'm just trying to teach myself programming. I always thought the "&" was only to be used with integers. What is the "&" for and why is it needed?

    Another thing... if I was to enter in a sentence, and I make an array for example: char sentence[50];

    Upon running the program, I enter in the sentence seperated by a spacebar, it would only show the first word! How do I make it so it takes the whole sentence?
    Last edited by 98dodgeneondohc; 04-14-2005 at 07:03 AM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    ok '&' is the one which give u the address, think of something like this

    Code:
    int i;
    
    when u say                                                                                                          _________
    &i --> it gives you the address of 'i' i.e  where the 'i' is located in the memory  |                 |
                                                                                                                                ---------------
                                                                                                                    1000            i
    
    so when u say &i -->  it gives you the address of 'i ' in this case it is 1000
    scanf the syantx is as follow
    Code:
    scanf("const char format", ... varibale);
    scanf function always requires address of the variable in order to place the value entered by the user. u are expected to specilfy '&' in front of the variable. in your case

    Code:
    scanf("%c", ch);
    the scanf is expecting the address of the variable ch to place u'r char eneted in the memory

    >Upon running the program, I enter in the sentence seperated by a spacebar, it would only show the first word! How do I make it so it takes the whole sentence?
    that is b'cose the scanf() fucntion reads the string up to the white space the rest is lost.

    the better way of doing this is to use fgets function to read the string. and this syntax is as follow
    [code]
    fgets(char *, int, FILE *);
    [code]

    example:
    Code:
    #include<stdio.h>
    
    int main()
    {
            char str[20];
            
           fgets(str, sizeof str, stdin);  // read the string
           printf("%s",str);       //  print the string on to the  screen
    
           getchar();
    }
    
    /* output: -
    hello world
    hello world
    */
    hope this helps

    s.s.harish

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    good explanation! thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tolower and locale
    By MK27 in forum C Programming
    Replies: 16
    Last Post: 02-03-2009, 09:05 PM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 07:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM