原地压缩字符串的简单实现

    技术2022-06-10  43

    char* compress(char str[]){ char* front = str; char* save = str; char temp; int count = 0; if(front != NULL){ while(*front!='/0'){ temp = *front; count = 0; while(temp == *front) { front++; count++; } if(count > 1) { *(++save) = count + '0'; *(++save) = *front; } else { save++; } } //while } *save = '/0'; return str; } int main(int argv, char** argc){ char str[] = "eeeeeaaaff"; //char str[] = "abc"; char* res; res = compress(str); cout<<res<<endl; return 0; };


    最新回复(0)