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