luc075.c (1990B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 08 Feb 2026 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* Write a program that stores a set of names of individuals and abbreviates the first and middle name to their first letter. 9 */ 10 11 /* Let Us C, Chap- 16 (Handling Multiple Strings), Qn No.: A(d) */ 12 13 /* This file is auto-generated by a bot. */ 14 /* This code is not compiled; it is for reference only. */ 15 16 17 #include <stdio.h> 18 #include <string.h> 19 #include <stdlib.h> 20 #include <ctype.h> 21 22 int main() 23 { 24 char names[5][50]; 25 char abbr[50]; 26 int i, j, k, len, space_count; 27 28 printf("Enter 5 full names (First Middle Last):\n"); 29 for (i = 0; i < 5; i++) 30 { 31 printf("Name %d: ", i + 1); 32 gets(names[i]); 33 } 34 35 printf("\nAbbreviated Names:\n"); 36 for (i = 0; i < 5; i++) 37 { 38 len = strlen(names[i]); 39 space_count = 0; 40 k = 0; 41 42 // Add first initial 43 abbr[k++] = names[i][0]; 44 abbr[k++] = '.'; 45 abbr[k++] = ' '; 46 47 // Find spaces to get subsequent parts 48 for (j = 0; j < len; j++) 49 { 50 if (names[i][j] == ' ') 51 { 52 space_count++; 53 if (space_count == 1) // Found start of Middle name 54 { 55 abbr[k++] = names[i][j+1]; 56 abbr[k++] = '.'; 57 abbr[k++] = ' '; 58 } 59 else if (space_count == 2) // Found start of Last name 60 { 61 // Copy the rest of the last name 62 int m = j + 1; 63 while (names[i][m] != '\0') 64 { 65 abbr[k++] = names[i][m++]; 66 } 67 // Stop searching 68 break; 69 } 70 } 71 } 72 abbr[k] = '\0'; 73 printf("%s\n", abbr); 74 } 75 76 return 0; 77 }