fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. #define ROWS 100
  7. #define COLUMNS 100
  8.  
  9. char matrix[ROWS][COLUMNS];
  10.  
  11. void generate_matrix() {
  12. for (int i = 0; i < ROWS; i++)
  13. for (int j = 0; j < COLUMNS; j++)
  14. matrix[i][j] = 'A' + (rand() % 26);
  15. }
  16.  
  17. int is_palindrome(const char *str, int len) {
  18. for (int i = 0; i < len / 2; i++)
  19. if (str[i] != str[len - i - 1])
  20. return 0;
  21. return 1;
  22. }
  23.  
  24. int count_palindromes(int size) {
  25. int count = 0;
  26. char temp[10];
  27.  
  28. for (int i = 0; i < ROWS; i++)
  29. for (int j = 0; j <= COLUMNS - size; j++) {
  30. for (int k = 0; k < size; k++)
  31. temp[k] = matrix[i][j + k];
  32. temp[size] = '\0';
  33. if (is_palindrome(temp, size)) count++;
  34. }
  35. return count;
  36. }
  37.  
  38. int main() {
  39. srand(time(NULL));
  40. generate_matrix();
  41.  
  42. clock_t start = clock();
  43. int total = count_palindromes(3);
  44. clock_t end = clock();
  45.  
  46. printf("Palindromes of size 3: %d\n", total);
  47. printf("Time: %f seconds\n", (double)(end - start) / CLOCKS_PER_SEC);
  48.  
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Palindromes of size 3: 372
Time: 0.000000 seconds