Advertisement
believe_me

Untitled

Sep 28th, 2022
1,031
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. void addChar(char** res, char s) {
  8.     int size = strlen((*res));
  9.     (*res) = (char*)realloc((*res), (size + 2) * sizeof(char));
  10.     (*res)[size + 1] = '\0';
  11.     (*res)[size] = s;
  12. }
  13.  
  14. char* getResult(char* s) {
  15.     int i = 0;
  16.     int j = strlen(s) - 1;
  17.     int isNotDup = 0;
  18.     char temp;
  19.     char* res = (char*)malloc(sizeof(char));
  20.     res[0] = '\0';
  21.     for (i = 0; i <= j; i++) {
  22.         isNotDup = 1;
  23.         if (s[i] == s[i + 1]) {
  24.             temp = s[i];
  25.             i++;
  26.             while (s[i + 1] == temp)
  27.                 i++;
  28.             addChar(&res, '_');
  29.             for (; (j != i && isNotDup); j--) {
  30.                 if (s[j] == s[j - 1]) {
  31.                     isNotDup = 0;
  32.                     temp = s[j];
  33.                     j--;
  34.                     while (s[j - 1] == temp)
  35.                         j--;
  36.                     addChar(&res, '_');
  37.                 }
  38.                 else {
  39.                     addChar(&res, s[j]);
  40.                 }
  41.             }
  42.         }
  43.         else {
  44.             addChar(&res, s[i]);
  45.         }
  46.     }
  47.     return res;
  48. }
  49.  
  50.  
  51.  
  52. int main() {
  53.     char s1[100];
  54.     char* res;
  55.     printf("Enter string: ");
  56.     fgets(s1, sizeof(s1), stdin);
  57.     s1[strlen(s1) - 1] = '\0';
  58.     res = getResult(s1);
  59.     printf("\nResult string: %s", res);
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement