💡⚡Beginner
Find the Initials of a Name
Extract name initials
Extract initials from a name (first letter of each word).
Program Code
find_initials.c
C
1#include <stdio.h>2#include <ctype.h>3#include <string.h>45void getInitials(char name[], char initials[]) {6 int j = 0;7 8 // First letter is always an initial9 if (name[0] != ' ') {10 initials[j++] = toupper(name[0]);11 }12 13 // Find first letter after each space14 for (int i = 1; name[i]; i++) {15 if (name[i - 1] == ' ' && name[i] != ' ') {16 initials[j++] = toupper(name[i]);17 initials[j++] = '.';18 }19 }20 21 // Add dot after first initial if more than one22 if (j > 1) {23 // Shift and add dot after first24 for (int i = j; i > 1; i--) {25 initials[i] = initials[i - 1];26 }27 initials[1] = '.';28 j++;29 }30 initials[j] = '\0';31}3233int main() {34 char name[] = "John Michael Smith";35 char initials[20];36 37 getInitials(name, initials);38 39 printf("Name: %s\n", name);40 printf("Initials: %s\n", initials);41 42 return 0;43}Output
Name: John Michael Smith
Initials: J.M.S.
Related Examples
Want to Learn More?
Explore our comprehensive tutorials for in-depth explanations of C programming concepts.
Browse Tutorials