I also tried to understand it! But since I am a very beggining person, I could not understand much of it! One day passed, I wrote no code! I felt like I forgot anything I learned up to that point so I decided to write a program that includes all the others I've done! But the program doesn't work!
The problem with this is that it keeps telling me I have 1 word even if I write whole pages! One word if I type anything, 0 words if I type nothing!
As you see, I have an integer called characters! That was supposed to count the characters in my text! I forgot that also!
Here is the code, it is driving me crazy and it is on my nerves since I can't figure out the problem why it tells me I have 1 word when I have many!
I would also appreciate some help on character counting
I also posted this on another thread, my mistake as it is 3:23AM when I am writing this!
Code:
#include <stdio.h>
#define OUT 0
#define IN 1
int main()
{
/* while(1)
{
*/
int x, state, words, lines, tabs, characters, char_num, letter_num, blanks;
state = OUT;
words = lines = tabs = characters = blanks = char_num = letter_num = 0;
while((x = getchar()) != EOF)
{
++ char_num; /* counts all char's, of any kind */
if (x == '\n')
++lines;
else if (x =='\t')
++tabs;
else if (x == ' ')
++blanks;
else
++letter_num; /* counts just the letters */
if (x == ' '|| x == '\n'|| x == '\t') /* no else on this line */
state = OUT;
else if (state == OUT)
{
state = IN;
++words;
}
}
printf("You have %d words:\n", words);
printf("You have %d lines:\n", lines);
printf("You have %d tabs :\n", tabs);
printf("You have %d blanks:\n", blanks);
printf("You have %d characters\n", char_num);
printf(" and %d letters.\n", letter_num);
*/ }
*/
}