0

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<5because 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?

nightshade
  • 638
  • 5
  • 15
Manish Giri
  • 3,562
  • 8
  • 45
  • 81
  • you could declare the string array as `char *tracks[] = {"xxx","yyy",..., NULL}` then you do not need to know how many string there are since you can loop until you find NULL. `for (i = 0; tracks[i] != NULL; ++i)` – AndersK Jun 10 '14 at 08:39

3 Answers3

2

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!");
}
P.P
  • 117,907
  • 20
  • 175
  • 238
0

You can do:

for (i = 0; i < 5; i++) {
    if (strstr(tracks[i], searchfor) {
        printf("\nMatch found\n);
        printf("Track[%n]: %s", i, tracks[i]);
        return;
    }
}
printf("\nNo matching tracks found!");
Teo Zec
  • 383
  • 2
  • 11
0

to adapt to a variable type you can use a define

#define TRACKS 5

right after the includes... whenever you want to change the amount no another value you just have to change that 5 to the new value.

 void findTrack(char search_for[])
 {
    int i;
    int found = 0;
    for(i = 0; i < TRACKS; 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!");
  }

I've commented the break because that depends on what you want to do... If you want to find just the first track that has that word in it you should let the break but if you want to find all matches, then you shouldn't break after findind the first one.

nightshade
  • 638
  • 5
  • 15