logo

UTK Notes


Clicker Questions - 04-Strings

Given the following program:

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

int main() {
    char s[50];
    char *x, *y;
    
    strcpy(s, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    x = s + 2;
    y = s + 5;

    strcpy(x, "01234567");
    printf("%s\n", s);

    strcat(y, "abcfe");
    printf("%s\n", x);

    printf("%s\n", x+15);
}

Question 1: What is the first line of ouput of this program?

Answer Let's use ASCII art to look at s, x, and y. We'll use an asterisk to represent the null character.
ABCDEFGHIJKLMNOPQRSTUVWXYZ*
^ ^    ^
k |    |
s x    y
After the strcpy(), the bytes become:
AB01234567*LMNOPQRSTUVWXYZ*
^ ^    ^
| |    |
s x    y
So the first line of output is: "AB01234567".

Question 2: What is the second line of ouput of this program?

Answer Now, the strcat() finds the end of the string that with '6', and appends "abcfe" to it:
AB01234567abcde*QRSTUVWXYZ*
^ ^    ^
| |    |
s x    y
So the second line is the string that starts with x: "01234567abcde".

Question 3: What is the third line of ouput of this program?

Answer We can count 15 characters from x:
AB01234567abcde*QRSTUVWXYZ*
^ ^    ^         ^
| |    |         |
s x    y        x+15
The last line of ouput is the string starting with "R": "RSTUVWXYZ".

PDF Download, 2022 Class Stats, 2023 Class Stats, 2024 Class Stats