Post

Week 11: Strings and String Functions — Solution

Week 11: Strings and String Functions — Solution

Exercise Solutions

Exercise 1: Read and Count Characters

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

int main(void) {
    char str[100];
    printf("Enter a sentence: ");
    fgets(str, sizeof(str), stdin);

    // strip trailing newline from fgets for an accurate count
    size_t len = strlen(str);
    if (len > 0 && str[len - 1] == '\n') {
        str[len - 1] = '\0';
        len--;
    }

    printf("You typed: %s\n", str);
    printf("Length: %zu\n", len);
    return 0;
}

Example Output:

1
2
3
Enter a sentence: I like pizza.
You typed: I like pizza.
Length: 13

Exercise 2: Modifying char[] vs char * Strings

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

int main(void) {
    char arr[] = "Hello";   // mutable: a copy on the stack
    char *ptr  = "Hello";   // points to a read-only string literal

    arr[0] = 'X';
    printf("Modified arr: %s\n", arr);

    // The line below is undefined behavior on most platforms — it crashes
    // because string literals live in read-only memory.
    //
    // ptr[0] = 'X';
    printf("Modifying ptr would write to read-only memory and crash.\n");

    return 0;
}

Example Output:

1
2
Modified arr: Xello
Modifying ptr would write to read-only memory and crash.

Exercise 3: Use String Functions Together

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

int main(void) {
    char a[100], b[100];

    printf("Enter first word: ");
    scanf("%99s", a);
    printf("Enter second word: ");
    scanf("%99s", b);

    if (strcmp(a, b) == 0) {
        printf("Comparison result: equal\n");
    } else {
        printf("Comparison result: not equal\n");
    }

    strcat(a, b);
    printf("Joined: %s\n", a);
    return 0;
}

Example Output:

1
2
3
4
Enter first word: Hello
Enter second word: World
Comparison result: not equal
Joined: HelloWorld

Exercise 4: Convert and Sum Two Numbers

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    char s1[20], s2[20];
    printf("Enter two numbers: ");
    scanf("%19s %19s", s1, s2);

    int a = atoi(s1);
    int b = atoi(s2);
    printf("Sum: %d\n", a + b);
    return 0;
}

Example Output:

1
2
Enter two numbers: 12 34
Sum: 46

Exercise 5: Count Words

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

int main(void) {
    char str[200];
    printf("Enter a sentence: ");
    fgets(str, sizeof(str), stdin);

    int words = 0;
    int in_word = 0;
    for (int i = 0; str[i] != '\0'; i++) {
        if (str[i] == ' ' || str[i] == '\n' || str[i] == '\t') {
            in_word = 0;
        } else if (!in_word) {
            in_word = 1;
            words++;
        }
    }

    printf("Words counted: %d\n", words);
    return 0;
}

Example Output:

1
2
Enter a sentence: I like C programming a lot
Words counted: 6
This post is licensed under CC BY 4.0 by the author.