Home > database >  Creating an ASCII table with C and the columns should have a tab between them
Creating an ASCII table with C and the columns should have a tab between them

Time:07-01

since this my first post, let's see how this goes. Let me introduce the problem first:

  1. Create an ASCII table that is 4 "columns" or "cells" wide.
  2. The table should include a given range of ASCII dec values, no scanf needed
  3. The range can be for example 40-56, 95-107, 20-27
  4. Every column should have tab between them, but not the last one, so a FULL 4-wide row cannot end to an tab, in every othercase it should
  5. Each "cell" should contain the given ASCII dec value, a hexadecimal value of that dec number and the corresponding ASCII character itself, if it's printable, in other cases a "?".

The idea is to do the task with if and less and with loops, for example, with for or while loops.

So here's my code that's works for the parts 1, 2, 3 and 5, and partly for the 4th too. I'll explain the issue with the outputs.

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main()
{
    int min=28;                         //give the starting point of the ASCII dec range
    int max=40;                         //give the ending point of the ASCII dec range
    

    int i, j;                           //these are used to produce rows and columns

    for(i=min; i<=max; i=i 4)           //create a four "cells" wide row
    {
        for(j=i; j<=i 2 && j<max; j  )  //print the first three values for the row
        {
            if(isprint(j)){             //print the ASCII character if it's printable, if not, print question mark                         
                printf("= 0xx %c\t", j, j ,j);  
            }else {
                printf("= 0xx ?\t",j, j);
            }
        }
        if(isprint(j 3))                //print the last value for each row
        {
            printf("= 0xx %c", j, j ,j);
        }else {
            printf("= 0xx ?",j, j);
        }
        
        printf("\n");                     //once a row has the max 4 "cells", start a new row and repeat the process
    }
    return 0;
}

And then this gives with "inputs" 28-40 this:

28 0x1c ?    29 0x1d ?   30 0x1e ?   31 0x1f 
32 0x20      33 0x21 !   34 0x22 "   35 0x23 #
36 0x24 $    37 0x25 %   38 0x26 &   39 0x27 '
40 0x28 (

Now, the ISSUE itself. This format is 98% what I want except, that after the symbol "(" for the value 40, there should be 2 spaces (=tab), like there's for everyother gap between columns, but there isn't. Like I wrote, a full row cannot end with tab, but a partial row like the last one, should one a tab. And just to clarify only row that can be less than 4 columns wide, is the last one.

THE question therefore is: how do I get a tab after the last ASCII-symbol for the last row if it's less than full (=4 columns wide).

Have been stuck with this little issue for days now (of course I haven't spent my whole time of being awake with programming, but multiple hours per day at least) so I would be really, really thankful if someone could introduce a fix for this!

CodePudding user response:

The problem is that the i < max condition of the loop prevents the last value from being printed with a tab.

Change that to i <= max. Then add a condition around printing the 4th column to check whether we've already reached the max.

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main()
{
    int min=28;                         //give the starting point of the ASCII dec range
    int max=40;                         //give the ending point of the ASCII dec range


    int i, j;                           //these are used to produce rows and columns

    for(i=min; i<=max; i=i 4)           //create a four "cells" wide row
    {
        for(j=i; j<=i 2 && j<=max; j  )  //print the first three values for the row
        {
            if(isprint(j)){             //print the ASCII character if it's printable, if not, print question mark
                printf("= 0xx %c\t", j, j ,j);
            }else {
                printf("= 0xx ?\t",j, j);
            }
        }

        if (j <= max) {                      //are we done?
            if(isprint(j 3))                //print the last value for each row
            {
                printf("= 0xx %c", j, j ,j);
            }else {
                printf("= 0xx ?",j, j);
            }
        }
        printf("\n");                     //once a row has the max 4 "cells", start a new row and repeat the process
    }
    return 0;
}
  • Related