String Functions

Remember that any answers to questions can appear as in the output, i.e.
printf("I observe ....");
or you can type your answers into files and use "cat" to show them. Have a different program for each part, even though there might be code copied from one to another. Don't forget to add the appropriate "include" statements at the top of the programs.

Part 1

1a) Check the program below. What do you think will be the output? Include a detailed explanation of your answer with your reasoning. What do you think is required to fix and compile it, if it has errors? In case it does not have errors, no explanation is needed. (Think in terms of ASCII codes and switch/case in C)

  #include <stdio.h>
  #include <string.h>



  int main() {
	char i ='A'; 
	switch(i){
	    case 'A':
	        printf("A"); break;
	    case 'a':
	        printf("a"); break;
	    case 'B':
	        printf("e"); break;
	    case 65:
	        printf("c"); break;
	    case 67:
	        printf("C"); break;
	    default:
	        printf("D"); break;
	}

  }
    

1b) Explain what the following functions are useful for (explain the variation of these function with different possible parameters and return value for each of them)
  1. strlen()
  2. strncmp()
  3. strncpy()
  4. strncat()

Write your answers in a text file and use a cat command to display it in the script.

Part 2

Consider the following code.

  #include <stdio.h>
  #include <string.h>
  
  int main() {
    char str[10] = "Gouda"; 
  
    printf("string is %s.\n", str);
    printf("length of str is %lu\n", strlen(str));
    printf("length of str is %lu\n", sizeof(str));
    if (strncmp(str, "Gouda", 10)) 
      printf("According to usdairy.com, Gouda can be grated, sliced, cubed and melted\n");
    else
      printf("Unknown cheese\n");
  
    return 0;
  }
When run, it gives the following output.
string is Gouda.
length of str is 5
length of str is 10
Unknown cheese 
Why are the two reported lengths different? Explain. Why is the cheese unknown? Explain.

Part 3

Write a program to take an input string from a user, using a char array to store the string of length up to 20 characters (use a null character check to stop the loop). Your program should change the case of letters entered by user, i.e., if "AbCdefGuM" is input, your program should convert it to "aBcDEFgUm". Do not use string anywhere even while printing the output; you should print one char at a time (NO new line character or space character should come after the output, i.e. output needs to be "aBcDEFgUm" only.)

Part 4

For this part you will need to use a 2-D char array to store 8 names of cheeses. Assumption: A cheese name can be up to 20 characters, and it always start with capital letter, e.g. "Havarti", not "havarti". Choose the names while writing a program and store it in string variables. Use the strncpy() function to copy the cheese name to the 2D char array defined earlier in this part. Your program should sort the cheese names in alphabetical order. Print the 2D array without using string literals. That is, use "%s" with a printf statement, do not use printf("Havarti"); . Test it with at least 3 test cases (with sorted list in acsending order, with sorted list in acsending order in descending order, random names but not sorted).
HINT: you will need a 1-D char array as temp and nested loops to sort the list of cheese names. You will have to use strncpy() and strncmp() functions.