-2

I'm working on program where I would like to find a match in a string. I am using the strstr() function to get the job done, but it has a weird behaviour. I am using fgets() to read the string and then using strstr() to find a match, but it always returns null if the string is not the exact same as the match.

For example:

>string to search match in : This is testing environment
>match  : test
>returns : (null)

>string to search match in : test
>match  : test
>returns : test

Why does it behave this way and can I fix this behaviour?

abligh
  • 24,573
  • 4
  • 47
  • 84
Abdir
  • 87
  • 1
  • 1
  • 6
  • 3
    How are you calling `strstr`? Can we see some code? – Kevin Dec 06 '15 at 15:57
  • 2
    Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). You might also want to learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Dec 06 '15 at 15:59
  • 2
    You are aware that "strings" read via `fgets()` typically contain the trailing new-line used to enter them? – alk Dec 06 '15 at 16:01
  • 1
    Probably a duplicate to: http://stackoverflow.com/q/34048907/694576 – alk Dec 06 '15 at 16:03
  • 1
    You got two down-votes on this relatively quickly (neither from me). For future reference, if you write in proper English, put in appropriate punctuation etc. (which I have now fixed for you), your question may be better received and more easily understood by those from whom you are seeking help. – abligh Dec 06 '15 at 18:13
  • See the following for many alternatives [Removing trailing newline character from fgets() input](http://stackoverflow.com/q/2693776/2410359) – chux - Reinstate Monica Dec 06 '15 at 18:34

1 Answers1

1

Use the following approach

char *p = strchr( match, '\n' );
if ( p ) *p = '\0';

p = strstr( source, match );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    Or just do `match[strcspn(match, "\n\r")] = '\0';` to cut off any sort of trailing new-line sequence. – alk Dec 06 '15 at 16:15
  • Since the newline will always be at the end, and the usual case is that it will be there (the newline not being there is the exceptional case) why not use `strrchr`? Or simply check the last character? :) – Some programmer dude Dec 06 '15 at 16:39
  • @JoachimPileborg Why to search the last occurence of the new line character instead of to search the first occurence?:) – Vlad from Moscow Dec 06 '15 at 16:46
  • Because `fgets` will only place one newline in the string, and it will always be last if it's there. No need to search through the whole string when you know where it will be (if it's there). – Some programmer dude Dec 06 '15 at 16:54
  • @JoachimPileborg And what? How are you going to find the first occurence of the character using strrchr specifying the address of the string? – Vlad from Moscow Dec 06 '15 at 16:59
  • There is no difference between `strchr` and `strrchr` if there is at most 1 occurrence of the character. – Kevin Dec 06 '15 at 17:04
  • @Kevin Actually there is a difference. strchr is more efficient than strrcht.:) – Vlad from Moscow Dec 06 '15 at 17:35