Thread: Counting printable and non-printable chars

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    5

    Question Counting printable and non-printable chars

    Hello I am very new to c programming; I am actually an electrical and comp. engineering tech. student.


    I am trying to create a code where the user enters in a series of printable and non-printable characters. With that I have to create a piece of code to discern how many of those entered are printable or non-printable. I was thinking of using the isalpha function.


    This piece of code I have only reads to the first space entered(if any). Basically any sort of constructive hints would be helpful at this point.


    I have been reading the subscribed text(dietel and dietel), but it is really of no use. And I have been watching these videos teaching how to program also, but I am still facing difficulties.

    Yes I do appreciate, that by the length of my code solely, it looks as though I have done nothing, but I assure you I have made multiple attempts, and by the obvious distaste for those who seek out people to write the code for them; that is not what I am asking for. To re-iterate I am just seeking some guidance.

    Code:
    	#include<stdio.h>
    	#include<string.h>
    
    
    	int main(void)
    {
    
    int count=0;
    int i=0;		
    
    
    		while (getchar() !='\n') 
    
    			count++;
    
    		
    
    
    			printf("You entered %d characters.  \a", count);
    			
    	return 0;
    
    	}

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I would suggest the following:

    1. Improve indending.

      In reality, your indenting is terrible. Adjust the preprocessor directives (the stuff beginning with #'s) to be in the farthest left they can be, against the margin. The rest of the code..... pick an indenting style and stick with it consistently:

      http://en.wikipedia.org/wiki/Indent_style

      If you need a reason for taking indenting seriously, it makes the code more readable, which makes it easy to debug and maintain.

      Code:
      #include<stdio.h>
      #include<string.h>
      
      int main(void)
      {
      	int count=0;
      	int i=0;		
      
      	while (getchar() !='\n') 
      	{
      		count++;
      	}	
      	printf("You entered &#37;d characters.  \a", count);
      
      	return 0;
      }
      Isn't that easier to read?
    2. Adjust your loop.

      First create a variable to read a character. Call it something like c and declare it like this:

      Code:
      int c;
      Now the while loop should be something like this:

      Code:
      while(((c = getchar()) != '\n') && (c != EOF))
      Inside the loop, c will be the character that you've read. You can do whatever you wish with it.
    3. Add condition to count.

      You should be counting only if the character is readable. If you want to use isalpha(), go for it, but you should look into isdigit() as well. After that, consider punctuation. You might do better to work on the opposite, and see if the character in question in a whitespace character, and if not, then count it.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dmux5 View Post
    I am trying to create a code where the user enters in a series of printable and non-printable characters. With that I have to create a piece of code to discern how many of those entered are printable or non-printable. I was thinking of using the isalpha function.
    No comment on the code at the moment, but you may want to investigate isprint(), which tells whether a char is... printable.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    5

    Question How to detect non printable as such

    How do I detect non-printable characters, like line terminators ? And differentiate them from printable. I suspect I would need to use two "for" loops. One for each.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    If people are going to take the time to reply to you, the least you can do is take the time to read the replies.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    5

    Question syntax for isdigit

    What is the proper syntax for the function isdigit?

  7. #7

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    Thanks a lot.

Popular pages Recent additions subscribe to a feed