Blank Sequence

This is a discussion on Blank Sequence within the C Programming forums, part of the General Programming Boards category; I am making a program that counts lines, tabs, and blanks! It works just fine with \n for new lines ...

  1. #1
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Blank Sequence

    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?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    If you mean a space as blank then use: ' '

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I am making a program that counts lines, tabs, and blanks!
    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.
    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.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.

  7. #7
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Thanks Guys and sorry, I forgot to post the code

    This is the program:

    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! All I needed was the ' ' sequence for blank! It works!

    Thanks to everybody for helping

  8. #8
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Unhappy What is The C Programming Language book ask from exercise 1 - 9 and 1-10

    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.
    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!

    MAYBE I AM STUPID BUT I DO NOT GET WHAT THESE 2 EXERCISES ARE TRYING TO SAY!

    PLS HELP

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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:
    Code:
    printf("\t");
    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.
    Last edited by swoopy; 06-14-2007 at 05:55 PM.

  10. #10
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    backspace???????????

    Quote Originally Posted by swoopy View Post
    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:
    Code:
    printf("\t");
    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.
    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");    
    	}
    }

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.

  12. #12
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    I did try your code, does not compile!

    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 &#37;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

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    Code:
    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;
    			}
    if you go on the red else - the following if will never be true
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 11:52 AM
  2. sequence
    By braddy in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 01:15 PM
  3. wsprintf and format specifiers
    By incognito in forum Windows Programming
    Replies: 2
    Last Post: 01-03-2004, 09:00 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Check for a blank cd-r
    By waldis in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2003, 05:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21