C Program to Print First Letter of Each Word
C Program to Print First Letter of Each Word
A school text exercise needs to first character of each word while preserving the buffer’s valid characters and final terminator.
A word-start scan remembers whether the previous input was a delimiter and emits the first non-delimiter character of each word.
Before this exercise, review strings and their terminator and array bounds. The worked program below applies those ideas to this specific problem.
Explain
Explain why first character of each word uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Use a name with leading and repeated spaces and print only its two initials.
First Character of Each Word: follow the data
Given data
A school text exercise needs to first character of each word while preserving the buffer’s valid characters and final terminator.
Rule
A word-start scan remembers whether the previous input was a delimiter and emits the first non-delimiter character of each word.
Observed result
R S C
Understand the operation
This example defines a word as a maximal non-whitespace sequence. isspace handles the chosen C-locale whitespace; repeated delimiters keep the scan at a beginning instead of emitting empty words.
A C string ends at its first null character. Read only within its accessible storage and reserve a position for that terminator when producing a changed string. These examples use basic text bytes, not a Unicode grapheme-processing library.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
const char text[] = "River School Club";
int beginning = 1;
for (size_t i = 0; text[i] != '\0'; i++) {
if (isspace((unsigned char)text[i])) beginning = 1;
else { if (beginning) { printf("%c ", text[i]); beginning = 0; } }
}
putchar('\n');
return 0;
}
Expected output
R S C
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
const char text[] = "River School Club";
int beginning = 1;
for (size_t i = 0; text[i] != '\0'; i++) {
if (isspace((unsigned char)text[i])) beginning = 1;
else { if (beginning) { printf("%c ", text[i]); beginning = 0; } }
}
putchar('\n');
return 0;
}
The beginning flag is reset by each space and cleared after the next first character is emitted.
Compare the printed answer with the original text and the stated ordering or filtering rule. The presence of a terminator makes %s usable; it does not by itself make every earlier write fit.
Follow the worked calculation
Start with the sample
The beginning flag is reset by each space and cleared after the next first character is emitted.
Apply the rule
Compare the printed answer with the original text and the stated ordering or filtering rule. The presence of a terminator makes %s usable; it does not by itself make every earlier write fit.
Check the result
R S C
R S C
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
const char text[] = "River School Club";
int beginning = 1;
for (size_t i = 0; text[i] != '\0'; i++) {
if (isspace((unsigned char)text[i])) beginning = 1;
else { if (beginning) { printf("%c ", text[i]); beginning = 0; } }
}
putchar('\n');
return 0;
}
Use a name with leading and repeated spaces and print only its two initials.
Worked answer
The complete worked answer applies the same explicitly bounded operation to a small variation. Read its stored bytes and terminator separately from the visible output.
C17 · complete program
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
const char text[] = " Asha Rao ";
int beginning = 1;
for (size_t i = 0; text[i] != '\0'; i++) {
if (isspace((unsigned char)text[i])) beginning = 1;
else { if (beginning) { printf("%c ", text[i]); beginning = 0; } }
}
putchar('\n');
return 0;
}
Expected output
A R
Run in C compiler →Recognize the important boundaries
Under this definition a punctuation-leading token begins with punctuation. If the task requires only alphabetic word starts, state and implement that different contract.
An untrusted input string needs a verified buffer bound and a valid terminator. strlen is not a safe way to prove arbitrary raw storage has a terminator.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
A word-start scan remembers whether the previous input was a delimiter and emits the first non-delimiter character of each word.
Continue with character-array storage.
Continue with bounded line reading.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.