What is the program /code to find index of a string in another string array?(C language) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the program /code to find index of a string in another string array?(C language)

The searching algorithm will have two inputs: the playlist, which is a string array that contains a list of songs in alphabetical order; and a particular song, which is a string. If the song is found in the list, the algorithm will return the index of the song, and it will return -1 otherwise. Please help ne with this code in C language

19th Aug 2020, 7:15 AM
Shweta Rajan Nambissan
Shweta Rajan Nambissan - avatar
4 Answers
0
#include <stdio.h> #include <string.h> int findTarget(char *target, char nameptr[][80], int size); int main() { int num, i; char target[100]; char names[10][100]; printf("Enter no. of songs:"); scanf("%d", &num); printf("Enter %d songs : ", num); for (i = 0; i < num; i++) { scanf("%s", names[i]); } printf("Enter target song: "); fflush(stdin); scanf("%s", target) ; printf("findTarget(): %d", findTarget(target, names, num)); } int findTarget(char *target, char nameptr[][80], int size) { int i; for (i = 0; i < size; i++) { if (strcmp(target,nameptr[i]) == 0) { return i; } } return -1; }
19th Aug 2020, 7:38 AM
Shweta Rajan Nambissan
Shweta Rajan Nambissan - avatar
0
#include <stdio.h> #include <string.h> int findTarget(char *target, char nameptr[][100], int size); int main() { int num, i; char target[100]; char names[10][100]; printf("Enter no. of songs:"); scanf("%d", &num); printf("Enter %d songs : ", num); for (i = 0; i < num; i++) { scanf("%s", names[i]); } printf("Enter target song: "); fflush(stdin); scanf("%s", target) ; printf("findTarget(): %d", findTarget(target, names, num)); } int findTarget(char *target, char nameptr[][100], int size) { int i; for (i = 0; i < size; i++) { if (strcmp(target,nameptr[i]) == 0) { return i; } } return -1; }
19th Aug 2020, 8:47 AM
Jayakrishna 🇮🇳
0
Thank you Jayakrishna
19th Aug 2020, 8:58 AM
Shweta Rajan Nambissan
Shweta Rajan Nambissan - avatar
0
Shweta Rajan Nambissan you're welcome.. It has just warning for size not matching.. Everything is fine otherwise..
19th Aug 2020, 9:06 AM
Jayakrishna 🇮🇳