Thanks! I did understand it
I understood the whole program and I want to thank anybody! I also decided to challenge myself by writing a program that counts words only on myself! I know how to make it cound lines, tabs, backslashes, etc!BTW, how do I make a backspace?? I have the sequence \b put in the right spot! But what is a backspace in a C program! I pressed the backspace button but still, nothing!
Here is the code for the WORD COUNTING program, not the one with backspace problems!
Code:
#include <stdio.h>
#define OUT 0
#define IN 1
int main()
{
int x, state, nw;
state = OUT;
nw = 0;
while((x = getchar()) != EOF)
{
if (x == ' '|| x == '\n'|| x == '\t')
state = OUT;
else if (state == OUT)
{
state = IN;
++nw;
}
}
printf("You have %d words\n", nw);
}
If you have time, pls consider giving me some critics and commentarries on it and help me improve it!
Thanks agin, To Everybody
Yes, I am doing this just because I want to learn C!
I did want to take some courses but I thought again and I think it is more rewardable(at least for me) to learn C on my own! I am not doing it for money, just pure pleasure and satisfaction!
And one more thing I am going to bother you people with is that I want to make a program that will read input and return output one word per line!
I thought I could do printf("%d\n\n", word)but that would only leave 2 lines! Give me an idea!I really don't know !
Thanks!
Well You can tell me anything u want, I barely know how to progrm!
I am trying to print one word per line from input!
So I suck at c would be:
I
Suck
At
C
That is what I am trying to do!
As I said before, I just started
I know I am very very annoying but I just started learning C! The book gives this exercise in a section of word counting! As far as I got with this book, it never said one thing about ctype.h or isspace() or any functions like those! I just know how to count lines, words, spaces, tabs, copy input to output, and very simple stuff! What I am trying to say is that there has to be a more simple way to do this program! They wouldn't be stupid to give this exercise in the book when it hasn't covered this stuff yet!
So please can you tell me a simpler way of doing this program, even if it is longer! Just a very noob way to take input with getchar() or scanf() and print the output one word on a line!
Thanks!
I tried your code! It did nto compile, I also messed a bit with it!
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, blanks;
state = OUT;
words = lines = tabs = characters = blanks = 0;
while((x = getchar()) != EOF)
{
if (x == '\n')
++lines;
else if (x =='\t')
++tabs;
else if (x == ' ')
++blanks;
else if (x == ' '|| x == '\n'|| x == '\t')
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);
}
}