Worksheet: C2 | CS 2113 Software Engineering - Spring 2022

Worksheet: C2

Worksheets are self-guided activities that reinforce lectures. They are not graded for accuracy, only for completion. Worksheets are due by Sunday night before the next lecture.

Note

Attempt to answer these questions before running the code. This will improve your ability to analyize and reason about code without an IDE or compiler. This skill we be helpful on the exams.

Questions

  1. Examine the program below and answer the following questions.

    #include <stdio.h>
    
    int main() {
        char a[3] = "Go Buff!";
    
        printf("%s\n", a);
    }
    
    • Explain why the full string "Go Buff!" will not print.
    • Provide two ways to fix the program so the full string would print.

  2. Examine the program below and answer the following questions.

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char str[] = "string";
    
        if (sizeof(str) == strlen(str)) {
            printf("equal\n");
        } else if (sizeof(str) > strlen(str)) {
            printf("greater than\n");
        }
    }
    
    • What is the output of this program?
    • How is the sizeof function different from the strlen function?

  3. Consider the following code snippet

    #include <stdio.h>
    #include <string.h>
    
    int main(){
      char str[] = "A string";
    
      printf("sizeof(str) =  %lu\n",sizeof(str));
      printf("strlen(str) =  %d\n",strlen(str));
      
    }
    
    • What is the output of this program?
    • Why does the sizes differ?

  4. Consider the following code snippet

    #include <stdio.h>
    #include <string.h>
    
    int main(){
      char * str = "A string";
    
      printf("sizeof(str) =  %lu\n",sizeof(str));
      printf("strlen(str) =  %d\n",strlen(str));
      
    }
    
    • What is the output of this program?
    • Why are the sizes same or different?

  5. Examine the program below and answer the following questions.

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char a[] = "aaa";
        char b[] = "bbb";
    
        if (strcmp(a, b) == -1) {
            printf("aaa is less than bbb\n");
        }
    
        if (strcmp(b, a) == 1) {
            printf("bbb is greater than aaa\n");
        }
    }
    
    • What is the output of this program?
    • Explain how function strcmp from the string.h library works.

  6. Examine the program below and answer the following questions.

    #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");
        }
    }
    
    • What is the output of this program?
    • Describe how a char * and an char array are similar.

  7. Examine the program below and answer the following questions.

    #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");
        }
    }
    
    • What is the output of this program?
    • Why is it different then the output from the previous question.

  8. The following program is WRONG!

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char str[100] = "C is my favorite language";
    
        str[strlen(str)+0] = '!';
        str[strlen(str)+1] = '!';
        str[strlen(str)+3] = '\0';
        
        printf("%s And python is my second!\n", str);
    }
    

    We want the output to be

    C is my favorite language!! And python is my second!
    

    But the output is this instead (note the single !)

    C is my favorite language! And python is my second!
    

    Answer the following questions:

    • Why did the program not function as expected?
    • Provide a corrected version of the program

  9. Examine the program below and answer the following questions.

    #include <stdio.h>
    #include <string.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");
        }
    }
    
    • What is the output for P1, P2, P3, and P4?
    • Please explain what exactly each if statement is checking, as in what is being compared and why they are equal or not.

  10. Examine the program below and answer the following questions.

    #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\n", str);
    }
    
    • What is the output of this program?
    • How did this happen?

  11. Examine the program below and answer the following questions.

    #include <stdio.h>
    
    void super_cool_function(char *p) {
        *p = *p - 4;
    }
    
    int main() {
        char a[6] = {'L','I','P','P','S', 4};
        
        for (int i = 0; i < 6; i++) {
            char * b = &a[i];
            super_cool_function(b);
        }
    
        printf("%s\n", a);
    }
    
    • What is the output of this program?
    • What does super_cool_function do?

  12. Examine the program below and answer the following questions.

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char string_a[24] = "Raise High";
        char * string_b = " the Roof Beam, Carpenters";
    
        printf("%s\n", strncat(string_a, string_b, 14));
    }
    
    • What is the output of this program?
    • What is the function definition of the C string.h function strncat. (Hint: use the man pages!)

  13. Consider the following code snippet

    #include <stdio.h>
    #include <string.h>
    
    int main(){
        char str[] = "this is a string";
        
        for(char * p = str; *p ; p++){//<-- A
            if(*p > 'l'){
                *p = *p+1; //<-- B
            }
        }
    }
    
    • For the code line marked as A, explain why the for loop will properly iterate over each character in the string str.
    • For the code line marked as B, explain how this will modify the string
    • What is the expected output?

  14. Consider the following code snippet

    int mystery(char * s1, char * s2){
      while( *s1++ == *s2++ && *s1 && *s2);//<-- A
      return( *s1 == '\0' && *s2 == '\0' );//<-- B
    }
    
    • What does the mystery function do?
    • Offer an explanation for the functionality of line A
    • Offer an explanation for the functionality of line B

  15. Examine the program below and complete the code.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void print_error_and_exit(void) {
        fprintf(stderr, "USAGE ERROR - try again like this:\n");
        fprintf(stderr, "count -to <number> -by <number>\n");
        exit(1); //exit the program
    }
    
    int main(int argc, char * argv[]) {
        int to_num;
        int by_num;
    
        // Validate argument count
        if (argc < 5 || argc > 5) {
            print_error_and_exit();
        }
    
        // Validate -to parameter
        if (strcmp(argv[1], "-to") != 0) {
            print_error_and_exit();
        }
    
        // Validate -to parameter number
        if (sscanf(argv[2], "%d", &to_num) == 0) {
            print_error_and_exit();
        }
    
        // COMPLETE THE PARAMETER VALIDATION CODE
    
    
        for (int count = by_num; count <= to_num; count += by_num) {
            printf("%d\n", count);
        }
    }
    

    Finish the code above so that the output is the following:

    $ ./count bad parameters 
    USAGE ERROR - try again like this:
    count -to <number> -by <number>
    
    $ ./count -to 10 -by 2
    2
    4
    6
    8
    10