Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- void addChar(char** res, char s) {
- int size = strlen((*res));
- (*res) = (char*)realloc((*res), (size + 2) * sizeof(char));
- (*res)[size + 1] = '\0';
- (*res)[size] = s;
- }
- char* getResult(char* s) {
- int i = 0;
- int j = strlen(s) - 1;
- int isNotDup = 0;
- char temp;
- char* res = (char*)malloc(sizeof(char));
- res[0] = '\0';
- for (i = 0; i <= j; i++) {
- isNotDup = 1;
- if (s[i] == s[i + 1]) {
- temp = s[i];
- i++;
- while (s[i + 1] == temp)
- i++;
- addChar(&res, '_');
- for (; (j != i && isNotDup); j--) {
- if (s[j] == s[j - 1]) {
- isNotDup = 0;
- temp = s[j];
- j--;
- while (s[j - 1] == temp)
- j--;
- addChar(&res, '_');
- }
- else {
- addChar(&res, s[j]);
- }
- }
- }
- else {
- addChar(&res, s[i]);
- }
- }
- return res;
- }
- int main() {
- char s1[100];
- char* res;
- printf("Enter string: ");
- fgets(s1, sizeof(s1), stdin);
- s1[strlen(s1) - 1] = '\0';
- res = getResult(s1);
- printf("\nResult string: %s", res);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement