≡ Menu

Read string from stdin using fgets

#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] = '';

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

  return 0;
}

Comments on this entry are closed.