11.22 Lab6
introduction-to-computer-science-laboratory-6.pdf
Introduction to Computer Science
Laboratory 6
-
Solve all the exercises present in the tutorial.
-
Write a C program that takes an integer
N
as input and prints an inverted asterisk pyramid withN
floors. For example, the output withN = 5
should be:
#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; }
-
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 fileinput_ex2.txt
.#include <stdio.h> int main() { int ch, pch; do { pch = ch; ch = getchar(); putchar('['); putchar(ch); putchar(']'); } while(ch != EOF); return 0; }
-
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; }
-
Write a C program that takes as input the file
input_ex3_ex4.txt
and creates a file calledoutput_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; }
-
Write two C programs:
-
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;
}
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;
}