Lecture Practice Quiz 8     

This quiz is the same as the Lab quiz (except for swap, which now swaps the pointers). 
However, this time you are not allowed to use the characters "[" and "]". (You must 
use pointer notation to write your functions.)

1. Kernighan and Ritchie strcmp.c (page 106)
   Write the function int strcmp(char *cs, char *ct) compares string cs to string ct and
   returns <0 if cs<ct, 0 if cs == ct, or >0 is cs > ct. With the addition of your function, 
   the program below should output a negative number (-13 would be OK).

   #include <stdio.h>
   int strcmp(char *s, char *t);
   main() {
      char s[] = "This is the first string";
      char t[] = "This is the second string";
      printf("%d\n", strcmp(s, t));
   }


2. Kernighan and Ritchie strcpy.c (page 105, 106)
   Write the function char *strcpy(char *s, char *t) that copies string t to 
   string s, returning s. With the addition of your function, the program below
   should output "This is the string" followed by "This is the string".

   #include <stdio.h>
   char *strcpy(char *s, char *t);
   main() {
      char s[100];
      char t[] = "This is the string";
      printf("%s\n", strcpy(s, t));
      printf("%s\n", s); 
   }


3. Kernighan and Ritchie strlen.c (page 39, 99, 103)
   Write the function int strlen(char *s) which returns the length of string s 
   (excluding the terminal '\0'). With the addition of your function, the program 
   below should output 18 

   #include <stdio.h>
   int strlen(char *s);
   main() {
      char s[] = "This is the string";
      printf("%d\n", strlen(s));
   }


   
4. Kernighan and Ritchie swap.c (page 88, 96, 110, 121)
   swap - swap two integer pointers (Thats POINTERS). With your program swap, the 
   following program should print "10 20" (a and b have not changed) followed by
   "20, 10" (the pointers have changed).

cis-lclient05:~/2012/cquiz>more pswap.c
   #include <stdio.h>
   void swap(int **ppx, int **ppy);
   main() {
      int a = 10, b = 20;
      int *pa = &a, *pb = &b;
      swap(&pa, &pb);
      printf("%d %d\n", a, b);
      printf("%d %d\n", *pa, *pb);
   }