Thread: How to detect null string?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    Question How to detect null string?

    How can I detect a null string?
    --if a user type nothing but simply a "Enter" key then a message is shown

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    How are you reading the input, what is the function you are using to read the input??? The basica idea would be to count the number of characters read, that should give you an idea of how many characters the user entered.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    6

    Smile

    How can I detect a null string?
    You can scan for null string using scanf("\n")
    However, if you wish to match specific enter key, you might want to consider using if statement. You have to declare a variable if you use if statement to match though

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >--if a user type nothing but simply a "Enter" key then a message is shown
    Since you should be using fgets for string data , simply test to see if fgets returns NULL for EOF. To check if the user simply typed return check the first character of the array for '\n', otherwise they gave you more than nothing.
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      char a[BUFSIZ];
    
      if ( fgets ( a, sizeof a, stdin ) == NULL )
        puts ( "Oops, no input read" );
      else if ( a[0] == '\n' )
        puts ( "Don't just hit enter, type something" );
      else
        printf ( "You typed \"%s\"\n", a );
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Depends on what you mean by a NULL string:
    Code:
    //The pointer is NULL, no string
    if(String == NULL)
    Code:
    //The string is empty
    if(strcmp(String, "") == 0)
    Code:
    //If the string begins with a NULL terminator, basically the same as #2.
    if(String[0] == '\0')
    Code:
    //If the string contains the letters NULL
    if(strcmp(String, "NULL") == 0)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    7
    Thank you all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. File Reading and storing to 1 variable
    By Rare177 in forum C Programming
    Replies: 34
    Last Post: 07-13-2004, 12:58 PM