Print how many times a sequence of characters appears in a string Given a string, count how many times a sequence of characters (in this code, the sequence is "ei") appears until the user types '#', then print the amount. I would like to know how to improve the code.

#include &lt;stdio.h&gt;
#include &lt;inttypes.h&gt;

int main(void)
{
    char ch, cur_ch, last_ch;
    cur_ch = '\0';
    uint16_t count = 0;

    puts("Enter a string (# to finish):");
    while ((ch = getchar()) != '#')
    {
        last_ch = cur_ch;
        cur_ch = ch;

        if (last_ch == 'e' &amp;&amp; cur_ch == 'i')
            count++;
    }

    printf("There %s %" PRIu16 " %s.\n", count == 1 ? "is" : "are", count,
    count == 1 ? "ei" : "ei's");

    return 0;
}
Missed detection of EOF: getchar() returns an int with values in the range of unsigned char and the negative value of EOF.  Saving that result in a char loses distinctiveness should getchar() return EOF due to stdin becoming closed or input error.

//char ch;
//while ((ch = getchar()) != '#')
int ch;
while ((ch = getchar()) != '#' &amp;&amp; ch != EOF)

Why uint16_t?  The input stream can be excessively long, and certainly more than 65,535.  Suggest a wider type.  (Good that code uses an unsigned integer.)

// uint16_t count = 0;
unsigned long long  = 0;

Code could do this with 2 variables rather than 3.

int ch;
previous = EOF;

while ((ch = getchar()) != '#' &amp;&amp; ch != EOF) {
  if (previous == `e` &amp;&amp; ch == `i') {
    count++;
  }
  previous = ch;
}

Recommend {}

// if (last_ch == 'e' &amp;&amp; cur_ch == 'i')
//     count++;
if (last_ch == 'e' &amp;&amp; cur_ch == 'i') {
    count++;
}

Grammar: Code goes though work to print are vs. is yet does not make correct with ei's vs. ei.