I am making a program that counts lines, tabs, and blanks! It works just fine with \n for new lines and \t for tabs! When I got to blanks I tried \0 and \b but none of them worked! What is the sequence for a blank?
I am making a program that counts lines, tabs, and blanks! It works just fine with \n for new lines and \t for tabs! When I got to blanks I tried \0 and \b but none of them worked! What is the sequence for a blank?
To make you job easier, you could use isspace() from <ctype.h>. It returns true (non-zero) if its argument is . . . well, a whitespace character. Usually this is spaces, tabs, newlines, formfeeds, etc, but it depends on the locale, I think.
\0 and \b are not considered blank characters, though.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
If you mean a space as blank then use: ' '
I think the OP is looking for a magical sequence like '\z' which represents spaces, tabs, etc. It doesn't exist. An escape sequence can only represent one character.I am making a program that counts lines, tabs, and blanks!
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
I expect they've got an if() statement of the form:
Code:if (c == '\n') { line_count++; } else if (c == '\t') { tab_count++; } else if (c == ' ') { space_count++; }
Last edited by swoopy; 06-14-2007 at 04:32 PM.
Well, maybe. In that case: http://picasso.cs.qc.edu/whitespace.html
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
This is the program:
Thanks! All I needed was the ' ' sequence for blank! It works!Code:#include <stdio.h> main() { int line, tab, blank, x; line = 0; tab = 0; blank = 0; while ((x = getchar()) != EOF) { if (x == '\n') ++line; else if (x == '\t') ++tab; else if (x == ' ') ++blank; } printf("You have %d lines\n", line); printf("You have %d tabs\n", tab); printf("You have %d blanks\n", blank); }
Thanks to everybody for helping
I am not asking you to write the code for me because I want to do it myself! All I need is an explanation of each! I do not understand them!Code:Exercise 1-9. Write a program to copy its input to its output replacing each string of one or more blanks by a single blank. Exercise 1-10. Write a program to copy its input to its output, replacing each tab byt \t, backspace by \b and backslah by \\ ! This makes tabs and backspaces visibile in an unambigous way.
MAYBE I AM STUPID BUT I DO NOT GET WHAT THESE 2 EXERCISES ARE TRYING TO SAY!
PLS HELP
Exercise 1-9 wants you to trim blanks in the input. Thus you would have no extra whitespace.
Exercise is 1-10 is easier to code, but a bit harder to comprehend. If you printed a tab to the screen:
It would display as a certain number of spaces, so you wouldn't know there was a tab in the input. So the exercise wants you to convert the tab to print a \ followed by a t. In C, to print a one \, you actually need \\. And then do the same backspace (\b) and backslash.Code:printf("\t");
Last edited by swoopy; 06-14-2007 at 05:55 PM.
Thanks! What do they mean by a backspace! the backslah works, the tab works, but how do I make it show the backspace! I did put the \b sequence it is all right!
But When I run the program, I press backspace, nothing happenes! What do I have to press to show me the \b! What is the backspace button in this program?????
Here is the code
Code:#include <stdio.h> int main() { int x; while((x = getchar()) != EOF) { if(x == '\t') printf("\\t\n"); else if(x == '\b') printf("\\b\n"); else if(x == '\\') printf("\\\\\n"); } }
getchar() cannot read a backspace(), because it is line buffered. To read a backspace from the user, you'd have to use a non-standard function like getch() or getche(). With Dev-C++, getche() is available in <conio.h>. (It has other quirks, like a newline actually being '\r' instead of '\n'. And if you use getch(), you have to print the character it returns if you want it to be displayed.)
There's an FAQ about getch(), just search for it.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
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
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); } }
I ALSO POSTED THIS IN WORD COUNTING THREAD! U DO NOT NEED TO ANSWER IT IN BOTH, JUST ONE!
Last edited by cookie; 06-17-2007 at 03:48 AM. Reason: forgot to poste the code, and I am dumb
if you go on the red else - the following if will never be trueCode: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; }
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler