fork download
  1. #include <stdio.h>
  2. #include <limits.h>
  3. #include <string.h>
  4.  
  5. void print_spaces(int n) {
  6. for (int i = 0; i < n; i++) printf(" ");
  7. }
  8.  
  9. int main(void) {
  10. //CONSOLE_SCREEN_BUFFER_INFO csbi;
  11. int columns, rows, cols_by_2;
  12.  
  13. //GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
  14. //columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
  15. //rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
  16. columns = 100;
  17. cols_by_2 = columns / 2;
  18.  
  19. // Put all options in an array
  20. char options[4][100] = {"1: Neue Spielrunde", "2: Charaktere laden",
  21. "3: Spielrunde speichern", "4: Programm beenden"};
  22.  
  23. char menu_header[5] = "Men\x81";
  24. int len_header_by_2 = strlen(menu_header) / 2;
  25.  
  26. print_spaces(cols_by_2 - len_header_by_2);
  27. printf("%s\n", menu_header);
  28.  
  29. // Find max half-length of string
  30. int max_val = INT_MIN;
  31. for (int i = 0; i < 4; i++) {
  32. int len_str = strlen(options[i]) / 2;
  33. if (max_val < len_str)
  34. max_val = len_str;
  35. }
  36.  
  37. // Compute spaces to add for max half-length string
  38. int no_of_spaces = cols_by_2 - max_val;
  39. for (int i = 0; i < 4; i++) {
  40. print_spaces(no_of_spaces);
  41. printf("%s\n", options[i]);
  42. }
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0s 4540KB
stdin
Standard input is empty
stdout
                                                Men�
                                       1: Neue Spielrunde
                                       2: Charaktere laden
                                       3: Spielrunde speichern
                                       4: Programm beenden