Skip to content

11.22 Lab6

introduction-to-computer-science-laboratory-6.pdf

Introduction to Computer Science

Laboratory 6

  1. Solve all the exercises present in the tutorial.

  2. Write a C program that takes an integer N​ as input and prints an inverted asterisk pyramid with N​ floors. For example, the output with N = 5​ should be:

    image

    #include <stdio.h>
    
    int main() {
        int number;
        int star = '*';
        printf("Please enter a number to generate an inverted asterisk pyramid:");
        scanf("%d", &number);
        for(int i = 1; i <= number; i++) {
            printf("%*c", i, star);
            for(int j = 1; j <= (number-i); j++) {
                printf(" *");
            }
            printf("\n");
        }
        return 0;
    }
    
  3. Write a C program that takes as input the file input_ex2.txt​ and prints to the screen all the characters between braces ([​ and ]​). Hint: Redirect the input of your program to the file input_ex2.txt​.

    #include <stdio.h>
    
    int main() {
        int ch, pch;
        do {
            pch = ch;
            ch = getchar();
            putchar('[');
            putchar(ch);
            putchar(']');
        } while(ch != EOF);
        return 0;
    }
    

  1. Write a C program that takes as input the file input_ex3_ex4.txt​ and prints only the uppercase characters.

    #include <stdio.h>
    
    int main() {
        int ch;
        do {
            ch = getchar();
            if(ch >= 'A' && ch <= 'Z') {
                putchar(ch);
            }
        } while(ch != EOF);
        return 0;
    }
    
  2. Write a C program that takes as input the file input_ex3_ex4.txt​ and creates a file called output_ex4.txt​ that contains only the lowercase characters of the input.

    Hint: Redirect the output of your program to the file output_ex4.txt​.

    #include <stdio.h>
    
    int main() {
        int ch;
        do {
            ch = getchar();
            if(ch >= 'a' && ch <= 'z') {
                putchar(ch);
            }
        } while(ch != EOF);
        return 0;
    }
    
  3. Write two C programs:

  4. The first should print the English alphabet.

#include <stdio.h>

int main() {
    int ch = 'A';
  do {
      putchar(ch);
      ch = ch + 1;
  } while(ch <= 'Z');
    return 0;
}
* The second receives a stream of characters and prints the ones whose ASCII code is even.

Hint: Redirect the output of the first program to a file and use it as input for the second one.

#include <stdio.h>

int main() {
    int ch;
  do {
      ch = getchar();
      if(ch % 2 == 0)
          putchar(ch);
  } while(ch != EOF);
    return 0;
}