Returns the first non- whitespace character encountered. - C String

C examples for String:char array

Description

Returns the first non- whitespace character encountered.

Demo Code

#include <stdio.h>
#include <ctype.h>

int get_first(void);

int main(void)
{
  int ch;//from  www  .  j av  a 2s.c o  m
  printf("Enter a line:\n");

  ch = get_first();
  printf("%c\n", ch);

  return 0;
}
// returns first non-whitespace character and clears remaining input until next line break or EOF
int get_first(void){
  int ch, garbage;

  do {
    ch = getchar();
  }
  while (isspace(ch));

  while((garbage = getchar()) != '\n' && garbage != EOF)
    ;

  return ch;
}

Result


Related Tutorials