0
#include <stdio.h>

#include <string.h>

char tracks[][80] = {
    "I left my heart in Harvard Med School",
    "Newark, Newark - a wonderful town",
    "Dancing with a Dork",
    "From here to maternity",
    "The girl from Iwo Jima",
};

void find_track(char search_for[]) {

  int i;

  for (i = 0; i < 5; i++) {

    if (strstr(tracks[i], search_for))

      printf("Track %i: '%s'\n", i, tracks[i]);
  }
}

int main() {

  char search_for[80];

  printf("Search for: ");

  fgets(search_for, 80, stdin);

  find_track(search_for);

  return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
RIBA
  • 1
  • 1
  • 3
    "does not run as it should be" doesn't help. What does it do? What does it not do? What should it do? – AKX Jan 08 '22 at 23:24
  • It does not find the string in the array of strings - Using the function strstr – RIBA Jan 08 '22 at 23:43

1 Answers1

0

The function fgets can append the entered string with the new line character '\n'.

You need to remove it as for example

fgets(search_for, 80, stdin);
search_for[ strcspn( search_for, "\n" ) ] = '\0';
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335