Skip to content

11.21 Tutorial6

intro-to-computer-science-gtiit-tutorial-06-2024.pdf

Introduction to Computer Science

Tutorial 6: printf() Formatting, ASCII, Standard Input/Output

1. printf() Formatting

1.1 Celsius to Fahrenheit Table

Temperatures can be expressed in various scales. Two of the most commonly used are Celsius and Fahrenheit. The relationship between these scales is summarized by the formula:

F = C × (9/5) + 32

where C is the temperature in Celsius degrees, and F is the corresponding temperature in Fahrenheit degrees.

Write a program celsius2fahr.c​ that:

  • Receives from standard input three floats x​, y​, and i​.
  • Prints out a table of Celsius/Fahrenheit degrees, showing all Celsius temperatures from x​ to y​ in steps of i​, and their corresponding Fahrenheit degrees.
  • The table should have headings:

  • "Celsius"

  • "Fahrenheit"
  • Values printed should be right-aligned, with two decimal places.
#include <stdio.h>

int main() {
    float start, end, step;
    scanf("(%f, %f, %f)", &start, &end, &step);// it also works if you input"(1, 20,2)"
    // But the best way is to scanf one variable in one time
    printf("%-9s | %9s\n", "Celsius", "Farenheit"); //if there is no minus "-", it will be left aligned
    for(float i = start; i <= end; i+=step) {
        printf("%-9.1f | %-9.1f\n", i, i*(9/5.0)+32);//less than 9 digits, then fill it
    }
    return 0;
}

1.2 Fahrenheit to Celsius Table

Write a program similar to the one above but to produce a table converting from Fahrenheit to Celsius.

#include <stdio.h>

int main() {
    float start, end, step;
    scanf("(%f, %f, %f)", &start, &end, &step);// it also works if you input"(1, 20,2)"
    // But the best way is to scanf one variable in one time
    printf("%-9s | %9s\n", "Celsius", "Farenheit"); //if there is no minus "-", it will be left aligned
    for(float i = start; i <= end; i+=step) {
        printf("%-9.1f | %-9.1f\n",(i-32)*5/9, i);//less than 9 digits, then fill it
    }
    return 0;
}

2. Character Input/Output with getchar()​ and putchar()

2.1 Line Break at 80 Characters

Write a program that processes a stream of characters from standard input and outputs it to standard output in such a way that:

  • No line exceeds 80 characters in width.
  • When a line of more than 80 characters is produced, a line break is forced at 80 characters, continuing the string on the next line.
#include <stdio.h>

int main() {
    int ch;
    int counter = 0;
    int breakline = '\n';
    do {
        ch = getchar();
        if (counter >= 80) {
            putchar(breakline);
            counter = 0;
        }
        printf("%c", ch);
        counter++;
    } while (ch != EOF);
    return 0;
}

2.2 Adjusted Text Formatting

Write a program that processes a stream of characters from standard input and outputs it to standard output with the following adjustments:

  • Multiple blank spaces are replaced by single spaces (e.g., "hi my"​ becomes "hi my"​).
  • After each period ('.'​), a blank space is inserted between the period and the next word.
#include <stdio.h>

int main() {
    int ch;
    int lastCharacter;
    do {
        lastCharacter = ch;
        ch = getchar();
        if(lastCharacter == '.' && ch !=' ') {
            putchar(' ');
            putchar(ch);
        }
        else if(ch != ' ' || lastCharacter != ' ') 
            putchar(ch);
    } while (ch != EOF);
    return 0;
}


3. A Text Statistics Program

3.1 Input Stream Statistics

Write a program that processes a stream of characters from standard input and outputs three different statistics of the input:

  • Total number of characters.
  • Total number of words.
  • Total number of lines.
#include <stdio.h>

int main() {
    int ch;
    int lastCharacter;
    int counter = 0;
    int wordCounter = 1;
    int lineCounter = 1;
    do {
        lastCharacter = ch;
        ch = getchar();
        counter++;
        if(ch == ' ' && lastCharacter != ' ') 
            wordCounter++;
        if(ch == '\n')
            lineCounter++;
    } while (ch != EOF);
    printf("\nCharacters: %d\nWords: %dLines: %d\n", counter, wordCounter, lineCounter);
    return 0;
}

4. Linux Commands and Redirection

File and Directory Statistics

The ls​ command has many options to list files and directories, files only, directories only, etc. Similarly, the wc​ command has options to compute various statistics. Use a combination of the ls​ and wc​ commands, with redirection and/or pipes, to count the number of directories present in the current directory.