This question is incomplete. The complete question is given below:
Write a program that gets a single character from the user. If the character is not a capital letter (between 'A' and 'Z'), then the program does nothing. Otherwise, the program will print a triangle of characters that looks like this:
A
ABA
ABCBA
ABCDCBA
Answer:
#include <stdio.h>
void printTri(char asciiCharacter){
// Row printer
// for(int i = 65; i <= ((int)asciiCharacter); i++){
int i = 65;
while(i <= ((int)asciiCharacter)){
int leftMargin = ((int)asciiCharacter) - i;
+
int j = 1;
while(j <= leftMargin){
printf(" ");
j++;
}
int k = 65;
while(k < ((int)i)){
printf("%c",(char)k);
k++;
}
int l = ((int)i);
while(l >= 65){
printf("%c",(char)l);
l--;
}
printf("\n");
i++;
}
}
int main()
{
for(;;){
printf("Please enter a character between A and Z. Enter (!) to exit \n");
char in;
// scanf("%c",&in);
in = getchar();
getchar();
printf("Printing Triangle for: %c \n",in);
if(((int)in) >= 65 && ((int)in) <= 90){
printTri(in);
printf("\n");
}else if(((int)in) == 33){
break;
} else {
continue;
}
}
return 0;
}
Explanation:
- Inside the printTri function, print spaces, characters in order as well as characters in reverse order.
- Inside the main function, take the character as an input from user then print the triangle by validating first to ensure that the character lies between a to z alphabets.