Thread: Trouble with fgets() and strstr()

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    1

    Trouble with fgets() and strstr()

    Hi everyone.
    I'm working with string functions, but I have a trouble with strstr(). Here is the code that works:

    Code:
    char text[] = "one two three";
      char substring[] = "two";
    
      if(strstr(text, substring))
        puts("The string sought was found");
      else
        puts("The string sought was not found");

    This is the code that doesn't work:

    Code:
    #define TEXT_LEN 100
    #define SUBSTRING_LEN 40
    
      char text[TEXT_LEN];
      char substring[SUBSTRING_LEN];
    
      printf("Enter the text to be searched:\n");
      fgets(text, TEXT_LEN, stdin);
    
      printf("Enter the string sought:\n");
      fgets(substring, SUBSTRING_LEN, stdin);
    
      if(strstr(text, substring))
        puts("The string sought was found");
      else
        puts("The string sought was not found");
    Why strstr() returns NULL even when substring is in text?

    I'am working on a GNU/linux system.
    Thanks.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Don't forget that fgets() retrieves the end of line character and places it in the string, you need to take this into account.

    Jim

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    "fgets()" also stores the newline, if there is room. So entering your original input would yield the following strings:

    "one two three\n"
    "two\n"

    So "two\n" is not a substring of the first. See here for an example of reading input from stdin with "fgets()" and removing the newline, if present: FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com

  4. #4
    Registered User jain.rani's Avatar
    Join Date
    Jul 2009
    Posts
    3
    #define TEXT_LEN 100
    #define SUBSTRING_LEN 40

    char text[TEXT_LEN];
    char substring[SUBSTRING_LEN];

    printf("Enter the text to be searched:\n");
    fgets(text, TEXT_LEN, stdin);

    printf("Enter the string sought:\n");
    fgets(substring, SUBSTRING_LEN, stdin);

    if(substring[strlen(substring)-1] =='\n')
    substring[strlen(substring)-1] = '\0';


    if(strstr(text, substring))
    puts("The string sought was found");
    else
    puts("The string sought was not found");



    Neetu

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Here is a neat way of doing the same thing as jain.rani suggested:
    Code:
    printf("Enter the string sought:\n");
    fgets(substring, SUBSTRING_LEN, stdin);
    
    substring[strcspn(substring, "\n")] = '\0';
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble With FILE I/O with fgets
    By loserone+_+ in forum C Programming
    Replies: 6
    Last Post: 08-12-2013, 07:07 AM
  2. trouble with strstr
    By vash in forum C Programming
    Replies: 4
    Last Post: 12-05-2012, 10:53 AM
  3. I'm really trouble with fgets i think
    By dayanike in forum C Programming
    Replies: 2
    Last Post: 03-25-2012, 11:38 AM
  4. fgets trouble
    By sababa.sababa in forum C Programming
    Replies: 2
    Last Post: 11-13-2009, 11:57 AM
  5. trouble with fgets() !
    By davewang in forum C Programming
    Replies: 13
    Last Post: 11-25-2008, 03:13 PM

Tags for this Thread