![]() |
VOOZH | about |
In a string that contains multiple words, each word is separated by a whitespace. In this article, we will learn how to print the first letter of each word using a C program.
The simplest method to print the first letter of each word is by using a loop. Let’s take a look at an example:
H G W t C p
Explanation: We traverse the string, and every time we find a non-space character that follows a space (or is at the start of the string), we print that character.
There are also a few other methods in C to print the first letter of each word. They are as follows:
Another approach is to use the strtok()function, which breaks a string into tokens based on delimiters. We can use space ' ' as the delimiter to extract each word from the string and print the first character of each word.
H G W t C p
We can also usesscanf() to read individual words from the string, one by one, and print the first letter of each word. This approach works by scanning each word and extracting the first character.
H G W t C p