Help with a simple C programming exercise

I am new to C programming, having a bit of difficulty with a programming exercise, I’m sure it’s just for those who know C, unfortunately you need to play by the rules of the exercise.

Here's the exercise:

Ask the user to request a capital letter. Use nested loops to create a pyramid pattern for example:

    A 

   ABA

  ABCBA

 ABCDCBA

ABCDEDCBA

The pattern should extend to the character entered. For example, the preceding pattern will be the input value E. Hint: use external to process strings. Use three inner loops in a row, one to process letters, one to print letters in ascending order, and one to print letters in descending order.

So, I got this far:

#include <stdio.h>

int main(void) {

 int rows;
 int spaces;

 char asc;
 char desc;
 char input;

 printf("Please enter an uppercase letter: ");
 scanf("%c", &input);

 for (rows = 'A'; rows <= input; rows++) {
  for (spaces = input; spaces > rows; spaces--) {
   printf(" ");
  }
  for (asc = 'A'; asc <= rows; asc++) {
   printf("%c", asc);
  }
  for (desc = asc - 2; desc >= rows; desc--) {
   printf("%c", desc);
  }
  printf("\n");
 }
 return 0;
}
0
source share
2 answers

:

for (desc = asc - 2; desc >= 'A'; desc--) {

, asc rows + 1. desc rows - 1. , >= rows , .

- >= 'A'.

+5
#include <stdio.h>
#include <string.h>
#define ROW 6

int main() {

  char let = '\0', ch;
  int row;

  scanf("%c", &let);
  for (row = 0; row <= ROW; row++) {
    for (ch = let; let > (ch + row); --ch) {
      for (ch = let; let < (ch + row); ++ch) {
        printf("%c", ch);
      }
      // ch = let;
      printf("\n");
    }
  }
  return 0;
}

.        // ;)

-1

All Articles