在c编程中搜索二维数组中的单词

时间:2014-05-13 19:21:48

标签: strstr

我正在尝试设置一个程序,当你输入/ s然后是一个电影标题时(例如/ s Green Mile它在电影数组中搜索那个电影标题,但我的strstr功能不起作用。还/ a添加movie / r显示数据库和/ q退出程序中的所有电影。

#include <stdio.h>
#include <string.h>
main() {

    char movies[1000][64];
    int i;
    int j = 1;
    int k = 0;
    int counter = 0;
    char buffer[64];
    char buffer2[64];
    int len;

    do {
      printf("Enter command for movie database:\n");
      fgets(buffer, 64, stdin);

      if (buffer[1] == 'a') {
         len = strlen(buffer);
         if (buffer[len - 1] == '\n')
             buffer[len - 1] = '\0';

         for(i=3; i<sizeof(buffer); i++) {
            movies[counter][k] = buffer[i];
            k++;
         }
         printf("You have chosen to enter a movie in the database.\n");
         printf("The movie added to the database is: %s\n", &movies[counter][0]);
         counter++;
         k = 0;
      }

      if (buffer[1] == 'q') {
         printf("You have chosen to quit.\n");
         printf("Goodbye.\n");
      }

      if (buffer[1] == 's') {
         for(i=2; i<sizeof(buffer); i++) {
            buffer2[k] = buffer[i];
            k++;
         }

         for (i = 0; i < counter; i++) {
            if (strstr(movies[i], buffer2) != NULL) {
               printf("Found %s in position %d,%s\n", buffer2, i + 1, movies[i]);
               printf("Index of Movie is %d.\n", i + 1);
            }
         }
         k = 0;
      }

      if (buffer[1] == 'r') {
         printf("You have choosen to report movies in database\n");
         printf("These are the movies you have watched:\n");
         for( i = 0; i < counter; i++ ) {
            printf("%d: %s\n", j,  &movies[i][0]);
         j++;
         }
      printf("\n");
      j = 1;
      }
   } while (buffer[1] != 'q');
}

1 个答案:

答案 0 :(得分:0)

修改后的代码。

#include <stdio.h>
#include <string.h>
main() {

    char movies[1000][64];
    int counter = 0;
    char buffer[64];
    char * movieTitle = &buffer[3];

    while(true) {
      printf("Enter command for movie database:\n");
      fgets(buffer, 64, stdin);

      //replace terminator every time
      int len = strlen(buffer);
      if (buffer[len - 1] == '\n')
          buffer[len - 1] = '\0';

      if(strstr(buffer, "/a") && counter < 1000) {
         strcpy(movies[counter], movieTitle);

         printf("You have chosen to enter a movie in the database.\n");
         printf("The movie added to the database is: %s\n", &movies[counter][0]);
         counter++;
      }

      else if (strstr(buffer, "/s")) {
         for (int i = 0; i < counter; i++) {
            if (strcmp(movies[i], movieTitle) == 0) {
               printf("Found %s in position %d,%s\n", movieTitle, i + 1, movies[i]);
               printf("Index of Movie is %d.\n", i + 1);
            }
         }
      }

      else if (strstr(buffer, "/r")) {
         printf("You have choosen to report movies in database\n");
         printf("These are the movies you have watched:\n");

         for( i = 0; i < counter; i++ ) {
            printf("%d: %s\n", i + 1,  &movies[i][0]);
         }
         printf("\n");
      }

      else if (strstr(buffer, "/q")) {
         printf("You have chosen to quit.\n");
         printf("Goodbye.\n");
         break;
      }

      else {
          printf("unrecognized command");
      }
   }
}