Reads input up to # and reports the number of times that the sequence ei occurs. - C String

C examples for String:String Console Input

Description

Reads input up to # and reports the number of times that the sequence ei occurs.

Demo Code

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

#define STOP '#'// www . j a  va 2s . c om

int main(void){
  char ch;
  unsigned int ei_count = 0;
  bool e_flag = false;

  printf("Count 'ei' occurs.\n");
  printf("Enter input (%c to stop):\n", STOP);

  while ((ch = getchar()) != STOP){
    ch = tolower(ch);
    if (ch == 'e')
      e_flag = true;
    else if (ch == 'i'){
      if (e_flag)
        ei_count++;
      e_flag = false;
    }
    else
      e_flag = false;

  }

  printf("The sequence 'ei' occurs %u times.\n", ei_count);

  return 0;
}

Result


Related Tutorials