fork download
  1. //Q98. Print initials of a name with the surname displayed in full.
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int main() {
  6. char str[100];
  7. int i, len;
  8. gets(str);
  9. len = strlen(str);
  10.  
  11. // Print initials
  12. if(str[0] != ' ')
  13. printf("%c.", str[0]);
  14. for(i = 0; i < len; i++)
  15. if(str[i] == ' ' && str[i+1] != ' ' && str[i+1] != '\0' && i < len-1)
  16. if(strchr(str+i+1, ' ') == NULL) break;
  17. else printf("%c.", str[i+1]);
  18.  
  19. // Print surname (last word)
  20. while(i < len && str[i] == ' ') i++;
  21. printf("%s", str + i);
  22. }
  23.  
Success #stdin #stdout 0s 5320KB
stdin
John David Doe
stdout
J.D.Doe