Use fgets to read string from standard input : Console Read String « Console « C / ANSI-C





Use fgets to read string from standard input

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

int main(void)
{
  char str[80];
  int i;

  printf("Enter a string: ");
  fgets(str, 10, stdin);

  /* remove newline, if present */
  i = strlen(str)-1;
  if( str[ i ] == '\n') 
      str[i] = '\0';

  printf("This is your string: %s", str);

  return 0;
}


           
       










Related examples in the same category

1. Get a string from stdin: how to use gets
2. Read formatted data from string: how to use sscanf
3.Get string from console and output it
4.Get string from console and assign it to a char pointer
5.Read string: A simple scanf and printf pair