Reputation: 47
This program is returning something which I'm not able to comprehend. Attached is the screenshot of the O/P simple program to find number of spaces, tabs, etc.
What am I missing?
#include <stdio.h>
#include <string.h>
int main() {
int count[] = { 0, 0, 0 }; /* 0 is spaces, 1 is tabs and 2 for newline. */
int string;
printf("Enter the paragraph: \n");
while ((string = getchar()) != EOF) {
if (string == ' ')
count[0]++;
else if (string == '\t')
count[1]++;
else if (string == '\n')
count[2]++;
}
printf("There are %d Spaces.\n", count[0]);
printf("There are %d Tabs.\n", count[1]);
printf("There are %d Newlines.\n", count[2]);
return 0;
}
Upvotes: 1
Views: 66
Reputation: 144969
From the screenshot it appears you typed Ctrl-Z to signal the end of file to your program. While this works in legacy systems such as MS/DOS and the Windows terminals, this key combination has a different meaning on unix systems such as linux: it causes the current process to be suspended by its the running shell parent. The process can be resumed later with the fg
command.
To signal the end on file on this system, you should type Ctrl-D instead.
Your program should produce the expected result then. The code seems OK, albeit it is quite confusing to name string
an int
variable that gets a single byte from getchar()
. Such a variable is usually named c
.
Upvotes: 2
Reputation: 409
If you want to send an EOF
in order to stop reading you should use CTRL+D
.
Upvotes: 1