homer512 icon

print name

homer512 | PRO | 01/26/17 07:45:42 PM UTC | 0 ⭐ | 754 👁️ | Never ⏰ | []
C |

910 B

|

None

|

0 👍

/

0 👎

#include <stdio.h>
 
 
int main(void)
{
  char first_initial, last_char;
  /*
   * initial command. \n added automatically. No need to use printf and
   * force it to parse a string without any format specifies
   */
  puts("Enter first and last name");
  /*
   * 1. Discard leading whitespace
   * 2. Record the first character
   * 3. Discard the rest of the first word
   */
  if(scanf(" %1c%*[^ \t\n]", &first_initial) != 1)
    return 1;
  /*
   * skip whitespace. This can't be part of the previous scanf because that
   * would fail if the first name is just a single character. It also can't be
   * part of the next scanf or we would concatenate multiple names
   */
  scanf(" ");
  /* Now read everything up to the first whitespace or EOF and print it */
  while(scanf("%1[^\n]", &last_char) == 1)
    putchar(last_char);
  /* print initial */
  printf(", %c\n", first_initial);
  return 0;
}

Comments