Reads input until encountering the # character and then reports the number of spaces read, the number of newline characters read, and the number of all other characters read. - C String

C examples for String:String Console Input

Description

Reads input until encountering the # character and then reports the number of spaces read, the number of newline characters read, and the number of all other characters read.

Demo Code

#include <stdio.h>
#define STOP '#'//  ww  w . java2 s  .  c o m

int main(void)
{
  char ch;
  unsigned int spaces = 0, newlines = 0, other= 0;

  printf("Enter input (%c to stop):\n", STOP);

  while((ch = getchar()) != STOP){
    if (ch == ' ')
      spaces++;
    else if (ch == '\n')
      newlines++;
    else
      other++;
  }
  printf("\n");
  printf("Character Count:\n");
  printf("Spaces: %u\nNewlines: %u\n Other: %u\n", spaces, newlines, other);

  return 0;
}

Result


Related Tutorials