Friday, April 19, 2024
HomeC ProgrammingCorrectly Padding Areas – and Tabs

Correctly Padding Areas – and Tabs


As you may inform by the publish title, a part of the sconvert program I missed is to transform tab characters into HTML areas. Like areas, tabs output blanks that have to be transformed to the   code for correct output on an internet web page.

In final week’s Lesson, I informed the riviting story of the sconvert utility, which I take advantage of to course of program output for this weblog. It replaces areas with the   (non-breakable area) HTML code. This program automates the conversion for me, so I don’t manually exchange the areas in pattern output, which is a ache and takes time.

The primary draft of the code, nevertheless, didn’t convert tabs. Including tab detection and output routines requires updating the present code. To keep away from messy if-else selections, I selected to make use of a switch-case construction to guage and convert streaming enter:

2023_04_08-Lesson.c


#embody <stdio.h>

int essential()
{
    int ch;

    whereas(1)
    {
        ch = getchar();
        if( ch==EOF )
            break;
        swap(ch)
        {
            case ' ':
                printf("&nbsp;");
                break;
            case 't':
                printf("&nbsp;&nbsp;&nbsp;&nbsp;");
                break;
            default:
                putchar(ch);
        }
    }

    return(0);
}

Instantly after grabbing a personality from commonplace enter, an if take a look at checks for the EOF. When it floats in, the limitless whereas loop is damaged.

The switch-case construction seems to be for twoitems: an area, or a tab. The default situation is to output the character enter, ch. In any other case, for the area or tab, one or 4 HTML non-breakable area codes (&nbsp;) are output.

This replace to the code converts tabs into non-breakable areas for HTML output. One factor the code doesn’t do is to calculate tab offsets.

For instance, I wrote a program that outputs the times of the week adopted by a quantity worth. Determine 1 reveals the output because it seems on the display screen:

Determine 1. Textual content output with tabs might not line up completely. (Tab stops set each eight positions.)

Sure, this system output is ugly as a result of tab stops within the terminal are set each eight column positions. Right here is how the up to date sconvert program interprets the output:

Monday    0
Tuesday    1
Wednesday    2
Thursday    3
Friday    4
Saturday    5
Sunday    6

Oops, the outcomes are totally different. This system merely interprets tabs into non-breakable HTML area codes with out consideration for precise tab stops on the terminal. To extra precisely convert tabs, extra manipulation is required within the code, which I cowl in subsequent week’s Lesson.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments