String exercise -- enter keywords to search for songs, and enter keywords in strings
# Include <stdio. h> # include <string. h> // use the strstr () function to find the string char tracks [] [80] = {// song list "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 ",}; void find_track (char search_for []) {int I; for (I = 0; I <5; I ++) {if (strstr (tracks [I], search_for )) // display all matching songs printf ("Track % I: '% s' \ n", I, tracks [I]) ;}} int main () {char search_for [80]; printf ("Search for:"); // enter the Search keyword fgets (search_for, 80, stdin); search_for [strlen (search_for) -1] = '\ 0'; find_track (search_for); // display the matched song return 0 ;}
Running result:
Key Points
- You can use char strings [...] [...] to create an array.
- The first group of square brackets is used to access the outer array.
- The second set of square brackets is used to access the elements in each inner array.
- With the string. h header file, you can use the string processing function in the C standard library.
- You can create multiple functions in a C program, but the computer always runs main () first ().
Problem