0

Q. Enter and store n number of strings and then search for a particular string.

I am able to enter the 'Number of strings' and then I am also able to enter the strings. But then the strings don't get printed. Neither the comparison and searching takes place, and it shows a segmentation error. This happens when I use gets() for the input of strings. But when I use scanf() for strings input, the program terminates right after I enter 'Number of strings'. Why so?

Here's the code:

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

int main()
{
  char str[200],names[100];
  int n,i;
  int flag=0;

  printf("Give number of strings:\n");
  scanf("%d",&n);

  printf("Enter the strings:\n");
  for(i=0;i<n;i++)
  {
    gets(names);  
  }

  printf("Entered Values are:\n");
  for(i=0;i<n;i++)
  {
    printf("%s\t",names[i]);
  }

  printf("Enter the string you want to search for:\n");
  scanf("%s",str);
  for(i=0;i<n;i++)
  {
    if(strcmp(names[i],str) == 0)
       {
      flag=1;
      break;
       }
  }
  if(flag==1)
    printf("Yes this string %s exists!\n",str);
  else
    printf("No it does not exists");


  return 0;
}
  • This code should give you all kinds of type mismatch warnings, espcially about using `char` where it expected `char*`. Fix those first. Hint: `names[100]` is **one** string, not an array of strings. – Blaze Feb 10 '20 at 14:40
  • 2
    Blaze suggests that you turn warnings of your compiler on (and listen to the errors it already reports). – Paul Ogilvie Feb 10 '20 at 14:44
  • 2
    What they said, plus never ever use `gets`. And don't use `conio.h` either. – klutt Feb 10 '20 at 14:45
  • 1
    How would `gets(names);` inside the for-loop put the strings read into the entries of the names array (assuming it is an array of strings)? – Paul Ogilvie Feb 10 '20 at 14:46
  • 1
    Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). Also note that `gets` is obsolete, and that you should not mix different types of input function. – Weather Vane Feb 10 '20 at 14:46
  • @klutt do you rule out termios too? I agree though that `conio.h` is unnecessary here. – Weather Vane Feb 10 '20 at 14:47
  • @WeatherVane Have never used it. Why do you wonder? – klutt Feb 10 '20 at 14:49
  • @klutt I was only wondering if you are in the "MS extensions bad, gcc extensions good" group. – Weather Vane Feb 10 '20 at 14:49
  • @WeatherVane I think an fully accurate answer would be too long to take here in this comment section. In short, there are benefits with using as few extensions as possible, but completely ruling them out is not a good thing. In most situations I would be more willing to accept gcc extentions, but the reasons are more solid than just a plain "M$ is eeeeevil" – klutt Feb 10 '20 at 14:57
  • @klutt that's reasonable. I actually think the MS's OS-specific `conio.h` is very useful when dealing specifically with the console (not a redirectable stream). I have always been baffled by the C standard library's glaring lack of "input one character" and the thousands of questions that result. – Weather Vane Feb 10 '20 at 15:04
  • @WeatherVane Correct me if I'm wrong, but I have the impression that very little real development for Windows is done in C today. Most C coding in Windows is just for the sake of learning C. If you ever end up coding C in a professional way, chances are pretty high that it will be in a *nix environment. – klutt Feb 10 '20 at 15:05
  • @klutt I think you are right, but either way the extensions are non-standard. And there are other extensions by MS I really do not like. – Weather Vane Feb 10 '20 at 15:06
  • @WeatherVane The file `windows.h` is also an extension, but I would not complain about that for obvious reasons. If you want to code a windows program in C, you simply need that. However, I do agree that user IO in standard C is very clunky. – klutt Feb 10 '20 at 15:12

1 Answers1

0
  1. gets function is dangerous avoid it.
  2. You need to understand how strings are stored in the memory.
char c[] = "Hello";` //is same as 
char c[5];
c[0] = 'H';
c[1] = 'e';
c[2] = 'l';
c[3] = 'l';
c[4] = '0';
c[5] = '/0';

In your snippet instead of allocating different arrays for storing input strings you are overwriting same array again and again.

In order to store n strings you need to allocate n character arrays. VLA's can be used to allocate 2D array like char names[n][100] of nX100 size. names[0...n] represents row in 2D matrix and each row can store a strings of length 100 bytes.

Update your code as below snippet,

char names[n][100];
printf("Enter the strings:\n");
for(i=0;i<n;i++)
{
   scanf("%99s",names[i]); //Avoid buffer overflow and mention max capacity
}

You can find complete code here

TruthSeeker
  • 1,539
  • 11
  • 24