By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,152 Members | 2,193 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,152 IT Pros & Developers. It's quick & easy.

Printing "tab"

P: n/a
mdh
I wrote a little insignificant program, to help me write a more
significant one!!...that was supposed to print all Ascii values from 0
to 127:

#include <stdio.h>
# define UPPER_LIMT 127

int main (){

int i;

printf("%7s%7s\n\n", "Decimal", "Value");
for ( i=0; i<= UPPER_LIMT; i++)
printf( "%3d%7c\n", i, i);
return 0;
}


However, I would like to see, for example, 'TAB' instead of an
upside-down question mark, at decimal value '9'. Is there a
conversion I could use for this in printf?

Thanks in advance.

Jun 16 '06 #1
Share this Question
Share on Google+
25 Replies


P: n/a

mdh wrote:
I wrote a little insignificant program, to help me write a more
significant one!!...that was supposed to print all Ascii values from 0
to 127:
>
#include <stdio.h>
# define UPPER_LIMT 127

int main (){

int i;

printf("%7s%7s\n\n", "Decimal", "Value");
for ( i=0; i<= UPPER_LIMT; i++)
printf( "%3d%7c\n", i, i);
return 0;
}
>>


However, I would like to see, for example, 'TAB' instead of an
upside-down question mark, at decimal value '9'. Is there a
conversion I could use for this in printf?


Homework.

Hint: consider a function which accepts a char as input and returns a
char pointer.

Tom

Jun 16 '06 #2

P: n/a
mdh
Tom St Denis wrote:
Hint: consider a function which accepts a char as input and returns a
char pointer.

oh oh....I am on page 77 of K&R...can't wait to get to page 93
"Pointers and Arrays"!!! But, if you change your hint to avoid
pointers...I will byte :-)

Jun 16 '06 #3

P: n/a

mdh wrote:
Tom St Denis wrote:
Hint: consider a function which accepts a char as input and returns a
char pointer.

oh oh....I am on page 77 of K&R...can't wait to get to page 93
"Pointers and Arrays"!!! But, if you change your hint to avoid
pointers...I will byte :-)


Use a switch or if statement then.

if (x == '\t') printf("tab");
else printf("%c", x);

.....

Look ma, no thought!

Tom

Jun 16 '06 #4

P: n/a
mdh

Tom St Denis wrote:
Tom St Denis wrote:

Use a switch or if statement then.

Yes...I thought as much...I was wondering if I could get away without a
long switch statement, but I guess not. Thanks for your input.

Jun 16 '06 #5

P: n/a

mdh wrote:
Tom St Denis wrote:
Tom St Denis wrote:

Use a switch or if statement then.

Yes...I thought as much...I was wondering if I could get away without a
long switch statement, but I guess not. Thanks for your input.


well you're gonna have a huge "default" grouping though. As basically
32-126 are printable on most CP437 terminals.

Of course for extra credit you could detect the current code page and
substitute as appropriate :-)

Tom

Jun 16 '06 #6

P: n/a
mdh

Tom St Denis wrote:
Of course for extra credit you could detect the current code page and
substitute as appropriate :-)


I will take a rain check on that or now :-)

Thank you for you help.

Jun 16 '06 #7

P: n/a
mdh


Use a switch or if statement then.

if (x == '\t') printf("tab");
else printf("%c", x);


This what I have come up with:
>
#include <stdio.h>
# define UPPER_LIMT 127
# define NOT_CHAR 1
# define IS_CHAR 0
# define MAXARR 10
int itochar ( char s[], char i);
int main (){

int i;
char s[MAXARR];

printf("%7s%7s\n\n", "Decimal", "Value");

for ( i=0; i<= UPPER_LIMT; i++)

(IS_CHAR==itochar(s, i)) ? printf("%3d %3c\n", i, i) : printf("%3d
%3s\n", i, s);

return (0);

}
/**********/

int itochar ( char s[], char i){

switch (i) {

case '\t':

s = "TAB";

return NOT_CHAR;

case '\n':

s = "NEWLINE";

return NOT_CHAR;

case '\b':

s = "BACKSPACE";

return NOT_CHAR;

case '\r':

s = "CARRIAGE RETURN";

return NOT_CHAR;

default:

return IS_CHAR;

}

>


My problem is that even though the function " itochar" recognizes a tab
or newline, and even though initially NOT_CHAR is "returned", the
default "IS_CHAR" is then subsequently returned. Please be easy. Any
insight would be appreciated.

Jun 16 '06 #8

P: n/a
mdh schrieb:
int main (){

int i;
char s[MAXARR];

printf("%7s%7s\n\n", "Decimal", "Value");

for ( i=0; i<= UPPER_LIMT; i++)

(IS_CHAR==itochar(s, i)) ? printf("%3d %3c\n", i, i) : printf("%3d
%3s\n", i, s);

return (0);

}
/**********/

int itochar ( char s[], char i){
This is the same as:
int itochar(char* s, char i) {
switch (i) {

case '\t':

s = "TAB";
Here you change the local variable s that it points to another string.
The char array s in function main is not affected. You have to copy the
string into the array:

strcpy(s, "TAB");
My problem is that even though the function " itochar" recognizes a tab
or newline, and even though initially NOT_CHAR is "returned", the
default "IS_CHAR" is then subsequently returned. Please be easy. Any
insight would be appreciated.


see above.

Thomas
Jun 16 '06 #9

P: n/a

Thomas J. Gritzan wrote:
mdh schrieb:
int main (){

int i;
char s[MAXARR];

printf("%7s%7s\n\n", "Decimal", "Value");

for ( i=0; i<= UPPER_LIMT; i++)

(IS_CHAR==itochar(s, i)) ? printf("%3d %3c\n", i, i) : printf("%3d
%3s\n", i, s);

return (0);

}
/**********/

int itochar ( char s[], char i){


This is the same as:
int itochar(char* s, char i) {
switch (i) {

case '\t':

s = "TAB";


Here you change the local variable s that it points to another string.
The char array s in function main is not affected. You have to copy the
string into the array:

strcpy(s, "TAB");


Also, it would be a good idea to increase MAXARR to accomodate the
largest string you're going to put into s, otherwise when you get to

strcpy(s, "CARRIAGE RETURN");

you will overflow s and write into memory that isn't part of the array,
which is bad. Set MAXARR equal to at least 16 (i.e. the number of
characters in "CARRIAGE RETURN" plus room for the terminating '\0'
character), and larger if you want to accomodate longer strings. If you
copy a string somewhere, that somewhere needs to be big enough to hold
the entire string plus one extra slot for the '\0' character.

[snip]

--
Mike S

Jun 16 '06 #10

P: n/a

Mike S wrote:
Thomas J. Gritzan wrote:
mdh schrieb:
int main (){

int i;
char s[MAXARR];

printf("%7s%7s\n\n", "Decimal", "Value");

for ( i=0; i<= UPPER_LIMT; i++)

(IS_CHAR==itochar(s, i)) ? printf("%3d %3c\n", i, i) : printf("%3d
%3s\n", i, s);

return (0);

}
/**********/

int itochar ( char s[], char i){


This is the same as:
int itochar(char* s, char i) {
switch (i) {

case '\t':

s = "TAB";


Here you change the local variable s that it points to another string.
The char array s in function main is not affected. You have to copy the
string into the array:

strcpy(s, "TAB");


Also, it would be a good idea to increase MAXARR to accomodate the
largest string you're going to put into s, otherwise when you get to

strcpy(s, "CARRIAGE RETURN");

you will overflow s and write into memory that isn't part of the array,

[snip]
<afterthought> You need to #include <string.h> if you want to use
strcpy. </afterthought>

--
Mike S

Jun 16 '06 #11

P: n/a
mdh

Mike S wrote:
<afterthought> You need to #include <string.h> if you want to use
strcpy. </afterthought>


thank you Mike.

I also realize that my error was

s="etc etc"

as opposed to

s[0]='t';
s[1]='....etc etc'

Thank you...I have not used strcpy...but certainly makes it easier.

Jun 16 '06 #12

P: n/a
mdh

Thomas J. Gritzan wrote:
Tks Thomas, appreciated.

Jun 16 '06 #13

P: n/a
On 2006-06-16, mdh <md**@comcast.net> wrote:
Use a switch or if statement then.

if (x == '\t') printf("tab");
else printf("%c", x);


This what I have come up with:
>>
<snip ill-formatted code>
>>


My problem is that even though the function " itochar" recognizes a tab
or newline, and even though initially NOT_CHAR is "returned", the
default "IS_CHAR" is then subsequently returned. Please be easy. Any
insight would be appreciated.

Two things:
1) You don't really need a function for this
2) Never underestimate formatting on Usenet.

This problem looks like it would be best solved using pointers:

#include <stdio.h>
#include <stdlib.h>
#define MAX_LIMIT 127

int main (void)
{
char *c[MAX_LIMIT];
char *special[] = {"(Tab)", "(Backspace)", "(Bell)", "(Null)", "(Space)"};
char ctr = 0;

/* Init char listing */
while (ctr < MAX_LIMIT)
{
switch (ctr)
{
case '\t':
c[ctr] = special[0];
break;
case '\b':
c[ctr] = special[1];
break;
case '\a':
c[ctr] = special[2];
break;
case '\0':
c[ctr] = special[3];
break;
case ' ':
c[ctr] = special[4];
break;
/* Whatever other cases you want here. */
default:
c[ctr] = malloc (2);
sprintf (c[ctr], "%c", ctr);
}

ctr++;
}

/* Display listing */
for (ctr = 0; ctr < MAX_LIMIT; ctr++)
printf ("%d: %s\n", ctr, c[ctr]);

return 0;
}
This is easily extensible; just add any other special texts to the special[]
array, and add a corresponsing case in the switch.

You should add code to check for malloc() failures, and there's a case for
replacing my while loop with a for.

I'm sorry that you haven't learned pointers yet; perhaps when you get to them,
you should come back to my solution and see how it works. It doesn't appear
to me that you could elegantly replace text like you want without pointers.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
Jun 16 '06 #14

P: n/a
mdh

Tom St Denis wrote:
Use a switch or if statement then.

if (x == '\t') printf("tab");
else printf("%c", x);


I have ended up using this:

int itochar ( char s[], char i){

switch (i) {

case '\t':

strcpy(s, "HORIZONTAL TAB");

return NOT_CHAR;


Is there a way to capture, for example, "Delete", "Escape", "Cancel"?

Jun 16 '06 #15

P: n/a
mdh
Andrew Poelstra wrote:
I'm sorry that you haven't learned pointers yet; perhaps when you get to them,
you should come back to my solution and see how it works. It doesn't appear
to me that you could elegantly replace text like you want without pointers.


Thanks Andrew...will do...and thanks for the insight. I will bank it
for a few more pages!!

Jun 16 '06 #16

P: n/a
mdh wrote:

I wrote a little insignificant program, to help me write a more
significant one!!...that was supposed to print all Ascii values from 0
to 127:

#include <stdio.h>
# define UPPER_LIMT 127

int main (){

int i;

printf("%7s%7s\n\n", "Decimal", "Value");
for ( i=0; i<= UPPER_LIMT; i++)
printf( "%3d%7c\n", i, i);
return 0;
}

However, I would like to see, for example, 'TAB' instead of an
upside-down question mark, at decimal value '9'. Is there a
conversion I could use for this in printf?


#include <limits.h>
....
unsigned int ch;
for (ch = 0; ch < UCHAR_MAX; ch++) {
if (isprint(ch) {
/* output the printing char as you please */
}
else {
/* deal with the non-printing char */
}
}

--
"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Jun 16 '06 #17

P: n/a
On 2006-06-16, mdh <md**@comcast.net> wrote:

Tom St Denis wrote:
Use a switch or if statement then.

if (x == '\t') printf("tab");
else printf("%c", x);
I have ended up using this:

int itochar ( char s[], char i){

switch (i) {

case '\t':

strcpy(s, "HORIZONTAL TAB");

return NOT_CHAR;


You'd better make sure that s[] can hold "HORIZONTAL TAB".

Is there a way to capture, for example, "Delete", "Escape", "Cancel"?


Not portably. You can grab an ASCII table and figure it out for most
desktop machines (hint: chars are integers).
--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
Jun 16 '06 #18

P: n/a
mdh

CBFalconer wrote:

unsigned int ch;
for (ch = 0; ch < UCHAR_MAX; ch++) {
if (isprint(ch) {
/* output the printing char as you please */
}
else {
/* deal with the non-printing char */
}
}

Nice...implemeted it...works nicely..thank you.

Jun 17 '06 #19

P: n/a
mdh
CBFalconer wrote:

unsigned int ch;
for (ch = 0; ch < UCHAR_MAX; ch++) {
if (isprint(ch) {
/* output the printing char as you please */
}
else {
/* deal with the non-printing char */
}
}

Nice...implemented it...works nicely..thank you.

Jun 17 '06 #20

P: n/a
mdh wrote:
I wrote a little insignificant program, to help me write a more
significant one!!...that was supposed to print all Ascii values from 0
to 127:
#include <stdio.h>
# define UPPER_LIMT 127

int main (){

int i;

printf("%7s%7s\n\n", "Decimal", "Value");
for ( i=0; i<= UPPER_LIMT; i++)
printf( "%3d%7c\n", i, i);
return 0;
}
However, I would like to see, for example, 'TAB' instead of an
upside-down question mark, at decimal value '9'. Is there a
conversion I could use for this in printf?

Thanks in advance.


A 128-entry conversion table would do it. The entries are
strings. At decimal enter "TAB", at 32 enter "BLANK", etc.
Print the strings in the table.
--
Julian V. Noble
Professor Emeritus of Physics
University of Virginia
Jun 17 '06 #21

P: n/a

mdh wrote:
I wrote a little insignificant program, to help me write a more
significant one!!...that was supposed to print all Ascii values from 0
to 127:
>
#include <stdio.h>
# define UPPER_LIMT 127

int main (){

int i;

printf("%7s%7s\n\n", "Decimal", "Value");
for ( i=0; i<= UPPER_LIMT; i++)
printf( "%3d%7c\n", i, i);
return 0;
}
>>


However, I would like to see, for example, 'TAB' instead of an
upside-down question mark, at decimal value '9'. Is there a
conversion I could use for this in printf?

Thanks in advance.


Jun 17 '06 #22

P: n/a
CBFalconer wrote:
mdh wrote:
I wrote a little insignificant program, to help me write a more
significant one!!...that was supposed to print all Ascii values from 0
to 127:

#include <stdio.h>
# define UPPER_LIMT 127

int main (){

int i;

printf("%7s%7s\n\n", "Decimal", "Value");
for ( i=0; i<= UPPER_LIMT; i++)
printf( "%3d%7c\n", i, i);
return 0;
}

However, I would like to see, for example, 'TAB' instead of an
upside-down question mark, at decimal value '9'. Is there a
conversion I could use for this in printf?


#include <limits.h>
...
unsigned int ch;
for (ch = 0; ch < UCHAR_MAX; ch++) {
if (isprint(ch) {
/* output the printing char as you please */
}
else {
/* deal with the non-printing char */
}
}


I wrote this several years ago. It may be of interest here. Note that
ASCII 0..31 are controls and not printable. Also 127 is not printable.

#include <stdio.h>
/*
The complete ASCII table is defined so that this program might
run as well on a machine whose native character set is not ASCII.
*/
char *ascii[] = {
"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
"BS ", "HT ", "LF ", "VT ", "FF ", "CR ", "SO ", "SI ",
"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
"CAN", "EM ", "SUB", "ESC", "FS ", "GS ", "RS ", "US ",
" ", " ! ", " \" "," # ", " $ ", " % ", " & ", " ' ",
" ( ", " ) ", " * ", " + ", " , ", " - ", " . ", " / ",
" 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 ", " { ", " | ", " } ", " ~ ", "DEL"
};
int main(void) {
int i;
size_t asiz = sizeof ascii / sizeof *ascii;
for (i = 0; i < asiz; ++i) {
if (i % 8 == 0) printf("\n|");
printf("%3d %s|", i, ascii[i]);
}
return 0;
}
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 17 '06 #23

P: n/a
Joe Wright <jo********@comcast.net> wrote:
I wrote this several years ago. It may be of interest here. Note that
ASCII 0..31 are controls and not printable. Also 127 is not printable.

#include <stdio.h>
/*
The complete ASCII table is defined so that this program might
run as well on a machine whose native character set is not ASCII.
*/
char *ascii[] = {
"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
"BS ", "HT ", "LF ", "VT ", "FF ", "CR ", "SO ", "SI ",
"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
"CAN", "EM ", "SUB", "ESC", "FS ", "GS ", "RS ", "US ",
" ", " ! ", " \" "," # ", " \u0024 ", " % ", " & ", " ' ",
" ( ", " ) ", " * ", " + ", " , ", " - ", " . ", " / ",
" 0 ", " 1 ", " 2 ", " 3 ", " 4 ", " 5 ", " 6 ", " 7 ",
" 8 ", " 9 ", " : ", " ; ", " < ", " = ", " > ", " ? ", " \u0040 ", " 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 ", " [ ", " \\ "," ] ", " ^ ", " _ ", " \u0060 ", " 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 ", " { ", " | ", " } ", " ~ ", "DEL"
};


Three of the characters in your table are not in the basic execution
character set.
Jun 17 '06 #24

P: n/a
Jordan Abel wrote:
Joe Wright <jo********@comcast.net> wrote:
I wrote this several years ago. It may be of interest here. Note that
ASCII 0..31 are controls and not printable. Also 127 is not printable.

#include <stdio.h>
/*
The complete ASCII table is defined so that this program might
run as well on a machine whose native character set is not ASCII.
*/
char *ascii[] = {
"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
"BS ", "HT ", "LF ", "VT ", "FF ", "CR ", "SO ", "SI ",
"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
"CAN", "EM ", "SUB", "ESC", "FS ", "GS ", "RS ", "US ",
" ", " ! ", " \" "," # ",

" \u0024 ",
" % ", " & ", " ' ",
" ( ", " ) ", " * ", " + ", " , ", " - ", " . ", " / ",
" 0 ", " 1 ", " 2 ", " 3 ", " 4 ", " 5 ", " 6 ", " 7 ",
" 8 ", " 9 ", " : ", " ; ", " < ", " = ", " > ", " ? ",

" \u0040 ",
" 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 ", " [ ", " \\ "," ] ", " ^ ", " _ ",

" \u0060 ",
" 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 ", " { ", " | ", " } ", " ~ ", "DEL"
};


Three of the characters in your table are not in the basic execution
character set.


And what exactly does that mean to you? '@' and '`' are printable,
aren't they? That the ASCII 'CAN' control is not in the basic execution
character set is indeed surprising. NOT!

I must have missed something. What is your point?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 18 '06 #25

P: n/a
Joe Wright <jo********@comcast.net> wrote:
And what exactly does that mean to you? '@' and '`' are printable,
aren't they? That the ASCII 'CAN' control is not in the basic execution
character set is indeed surprising. NOT!
You didn't include the ascii CAN control in a string literal, only the
letters C, A, and N.
I must have missed something. What is your point?

Jun 18 '06 #26

This discussion thread is closed

Replies have been disabled for this discussion.