Assignment 6 — Strings with pointers

Four small programs that walk strings with a pointer and no brackets: one that reads a word both ways, one that compares two, one that joins two, and one that takes a letter out.

Due Oct 14, 2026

A string is an array of char that carries its own end, the '\0', and in Class 14 you walked one with a pointer, the way you walked an array. These four programs make the same walk with nothing but pointers: no [ or ] in any function but main.

The reading for the week is [DIS] 2.6.1 — C’s Support for Statically Allocated Strings and [DIS] 2.6.3 — Libraries for Manipulating C Strings and Characters, due before class on Thursday 10/15: Tuesday is fall break. Quiz 6, on Tuesday 10/20, has a question on it.

You will hand in five files: palindrome.c, same.c, append.c, squeeze.c and notes.txt — and words.c, if you do the optional step 6.

Every program compiles with gcc -Wall and no warnings. Every function you write gets the design recipe’s one-line comment above it. Every program also gets at least one assert of your own, beside the ones given.

  1. palindrome.c — the same both ways. Write int is_palindrome(char * s), which returns 1 if s reads the same forwards and backwards, and 0 if not. It changes nothing in s. Your main:

    int main() {
        assert(is_palindrome("racecar") == 1);
        assert(is_palindrome("abba") == 1);
        assert(is_palindrome("pointer") == 0);
        assert(is_palindrome("") == 1);
        printf("all examples passed\n");
    }

    Done when it prints all examples passed.

  2. same.c — two strings, walked together. Write int same(char * a, char * b), which returns 1 if a and b hold the same characters, and 0 if not. Your main:

    int main() {
        assert(same("pointer", "pointer") == 1);
        assert(same("pointer", "pointed") == 0);
        assert(same("point", "pointer") == 0);
        assert(same("pointer", "point") == 0);
        assert(same("", "") == 1);
        printf("all examples passed\n");
    }

    Done when it prints all examples passed. The third and the fourth asserts are the ones a walk that stops too early gets wrong.

  3. append.c — one string after another. Write void append(char * dst, char * src), which adds the characters of src at the end of dst, with a '\0' after them. dst is an array with room for both. Your main:

    #define SIZE 20
    
    int main() {
        char word[SIZE] = "point";
        append(word, "er");
        assert(word[5] == 'e');
        assert(word[6] == 'r');
        assert(word[7] == '\0');
        append(word, "");
        assert(word[7] == '\0');
        printf("all examples passed\n");
    }

    Done when it prints all examples passed.

  4. squeeze.c — a letter taken out. Write void squeeze(char * s, char c), which removes every c from s, in place. Two pointers walk s: one reads every character, and the other writes only the ones that are not c. Your main:

    int main() {
        char text[] = "a b  c";
        squeeze(text, ' ');
        assert(text[0] == 'a');
        assert(text[1] == 'b');
        assert(text[2] == 'c');
        assert(text[3] == '\0');
    
        char fruit[] = "banana";
        squeeze(fruit, 'a');
        assert(fruit[0] == 'b');
        assert(fruit[1] == 'n');
        assert(fruit[2] == 'n');
        assert(fruit[3] == '\0');
        printf("all examples passed\n");
    }

    Done when it prints all examples passed.

  5. notes.txt — three short answers.

    1. char word[] = "hello"; makes sizeof(word) 6. What is the sixth byte, and why does a string need it?
    2. same(a, b) walks the two strings. What would a == b compare instead, and why is that a different question?
    3. append writes past the '\0' of dst. If dst has no room for src, what does C promise will happen?
  6. words.c — optional: count the words. This step is optional and not needed for full marks. Write int count_words(char * s), which returns how many words s holds, a word being a run of characters that are not spaces. Your main:

    int main() {
        assert(count_words("two words") == 2);
        assert(count_words("  spaces   around  ") == 2);
        assert(count_words("") == 0);
        printf("all examples passed\n");
    }

    Done when it prints all examples passed.