Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You have seen that the switch construct only allows to test for integer or chara

ID: 3676942 • Letter: Y

Question

You have seen that the switch construct only allows to test for integer or character values. Very often, one wishes to test strings. For instance, you may imagine having to recognize a limited number of strings, such as file extensions, to be able to say that if you have '.exe' then it's an executable program, if you have '.wav' or '.mp3' then it's an audio file, and if you have '.jpg', '.gif or '.png' then it's an image file. You could of course use strcmp() in a series of if... else if... else if... statements, but this is a bit clumsy, especially as the list grows, and it may take more and more time to check values. You will code in this lab an alternative way of recognizing strings, which works as follows. You store all the words that you want to recognize (file extensions are a good example) into an array of strings in which the words are in alphabetical order (this is important!). Note that you should net let the compiler compute the size, but set it. For instance You write a function that runs a binary search into the array. The principle of a binary search is simple, and works as a search in a dictionary: you compare (using strcasecmp()) the value that you are looking for to the value in the middle of the array. If it's the value you were looking for, you return its index in the array. If not, if it's greater than what you are looking for, then the value that you are looking for, if present, should be in the first half of the array, and you check the value in the middle of the first half. Otherwise you check the value in the middle of the second half. You repeat until you find the value or you can say with certainty that it's not in the array, in which case you can return - 1. With the index returned by the preceding function (an integer!) you can use a switch construct and display the suitable message for the suitable case. Write a program that uses the array above, a search function, reads an extension, and displays a message saying what type of file it should be.

Explanation / Answer

#include <stdio.h>
#define ARRAY_SIZE 8


int binary_search(char *file_extensions[], char *target)
{
int bottom= 0;
int mid;
int top = ARRAY_SIZE - 1;

while(bottom <= top)
{
mid = (bottom + top)/2;
if (strcmp(file_extensions[mid], target) == 0)
{
return mid;
} else if (strcmp(file_extensions[mid], target) > 0)
{
top = mid - 1;
} else if (strcmp(file_extensions[mid], target) < 0)
{
bottom = mid + 1;
}
}
return -1;
}

void switch_case (char *file_extensions[], int index)
{
   switch(index) {
case 0 :
printf("It should be .c file! " );
break;
case 1 :
   printf("It should be .exe file! ");
   break;
case 2 :
printf("It should be .gif file! " );
break;
case 3 :
   printf("It should be .h file! ");
   break;
   case 4 :
printf("It should be .jpg file! " );
break;
case 5 :
   printf("It should be .mp3 file! ");
   break;
   case 6 :
printf("It should be .png file! " );
break;
case 7 :
   printf("It should be .wav file! ");
   break;
default :
printf("File Extension Not Found! " );
}
}

int main()
{
   char *file_extensions[ARRAY_SIZE]= {".c",".exe",".gif",".h",".jpg",".mp3",".png",".wav"};

   char input[20];
   printf("Enter the Extension you want to Find: ");
   scanf("%s",&input);
   int index = binary_search(file_extensions,input);
   switch_case(file_extensions,index);

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote