Reverse a String in C Using Pointers – Three Approaches with Code

Reversing a string in C means rearranging its characters so the last character becomes the first and vice versa: “Hello” becomes “olleH”. The standard technique is the two-pointer swap — place one pointer at the start and one at the end, swap the characters they point to, then move the pointers toward each other until …

K&R C Chapter 7 Exercise Solutions — Input and Output

Chapter 7: Input and Output covers the standard I/O library in depth — formatted output with printf, formatted input with scanf, file access with fopen/fclose, line I/O with fgets/fputs, and error handling via stderr. The chapter also introduces variable-argument functions (stdarg.h) through a worked minprintf example. The exercises range from practical utilities (a case-converter, a …

C Program to Convert Number to Words (0 to 99999)

Converting a number to words means printing its English name: 4562 becomes four thousand five hundred sixty two. This is useful in invoice generation, banking applications, and cheque printing. The logic handles a key edge case — the teens (11–19) — where the tens digit 1 does not produce “ten X” but a single combined …

K&R C Chapter 6 Exercise Solutions — Structures

Chapter 6: Structures introduces struct, typedef, and self-referential structures (linked lists, binary trees). The chapter builds a word-frequency counter using a binary tree, then extends it with a hash table. Exercise 6-3 adds a cross-reference listing; 6-6 implements a rudimentary #define processor using a hash table — the most complex exercise in this chapter. These …

Day of the Week from a Date of Birth in C

Problem Statement Write a C program to find the day of the week from a date of birth. For example, given 02/04/2017 (2nd April 2017), the program should tell you it was a Sunday. The Approach Pick a base year whose January 1st is a known weekday. We use 1900 (Jan 1, 1900 was a …

K&R C Chapter 5 Exercise Solutions — Pointers and Arrays

Chapter 5: Pointers and Arrays is the chapter C beginners fear most — and with reason. K&R covers pointer arithmetic, the equivalence between pointers and arrays, pointers to functions, and multi-dimensional arrays. The exercises build real utilities: getfloat, strncpy/strncat/strncmp from scratch, a tail command, a sort program with -r/-n/-f/-d flags, and a full C declaration …