Thread: Need help with strstr() and 2-d characters.

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    1

    Need help with strstr() and 2-d characters.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void findtrack(char ip[4][80] , char substring[]) 
    {
        int i;
        
        for (i=0 ; i<4 ; i++) 
         {    
        if (strstr(ip[i] , substring  ) ) 
        
         printf("Matching track is:%d full name:%s\n" ,i+1 ,  ip[i]);
        
        }
    }
    
    int main() {
    
    char tracks[4][80];
    
    char find[40];
    
    printf("enter first track:");
    fgets(tracks[0] , 80 , stdin);
    
    printf("enter second track:");
    fgets(tracks[1] , 80 , stdin);
    
    printf("enter third track:");
    fgets(tracks[2] , 80 , stdin);
    
    printf("enter fourth track:");
    fgets(tracks[3] , 80 , stdin);
    
    printf("enter a string to find track:");
    fgets(find , 40 , stdin);
    
    findtrack(tracks , find);
    }
    Test Case1 -
    Input-

    enter first track:hello
    enter second track:animal
    enter third trackayphone
    enter fourth track:magic
    enter a string to find track:ma

    Output-Nothing (didn't find "ma" in fourth track )

    Test Case2- Input-

    enter first track:hello
    enter second track:animals
    enter third track:case
    enter fourth track:sadf
    enter a string to find track:ls

    Output- Matching track is:2 full name:animals


    Can you please tell me why there is a difference in output even though the string to be found is present in one of the tracks in both the cases?

    When the string to be found contains the last alphabet of one of the tracks , the code works , otherwise it does not .

    Please guide me

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    fgets leaves the newline character ("\n") in the buffer when the line is read, so right now when you look for "ma" inside the string "magic", you are actually looking for "ma\n" inside the string "magic\n".

    To deal with this I like to use a function called "chomp" which removes the trailing newline after calling fgets. Example:

    Code:
    char buf[100];
    fgets(buf, sizeof buf, stdin);
    chomp(buf);
    printf("You said: \"%s\".\n", buf);
    Try this without 'chomp' (comment it out), type Hello and notice the output will look like this (the quotes make the trailing newline noticeable):

    Code:
    You said: "Hello
    ".
    Use chomp after fgets and the output will be nicer. Here is one way to implement chomp. If there is not a trailing newline or if the string is too short to have a newline, it does nothing:

    Code:
    #include <stdlib.h>
    #include <string.h>
    
    void chomp(char line[])
    {
        size_t n = strlen(line);
        if (n == 0) return;
        if (line[n-1] == '\n')
            line[n-1] = '\0';
    }

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    fgets leaves the newline character ("\n") in the buffer when the line is read
    Don't you mean, fgets() also extracts the newline character ('\n') from the stream and places it in the string before appending the end of string character ('\0')?


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strstr
    By doubty in forum C Programming
    Replies: 1
    Last Post: 08-28-2009, 04:39 AM
  2. strstr and utf-8
    By MK27 in forum C Programming
    Replies: 2
    Last Post: 02-01-2009, 10:54 PM
  3. strstr and '\0'
    By dNNNY in forum C Programming
    Replies: 14
    Last Post: 10-24-2008, 08:59 PM
  4. pch=strstr?
    By reRanger in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2004, 01:33 PM
  5. strstr help..
    By c2turbo in forum C Programming
    Replies: 7
    Last Post: 09-27-2003, 05:13 PM

Tags for this Thread