logo

UTK Notes


Clicker Questions - 05-More-Strings

Behold the following program in C:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    char s[20];
    int i;

    for (i = 0; i < 15; i++) s[i] = '0' + i % 10;
    s[i] = '\0';

    printf("%s\n", s);                   /* First line*/
    
    for (i = 0; i < 15; i += 4) s[i] = '\0';

    for (i = 0; i < 15; i += 3) {
        printf("%d-%s\n", i, s+i);      /* Lines two through six */    
    }

    strcat(s+7, s+13);
    printf("%s\n", s+7);                /* Last line */

    return 0;
}

This program has seven lines of output. Each clicker question asks for a line of output.

Remember, it’s strcat(destination, source).

Question 1: What is line 1?

Answer You should write down your work to answer this question. It's easiest to do in a text editior, but if you don't have access to a text editor while doing this, then use a piece of paper. Trying to do this in your head will be confusing.

Start with writing down the characters in s, using an asterisk for the null character:
s: 01234567891234*
Let's label where s+11 is:
s: 01234567891234*
             |
             s
That lets us answer the first question: "1234".

Question 2: What is line 2?

Answer Now, let's set every fourth character to the null character, starting with character zero:
s: *123*567*901*34*
And let's label the pointers at s is:
s: *123*567*901*34*
   |
   0
That lets us answer the second question: 0-

Question 3: What is line 3?

Answer Now, let's set every fourth character to the null character, starting with character zero:
s: *123*567*901*34*
And let's label the pointers at s+3 is:
s: *123*567*901*34*
   |  |
   0  3
That lets us answer the third question: 3-3

Question 4: What is line 4?

Answer Now, let's set every fourth character to the null character, starting with character zero:
s: *123*567*901*34*
And let's label the pointers at s+6 is:
s: *123*567*901*34*
   |  |  |
   0  3  6
That lets us answer the fourth question: 6-67

Question 5: What is line 5?

Answer Now, let's set every fourth character to the null character, starting with character zero:
s: *123*567*901*34*
And let's label the pointers at s+9 is:
s: *123*567*901*34*
   |  |  |  |
   0  3  6  9
That lets us answer the fifth question: 9-901

Question 6: What is line 6?

Answer Now, let's set every fourth character to the null character, starting with character zero:
s: *123*567*901*34*
And let's label the pointers at s+12 is:
s: *123*567*901*34*
   |  |  |  |  |
   0  3  6  9  12
That lets us answer the sixth question: 12-

Question 7: What is line 7?

Answer Now, let's label s+7 and s+13:
s: *123*567*901*34*
          |     |
        s+7   s+13
The strcat() will find the first null character after (s+7). That's at s+8. It then copies the characters from s+13 to the null character, and null terminates. The string now looks like:
s: *123*56734*1*34*
          |     |
        s+7   s+13
When we print starting at s+6, we get the characters "6734".

PDF Download, 2022 Class Stats, 2024 Class Stats