Advertisement
dxvmxnd

Untitled

Dec 24th, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. #include <locale.h>
  5.  
  6. #define MAX_LEN 100
  7.  
  8. bool has_repeated_characters(const char *str, int len, char *repeated_char) {
  9. for (int i = 0; i < len; i++) {
  10. for (int j = i + 1; j < len; j++) {
  11. if (str[i] == str[j]) {
  12. *repeated_char = str[i];
  13. return true;
  14. }
  15. }
  16. }
  17. return false;
  18. }
  19.  
  20. void process_word(const char *word) {
  21. int len = strlen(word);
  22. char protocol[MAX_LEN] = "";
  23. int start = 0, end = len - 1;
  24. bool forward = true;
  25. char temp[MAX_LEN];
  26. char repeated_char;
  27.  
  28. while (start <= end) {
  29. int temp_len = 0;
  30.  
  31. if (forward) {
  32. for (int i = start; i <= end; i++) {
  33. temp[temp_len++] = word[i];
  34. temp[temp_len] = '\0';
  35. if (has_repeated_characters(temp, temp_len, &repeated_char)) {
  36. protocol[strlen(protocol) - 1] = '\0';
  37. strcat(protocol, "_");
  38. start = i + 1;
  39. forward = false;
  40. break;
  41. }
  42.  
  43. strncat(protocol, &word[i], 1);
  44. }
  45. if (forward) {
  46. break;
  47. }
  48. } else {
  49.  
  50. for (int i = end; i >= start; i--) {
  51. temp[temp_len++] = word[i];
  52. temp[temp_len] = '\0';
  53. if (has_repeated_characters(temp, temp_len, &repeated_char)) {
  54. protocol[strlen(protocol) - 1] = '\0';
  55. strcat(protocol, "_");
  56. end = i - 1;
  57. forward = true;
  58. break;
  59. }
  60. strncat(protocol, &word[i], 1);
  61. }
  62. if (!forward) {
  63. break;
  64. }
  65. }
  66. }
  67.  
  68. printf("«Протокол» просмотра: %s\n", protocol);
  69. }
  70.  
  71. int main() {
  72. system("chcp 1251");
  73. setlocale(LC_ALL, "");
  74.  
  75. char word[MAX_LEN];
  76. printf("Введите слово (только прописные русские буквы): ");
  77. scanf("%s", word);
  78.  
  79.  
  80. for (int i = 0; word[i] != '\0'; i++) {
  81. if (word[i] < 'А' || word[i] > 'Я') {
  82. printf("Ошибка: допустимы только прописные русские буквы.\n");
  83. return 1;
  84. }
  85. }
  86.  
  87. process_word(word);
  88.  
  89. int n;
  90. scanf("%d", &n);
  91.  
  92. return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement