2

In C Programming, how do I store user input in a variable so that I can get a substring from it? When typing in "hello Point" in the console I get an error: The substring is NULL. This means my word variable is empty? What exactly did I do wrong and why?

#include <stdio.h>
#include <string.h>


int main()
{
   char word[100];

   printf ("Enter a word: ");
   scanf ("%s", word);
   const char needle[] = "Point";
   char *ret;

   ret = strstr(word, needle);

   printf("The substring is: %s\n", ret);
   return(0);
}
Asperger
  • 3,064
  • 8
  • 52
  • 100
  • "What exactly did I do wrong?" You tried to print `ret` without checking that it isn't `NULL`, which is a valid (and likely) return value of `strstr`. – M Oehm Dec 15 '15 at 15:41

2 Answers2

3

%s for scanf() stops reading when it found a whitespace.

Try using scanf ("%99[^\n]", word); instead. (added 99 to avoid buffer overrun)

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Can you please explain how that part works? %99[^\n] – Asperger Dec 15 '15 at 15:43
  • 1
    `%[\n]` is "read characters until a newlline character (or EOF) is found". – MikeCAT Dec 15 '15 at 15:44
  • So is it some sort of regular expression? – Asperger Dec 15 '15 at 15:44
  • @Asperger : `99` is the limit of the number of characters copied to the buffer. `[^\n]` sets the input delimiter to rather than the default which is any whitespace character (space, tab, newline etc. ). At this point you might refer to the documentation for `scanf()`. In this case you might alternatively use `fgets()`. – Clifford Dec 15 '15 at 15:47
  • @Asperger : No, it is not as flexible as *regular expressions*, it just sets the delimiter. – Clifford Dec 15 '15 at 15:48
2

strstr returns NULL if the substring is not found. This is the case here. You are using scanf to read string. It will stop scanning after first occurrence of a white space, an ' ' here. Therefore, only hello will be stored in word and strstr(word, needle) will return NULL.

Use fgets instead to read string.

fgets(word, sizeof(word), stdin);
haccks
  • 104,019
  • 25
  • 176
  • 264
  • 2
    You shouldn't use magic numbers. I think `fgets(word, sizeof(word), stdin);` is better usage of `fgets()`. – MikeCAT Dec 15 '15 at 15:48
  • @haccks im between MikeCATs and your solution. What the advantage and disadvantages of both solutions? – Asperger Dec 16 '15 at 09:23
  • @Asperger; Using `fgets` over `scanf` to read string is always safe. For detailed explanation read [this](http://stackoverflow.com/a/3302594/2455888). – haccks Dec 16 '15 at 09:28
  • Where do I put my needle variables within fgets? word is the sentence but its not looking for anything in the sentence – Asperger Dec 16 '15 at 09:51
  • @Asperger; Just replace the statement `scanf ("%s", word);` with `fgets(word, sizeof(word), stdin);`. That's it. – haccks Dec 16 '15 at 09:53
  • Oh sorry, I replaced the wrong line. It works now. Why do so many people use scanf? Even my teacher – Asperger Dec 16 '15 at 09:54
  • In the beginning almost everyone starts with `scanf`. Now you learned about `fgets` so you should use it from now :) – haccks Dec 16 '15 at 10:06