Advertisement
homer512

print name

Jan 26th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.91 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3.  
  4. int main(void)
  5. {
  6.   char first_initial, last_char;
  7.   /*
  8.    * initial command. \n added automatically. No need to use printf and
  9.    * force it to parse a string without any format specifies
  10.    */
  11.   puts("Enter first and last name");
  12.   /*
  13.    * 1. Discard leading whitespace
  14.    * 2. Record the first character
  15.    * 3. Discard the rest of the first word
  16.    */
  17.   if(scanf(" %1c%*[^ \t\n]", &first_initial) != 1)
  18.     return 1;
  19.   /*
  20.    * skip whitespace. This can't be part of the previous scanf because that
  21.    * would fail if the first name is just a single character. It also can't be
  22.    * part of the next scanf or we would concatenate multiple names
  23.    */
  24.   scanf(" ");
  25.   /* Now read everything up to the first whitespace or EOF and print it */
  26.   while(scanf("%1[^\n]", &last_char) == 1)
  27.     putchar(last_char);
  28.   /* print initial */
  29.   printf(", %c\n", first_initial);
  30.   return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement