1

I have a simple C program as follows:

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

int main()
{
  char a[100],b[100];
  char *ret;
  printf("Enter the string\n");
  scanf("%s",a);
  printf("Enter the substring to be searched\n");
  scanf("%s",b);
  ret= strstr(a,b);
  if(ret==NULL)
  {
    printf("Substring not found\n");
  }
  else
  {
    printf("Substring found \n");
  }
}

When I execute the following program, scanf to read the string into b is not waiting for me to enter the substring and the print statement that prints the substring not found is being printed on the console. I tried to give %sand tried in the scanf statement and removed \n from the printf statements and nothing changed the way it executed the program. It would be great if someone solves this simple problem. Thanks in advance.

user8540390
  • 143
  • 1
  • 10
  • 4
    Show your inputs, does the first contain a space? The `%s` format specifier stops at the first whitespace character. – Weather Vane May 05 '18 at 15:51
  • 1
    scanf() don’t accept the string with white space. – danglingpointer May 05 '18 at 15:51
  • I dont found any mistake in your code.It worked fine for me in gcc. try ` printf("\nEnter the string");` and `printf("\nEnter the substring to be searched");` .Use `\n` before. – anoopknr May 05 '18 at 15:52
  • 3
    You can find the answer here: https://stackoverflow.com/questions/6282198/reading-string-from-input-with-space-character – user3137124 May 05 '18 at 15:56
  • If i enter `hi how are you` as my main string input the problem exists and second scanf is not waiting for the input and atlast substring not found is being printed. Where as if i enter `heyyyyyyyyyyyy` as my main string input and the second scanf is waiting for me to enter the substring, and i entered `eyyy` and the result is substring found. – user8540390 May 05 '18 at 15:56
  • The first string `a` will receive `"hi"` and before you can type anything else the second string `b` will contain `"how"`, which is not a substring of `a`. – Weather Vane May 05 '18 at 16:09

2 Answers2

3

You can use scanf ("%[^\n]%*c", variable); with this scanf will read the whole line, instead of stopping when a space is reached.

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

int main()
{
  char a[100];
  char b[100];
  char *ret;
  printf("Enter the string\n");
  scanf ("%[^\n]%*c", a);

  printf("Enter the substring to be searched\n");
  scanf ("%[^\n]%*c", b);
  ret= strstr(a,b);
  if(ret==NULL)
  {
    printf("Substring not found\n");
  }
  else
  {
    printf("Substring found \n");
  }
}

Also you can use fgets

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

int main()
{
  char a[100];
  char b[100];
  char *ret;
  printf("Enter the string\n");
  fgets(a,100,stdin);//100 is the size of the string, you could use sizeof()

  printf("Enter the substring to be searched\n");
  fgets(b,100,stdin);//100 is the size of the string, you could use sizeof()
  ret= strstr(a,b);
  if(ret==NULL)
  {
    printf("Substring not found\n");
  }
  else
  {
    printf("Substring found \n");
  }
}
Chopi
  • 1,123
  • 1
  • 12
  • 23
2

try to use fgets instead of scanf, probably the reason is that the spaces are treated as delimiters, and the parts before the space are treated as a and the part right after the space will be treated as b. Therefore the programme did not prompt you for another input.

For your information: Reading string from input with space character?

chungonion
  • 125
  • 3
  • 8