Printing "tab"

M

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

Tom St Denis

mdh said:
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
 
M

mdh

Tom said:
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 :)
 
T

Tom St Denis

mdh said:
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
 
M

mdh

Tom said:
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.
 
T

Tom St Denis

mdh said:
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
 
M

mdh

Tom said:
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.
 
M

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

Thomas J. Gritzan

mdh said:
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
 
M

Mike S

Thomas said:
mdh said:
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]
 
M

Mike S

Mike said:
Thomas said:
mdh said:
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>
 
M

mdh

Mike said:
<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.
 
C

CBFalconer

mdh said:
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

Andrew Poelstra

This what I have come up with:


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

mdh

Tom said:
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"?
 
M

mdh

Andrew said:
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!!
 
A

Andrew Poelstra

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).
 
M

mdh

CBFalconer said:
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.
 
M

mdh

CBFalconer said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,529
Latest member
JaclynShum

Latest Threads

Top