You are viewing a single comment's thread. Return to all comments →
Here is my approach. Maybe more complexity, but it's a modular version. Any comments are welcome:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #define LENGTH 100 char* reduce_string(char* s) { int i, r_len, len = strlen(s); bool found = false; char* red_str; for(i = 0; i< len-1; ++i){ //Looking for repetitions if(s[i] == s[i+1]){ found = true; break; } } if(found){ r_len = len-2; if(r_len == 0) return NULL; } else{ return s; //r_len = len; //i = len; } //Replacement red_str = (char*)malloc(len*sizeof(char)); int k = 0; for(int j = 0; j < len; ++j){ if(j != i && j != i+1){ red_str[k] = s[j]; ++k; } } return red_str; } char* super_reduced_string(char* s){ char *sup_red, *str_tmp; do{ str_tmp = s; sup_red = reduce_string(s); s = sup_red; }while(str_tmp != s && s != 0); return sup_red; } int main() { char str[100]; char* str_red; char* str_test; scanf("%s", str); str_red = super_reduced_string(str); if(str_red == 0) printf("Empty String\n"); else printf("%s\n", str_red); return 0; }
Seems like cookies are disabled on this browser, please enable them to open this website
Super Reduced String
You are viewing a single comment's thread. Return to all comments →
Here is my approach. Maybe more complexity, but it's a modular version. Any comments are welcome: