CS140 -- Midterm Exam: Q4 - Q7

The following code is for questions 4 through 7
char *revstring(char *s)
{
  int i, len;
  char tmp;

  len = strlen(s);

  for (i = 0; i < len/2; i++) {
    tmp = s[i];
    s[i] = s[len-i-1];
    s[len-i-1] = tmp;
  }
  return s;
}
The following choices will be for questions 4 through 7.
a. Give Him Six!
b. !xiS miH eviG
c. Jim
d. miJ
e. Jime Him SmiJ
f. JimmiJe Him Six!
g. Segmentation Violation
h. Could be anything (seg fault, bus error, random characters)


Question 4: When the following main() is compiled with the above implementation of revstring(), what is the output of the program?
main()
{
  char s1[100];

  strcpy(s1, "Give Him Six!");
  printf("%s\n", revstring(s1));
}
Question 5: When the following main() is compiled with the above implementation of revstring(), what is the first line of output of the program?
main()
{ 
  char s1[100];
  char *s;
  
  strcpy(s1, "Give Him Six!");
  s = revstring(s1);
  strcpy(s1, "Jim");
  printf("%s\n", revstring(s1));
  printf("%s\n", s);
}
Question 6: What is the second line of output (of the program in Question 5)? Question 7: When the following main() is compiled with the above implementation of revstring(), what is the output of the program?
main()
{
  char *s, *s2, *s3;

  strcpy(s, "Jim");
  s2 = strdup(s);
  
  s3 = revstring(s);
  printf("%s\n", s2);
}