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.
palindrome.c— the same both ways. Writeint is_palindrome(char * s), which returns1ifsreads the same forwards and backwards, and0if not. It changes nothing ins. Yourmain: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.same.c— two strings, walked together. Writeint same(char * a, char * b), which returns1ifaandbhold the same characters, and0if not. Yourmain: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.append.c— one string after another. Writevoid append(char * dst, char * src), which adds the characters ofsrcat the end ofdst, with a'\0'after them.dstis an array with room for both. Yourmain:#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.squeeze.c— a letter taken out. Writevoid squeeze(char * s, char c), which removes everycfroms, in place. Two pointers walks: one reads every character, and the other writes only the ones that are notc. Yourmain: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.notes.txt— three short answers.char word[] = "hello";makessizeof(word)6. What is the sixth byte, and why does a string need it?same(a, b)walks the two strings. What woulda == bcompare instead, and why is that a different question?appendwrites past the'\0'ofdst. Ifdsthas no room forsrc, what does C promise will happen?
words.c— optional: count the words. This step is optional and not needed for full marks. Writeint count_words(char * s), which returns how many wordssholds, a word being a run of characters that are not spaces. Yourmain: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.