0
 #include <stdio.h>
 #include<cs50.h>
 #include <string.h>
 char array[][80] = {"CS50", "C Programming", "Java", "Python", "PHP",};
 void find(char search[])
 {
     for (int i = 0; i < 5; i++) 
     {
         if (strstr(array[i], search))
         {
         printf("Track %i: '%s'\n", i, array[i]);
         }
     }
 }
 int main()
 {
     char searchfor[80];
     puts("Search for:");
     fgets(searchfor,80,stdin);
     find(searchfor);
     return 0;
 }
1

Are you sure the problem is with strstr? Did you use debug50 or gdb to make sure everything is as you expect? Did you read the man page on fgets? Of particular interest:

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.

12
  • Yes, I have read reference50 and also used valgrind to check memory leaks. – qwerty Dec 6 '16 at 16:47
  • I have also tried using pointers in this program but it's not working. – qwerty Dec 6 '16 at 16:51
  • If a newline is read, it is stored into the buffer. use debug50 or gdb to see what your "needle" is. – DinoCoderSaurus Dec 6 '16 at 17:06
  • It is working with GetString() but not with fgets(). – qwerty Dec 6 '16 at 17:47
  • Exactly. Using fgets, the "enter" to indicate end of input (new line) is included in searchfor. Therefore, strstr does not find a match. – DinoCoderSaurus Dec 6 '16 at 17:52
0
 #include <stdio.h>
 #include<cs50.h>
 #include <string.h>
 char array[5][80] = {"CS50", "C Programming", "Java", "Python", "PHP",};
 char* find(char search[])
 {


     for (int i = 0; i < 5; i++) 
     {
         char* n = strstr(array[i], search);
         if (n != NULL)
         {

           return (n);        //printf("Track %i: '%s'\n", i, array[i]);
         }
     }
     return "no found";
 }
 //void find(char search[]);
 int main()
 {
     char* searchfor;
     puts("Search for:");
     searchfor = GetString();
     char* m = find(searchfor);
     printf("Track : '%s'\n" , m);
     free(searchfor);
     return 0;
 }

Is a possible alternative to your program

2
  • I know it works.But I am trying to avoid using getstring since it exists only in cs50 library.Tell me an alternative for my program using fgets. – qwerty Dec 6 '16 at 19:34
  • GetString is like any other function in C, like printf for example, these functions are not part of the language but someone wrote them, to make life easier. As for your question, we can use fgets by adapting our program so that the strings match, in order to use srtstr, as you want, there are functions like gets, which add '\ 0' to the end of the string, but are considered obsolete and normally no longer part of stdio.h: char array[5][80] = {"CS50\n", "C Programming\n", "Java\n", "Python\n", "PHP\n",}; – MARS Dec 6 '16 at 23:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .