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?
Printable View
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.
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.Quote:
I am making a program that counts lines, tabs, and blanks!
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++;
}
Well, maybe. In that case: http://picasso.cs.qc.edu/whitespace.html
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");
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.
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!
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;
}