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.
It really doesn’t seem necessary to read the file into an array. You just need to keep track of how many times
eiis found before you read a#or reach EOF:Testing (the program is called
eiand is built fromei.c):The first one stops at the
#includeline, 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
indexto, 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 anint, not achar. It can return any valid character plus a distinct value, EOF. So it must return a value that’s bigger than achar. (One of two things happens if you usechar. Ifcharis 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. Ifcharis 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:
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
iappears in the input and is not preceded bye. Consider the line@include <stdio.h>: you start withindexas 1; that is ani, so you decrement index, butwordChar[0]is not ane, so you don’t increment it again, but the end of the loop does, so the loop checksindex1 again, and keeps on going around the loop testing that theiisiand@is notefor a long time.There’s no reason to decrement and then increment
index; just use: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.
Note that you could reasonably write the nested
ifas: