Read the user's first name and last name then print the entered names on one line and the number of letters in each name on the following line. - C String

C examples for String:String Console Input

Introduction

Align each letter count with the end of the corresponding name.

Demo Code

#include <stdio.h>
#include <string.h>

int main(void)
{
  char first_name[20];
  char last_name[20];

  printf("Enter your first and last name: ");
  scanf("%s %s", first_name, last_name);
  printf("\n");/*from w  ww  .j  av a2 s .  co  m*/
  printf("%s %s\n", first_name, last_name);
  printf("%*lu %*lu\n", // right justified
       (int) strlen(first_name), strlen(first_name),
       (int) strlen(last_name), strlen(last_name));
  printf("\n");
  printf("%s %s\n", first_name, last_name);
  printf("%-*lu %-*lu\n", // left justified
       (int) strlen(first_name), strlen(first_name),
       (int) strlen(last_name), strlen(last_name));
  printf("\n");

  return 0;
}

Result


Related Tutorials