I am trying to learn string handling in C. I wrote out a program, which stores some music tracks, and helps the user to check if the song he/she has in mind, exists in the tracks stored. This is done by asking the user to enter a string of characters. The program then uses the strstr() function to check if the word/text entered matches with any word/text on any track, and if so, it displays the track number and name.
The code which I have written functions properly on the outset. But there's a big flaw, and it is due to incorrect usage of the for loop. I'll show the program and two pieces of output first-
#include<stdio.h>
#include<string.h>
//Define tracks array
char tracks[][80]={ //[][80] is a 2D array- for storing tracks and the size of each track, which is 80 at max
"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",
};
//Define function to search for text in tracks array
void findTrack(char search_for[])
{
int i;
for(i = 0; i < 5; i++)
{
if(strstr(tracks[i], search_for))
{
printf("\n Match found\n");
printf("\n Track[%i]: %s\n", i, tracks[i]);
break;
}
else
printf("\n No matching tracks found!");
}
}
int main()
{
char search_for[80];
printf("\n Enter the text to search:\n");
scanf("%79s", search_for);
findTrack(search_for);
return 0;
}
1st Output-
Enter the text to search:
Harvard
Match found
Track[0]: I left my heart in Harvard med school
As you can see, this output is correct. The word Harvard
was present in the first track I stored in the tracks
array. This was found in the first iteration of the for loop
inside the findTrack
function, hence the correct output.
However, if I run the program again, and this time I give the text to search as town
, the resulting output is -
2nd Output-
Enter the text to search:
town
No matching tracks found!
Match found
Track[1]: Newark, Newark- a wonderful town
This happened because on the first iteration of the for
loop ie: tracks[0]
, the strstr
did not find any match, so it printed the statement in the else
part of the loop. When the loop completed iteration 1, for tracks[1]
, a match was found in town
, and hence it printed the if
part of the loop.
Similarly, if I run the program again and give Jima
as the text to search, the output is-
Enter the text to search:
Jima
No matching tracks found!
No matching tracks found!
No matching tracks found!
No matching tracks found!
Match found
Track[4]: The girl from Iwo Jima
I think the flaw is due to the incorrect for loop. But I'm not sure how to do it right using a for loop. Any suggestions on correcting this would be highly appreciated. Thank You!
EDIT
In my code, I'm using a for
loop with i<5
because I know that there are 5 tracks
stored in my program. However, a better way to code would be to run a loop irrespective of/in cases when the number of tracks is not known. This could happen in a scenario when my number of tracks change, so it would be inefficient to keep changing the for loop each time. Any suggestions on how to implement this change?
You only want to print Not found
after checking the entire array tracks
for match. So you could use a flag and check it after the loop:
void findTrack(char search_for[])
{
int i;
int found = 0;
for(i=0;i<5;i++)
{
if(strstr(tracks[i], search_for))
{
found = 1;
printf("\n Match found\n");
printf("\n Track[%i]: %s\n", i, tracks[i]);
break;
}
}
if (!found) printf("\n No matching tracks found!");
}