Quiz C2-1 | CS 2113 Software Engineering - Spring 2021

Quiz C2-1

Submit your answers via this Google Form

Question 1

What is the output of this program

#include <stdio.h>
int main() {

  char str[100] = "the importance of being earnest";
  str[0] -= 32;
  str[4] -= 32;
  str[18] -= 32;
  str[24] -= 32;
  printf("%s",str);
}

Question 2

Fix the program below so the output is “C is my favorite language! and python is my second!”:

#include <stdio.h>
#include <string.h>
int main() {

  char str[26] = "C is my favorite language";

  str[strlen(str)] = '!';
  printf("%s and python is my second!",str);
}

Question 3

What is the output of the program below?

#include <stdio.h>
#include <string.h>
int main() {

  char str1[10] = "10 9 8 7 6 5 4 3 2 1";
  char str2[] = "10 9 8 7 6 5 4 3 2 1";
         
  if(strcmp(str1,str2) == 0 ){ //tests if they are the same string
      printf("Same\n");    
  }else{
      printf("Different\n");
  }
}

Question 4

What is the output of the program below?

#include <stdio.h>
#include <string.h>
int main() {

  char str1[] = "10 9 8 7 6 5 4 3 2 1";
  char * str2 = "10 9 8 7 6 5 4 3 2 1";
         
  if(strcmp(str1,str2) == 0 ){ //tests if they are the same string
      printf("Same\n");    
  }else{
      printf("Different\n");
  }
}

Question 5

What is the function definition of the C string.h function strcat. (Hint: use the man pages!)

Question 6

What is the output for P1, P2, P3, and P4?

#include <stdio.h>

int main(){
  char *strings[3] = {
    "Hello",
    " world",
    " !!!"};
  char o = 'o';
  char *o_str = "o";
  char *world = "world";

  printf("P1: ");
  if( strings[0][4] == o){
    printf("true\n");
  }else{
    printf("false\n");
  }
  
  printf("P2: ");
  if (*strings[0]+4 == *o_str) {
    printf("true\n");
  } else {
    printf("false\n");
  }
  
  printf("P3: ");
  if (strcmp((strings[1] + 1),world) == 0) {
    printf("true\n");
  } else {
    printf("false\n");
  }
  
  printf("P4: ");
  if (strings[2] == " !!!") {
    printf("true\n");
  } else {
    printf("false\n");
  }
}