6/26/2008

2008_0626 Exercise 1-8

2008_0626 Exercise 1-8
3rd try
-----------------------------------------------------
#include <stdio.h>
/*initialize the varibles*/
main()
{
int c,b,t,n;

b = 0;
t = 0;
n = 0;

printf ("Please enter a sentence for testing");

/*start the counting*/
while ((c = getchar()) != EOF)
if (c == ' ')
++b;
else if (c == 't')
++t;
else if (c == 'n')
++n;

/*output the result*/
printf("blank(s) %dntabs(s) %dnnewline(s) %d", b,t,n);
system("pause");
}
-----------------------------------------------------
After emailed Mr. Kimiya,I added a system("pause"); at the bottom of this program,and whe '=' should replace by '=='(although it doesn't seem to be so neccesary,these two signs are different, '=' means evaluating while '==' means that it would check if the two sides of it are equal )
When running it,I finally got my goal - Counting!
but I should give some questions:
1 ---> why should I add a system("pause"); in the bottom?According to Mr. Kimiya,it seems to be something to do with getchar(),and I have checked the text book,it says that getchar() can read the next character in a character stream,but I don't really have any idea of this bold word 'next',maybe when working,getchar() can only hold words in the next charactor,thus,when it comes to the end of the charactor stream,it still want to hold words from the next,while there's no next charators any more.But what about EOF?We get an EOF at the end of a charactor stream,making getchar() stop picking up chactors!!!I don't know what's wrong,but I will remain it for myself and solving in in the future. Questions Unsolved
2 ---> when the .exe file,I should type in 'ctrl+Z' twice,only by this,can I get the result display,but I really don't know why should I do like this.Although Mr. Kimiya says that this is also something to do with getchar(),I still don't know why.Perhaps,this is something to do with the 'input and output',well,if so,I will get the answer when I get to this unit ^_^

No comments:

Post a Comment