Skip 26. Or better yet, skip printing everything non-printable.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int i;
for ( i = 0; i < 256; ++i )
{
putchar(isprint(i) ? i : '.');
putchar(i % 16 == 15 ? '\n' : ' ');
}
return 0;
}
/* my output
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
! " # $ % & ' ( ) * + , - . /
0 1 2 3 4 5 6 7 8 9 : ; < = > ?
@ A B C D E F G H I J K L M N O
P Q R S T U V W X Y Z [ \ ] ^ _
` a b c d e f g h i j k l m n o
p q r s t u v w x y z { | } ~ .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
[Oops. I see your reply beat my edit.]
I'd use isprint.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int i;
for ( i = 0; i < 128; ++i )
{
putchar(isprint(i) ? i : '.');
putchar(i % 32 == 31 ? '\n' : ' ');
}
return 0;
}
/* my output
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
@ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _
` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ .
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Check out http://www.asciitable.com/
The full list of ASCII characters are there. The function isprint() knows which of the characters are printable. isprint() returns a nonzero value if the input is a printable character, including the space character (0x20 – 0x7E). The other characters are either extended, or do other features which aren't print friendly.
More information on isprint()
isprint() is included in the ctype library, ctype.h:
int isprint(int c);
» This function returns nonzero if c is a space or a character for which either isalnum() or ispunct() returns nonzero.
int isalnum(int c);
» This function returns nonzero if c is any of or other locale-specific alphabetic character.
<em>According to AsciiTable.com</em>
Characters ASCII
a thru z 97 thru 122
A thru Z 65 thru 90
0 thru 9 48 thru 57
int ispunct(int c);
» This function returns nonzero if c is any of the following or other implementation-defined punctuation character:
<em>According to AsciiTable.com</em>
Characters ASCII
! " # $ % & ' ( ) * + , - . / 33 thru 47
: ; < = > ? @ 58 thru 64
[ \ ] ^ _ ' 91 thru 96
{ | } ~ 123 thru 126
Hope this helps,
-Stack Overflow
The greater utility of functions likeispunct and isalnum are that they also work with other character sets, some of which, for example, do not necessarily have a contiguous alphabet or such. They are designed to work regardless of the particular implementation.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
See the attachments, I just want the fourth attachment (Dos, page2) to be like the second one (W32, page2).
DOS has a particular issue with the value 26. Apparently it is an end-of-file marker. So on a DOS system, it will not work the way you'd like. Thus my recommendation to skip it.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314