logo

How to find a string in an array of strings in C

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char* argv[]) { char arr[3][100] = {"apple", "orange", "programmer"}; char input[100]; printf("Input a word: "); fgets(input, 100, stdin); input[strlen(input) - 1] = 0; int i; for (i = 0; i < strlen(input); i++) { input[i] = tolower(input[i]); } for (i = 0; i < 3; i++) { if (strcmp(arr[i], input) == 0) { printf("Found input string at index %d\n", i); break; } } if (i >= 3) { printf("The input string was not found\n"); } printf("Value of i is %d\n", i); return 0; }