Thread: Different scanf and strlen?

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    13

    Post Different scanf and strlen?

    Below is my code for this algorithm:


    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
        {
            int i, n, totalA; 
            char studName[31]; // limit the name to 30 characters
    
            // Gets the Student Name into variable studName
    
                        printf("Please Input Student Name : ");
            gets(studName);
    
            /*
            scanf("%s",studName) won't give the correct strlen if a user input such
            as "Ahmad Albab" 
            */ 
    
     
            // find the length of the student's name
                        i= strlen(studName);
     
    
            // Start processing the words
                        totalA=0;
    
    
            for (n = 0; n <= i; n+=1)
                {
                    if (studName[n]=='A' || studName[n]=='a')
                    {
                                            totalA++; 
                    }
                }
                        printf("\nTotal number of A or a in the student name is %d",totalA);
                        getch();
    
        }
    The output :

    Please Input Student Name : Ahmad Albab

    Total number of A or a in the student name is 4



    Anybody please explain why if i use scanf, strlen for names with space in the middle is not returning the correct value.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It's because scanf with %s uses SPACE as a delimiter.

    You should definitely do all you can to forget you ever knew about the gets() function. Not that scanf("%s") is any safer by itself.

    Typically, use
    Code:
    char buff[BUFSIZ];
    fgets( buff, sizeof(buff), stdin );
    to read ALL input, then process what you need out of buff, rather than attempting to read it from the input directly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    13
    so, my coding should be like this:

    Code:
    char studName[BUFSIZ];
    fgets(studName,sizeof(studName), stdin);
    am i right..

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    No, you fgets() into buff, then check buff for length, and then copy at most 30 characters into studName
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by adnilsah017 View Post
    Anybody please explain why if i use scanf, strlen for names with space in the middle is not returning the correct value.
    If you print the student name before parsing it, you'll SEE why it happens...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another strlen
    By voidX in forum C Programming
    Replies: 15
    Last Post: 01-09-2011, 11:24 PM
  2. Strlen(...)
    By Korhedron in forum C++ Programming
    Replies: 6
    Last Post: 06-10-2003, 03:02 PM
  3. Strlen
    By NavyBlue in forum C Programming
    Replies: 7
    Last Post: 11-11-2002, 06:34 AM
  4. strlen
    By XSquared in forum C++ Programming
    Replies: 8
    Last Post: 07-16-2002, 07:05 PM
  5. Why O why, strlen?
    By Sebastiani in forum C Programming
    Replies: 11
    Last Post: 08-24-2001, 01:41 PM