Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9201571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:02:48+00:00 2026-06-17T23:02:48+00:00

My task is: Write a program that reads input up to # and reports

  • 0

My task is:

Write a program that reads input up to # and reports the number of times that the sequence ei occurs.

I wrote something that in most of the times works, but there are inputs when it dosent…

Like this input:(suppose to return 1)

sdlksldksdlskd
sdlsklsdks
sldklsdkeisldksdlk
#
number of combination is: 0

This is the code:

int main(void)

{
    int index = 0;
    int combinationTimes = 0;
    int total = 0;
    char userInput;
    char wordChar[index];

    printf("please enter your input:\n");

    while ((userInput = getchar()) != '#')
    {
        if (userInput == '\n')
            continue;

        wordChar[index] = userInput;
        index++;
        total++;
    }

    for (index = 1; index < total; index++)
    {
        if (wordChar[index] == 'i')
        {
            if (wordChar[--index] == 'e')
            {
                combinationTimes++;
                ++index;
            }
        }
    }

    printf("number of combination is: %d", combinationTimes);

    return 0;
}

Can you please tell me what am I not getting 1 using this input?

in the book he said to test it with “Receive your eieio award” and it worked…but after i played with it a little i see that not always.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:02:50+00:00Added an answer on June 17, 2026 at 11:02 pm

    It really doesn’t seem necessary to read the file into an array. You just need to keep track of how many times ei is found before you read a # or reach EOF:

    #include <stdio.h>
    
    int main(void)
    {
        int c;
        int ei_count = 0;
        while ((c = getchar()) != EOF && c != '#')
        {
            if (c == 'e')
            {
                int c1 = getchar();
                if (c1 == 'i')
                    ei_count++;
                else if (c1 != EOF)
                    ungetc(c1, stdin);
            }
        }
        printf("ei appeared %d times\n", ei_count);
        return(0);
    }
    

    Testing (the program is called ei and is built from ei.c):

    $ ei < ei.c
    ei appeared 0 times
    $ sed 1d ei.c | ei
    ei appeared 1 times
    $ sed 's/#/@/' ei.c | ei
    ei appeared 4 times
    $
    

    The first one stops at the #include line, the second stops at the # in the comparison, and the third reads the entire file. It also gives the correct output for the sample data.


    Analysing the code

    Your primary problem is that you do not allocate any space for the array. Change the dimension of the array from index to, say, 4096. That’ll be big enough for your testing purposes (but really the program should pay attention to the array and not overflowing it — but then I don’t think the array is necessary at all; see the code above).

    The next primary problem is that despite its name, getchar() returns an int, not a char. It can return any valid character plus a distinct value, EOF. So it must return a value that’s bigger than a char. (One of two things happens if you use char. If char is a signed type, some valid character — often ÿ, y-umlaut, U+00FF, LATIN SMALL LETTER Y WITH DIAERESIS — is also treated as EOF even though it is just a character. If char is an unsigned type, then no input matches EOF. Neither is correct behaviour.)

    Fixing that is easy, but your code does not detect EOF. Always handle EOF; the data may be malformatted. That’s a simple fix in the code.

    A tertiary problem is that the printf() statement does not end with a newline; it should.

    Your test condition here is odd:

            if (wordChar[--index] == 'e')
            {
                combinationTimes++;
                ++index;
            }
    

    It’s odd to use one pre-increment and one post-increment, but that’s just a consistency issue.
    Worse, though, is what happens when the character i appears in the input and is not preceded by e. Consider the line @include <stdio.h>: you start with index as 1; that is an i, so you decrement index, but wordChar[0] is not an e, so you don’t increment it again, but the end of the loop does, so the loop checks index 1 again, and keeps on going around the loop testing that the i is i and @ is not e for a long time.

    There’s no reason to decrement and then increment index; just use:

            if (wordChar[index-1] == 'e')
                combinationTimes++;
    

    With those fixed, your code behaves. You trouble was largely that you were using an array that was not big enough (being size 0), and you were overwriting quasi-random memory with the data you were reading.

    #include <stdio.h>
    
    int main(void)
    {
        int index = 0;
        int combinationTimes = 0;
        int total = 0;
        int userInput;
        char wordChar[4096];
    
        printf("please enter your input:\n");
    
        while ((userInput = getchar()) != '#' && userInput != EOF)
        {
            if (userInput == '\n')
                continue;
    
            wordChar[index] = userInput;
            index++;
            total++;
        }
        printf("total: %d\n", total);
    
        for (index = 1; index < total; index++)
        {
            if (wordChar[index] == 'i')
            {
                if (wordChar[index-1] == 'e')
                    combinationTimes++;
            }
        }
    
        printf("number of combination is: %d\n", combinationTimes);
    
        return 0;
    }
    

    Note that you could reasonably write the nested if as:

            if (wordChar[index] == 'i' && wordChar[index-1] == 'e')
                combinationTimes++;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following task: Write a program that asks for a number and
I have been assigned wit the task to write a program that takes a
I have to write program that create process using pipe() . My first task
The task is to write a program which prompts for a filename and then
My task is to write an app(unfortunatly on C) which reads expression in infix
I'm needing to write a program (C#) that will allow the user to create
I'm making a task-based program that needs to have plugins. Tasks need to have
I have a Task that reads strings from a blocking collection and is supposed
I'm trying to write a program that takes a large data frame and replaces
I need to write a simple terminal-based program that should, Read some text from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.