C說話字符串原地緊縮完成辦法。本站提示廣大學習愛好者:(C說話字符串原地緊縮完成辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C說話字符串原地緊縮完成辦法正文
本文實例講述了C說話字符串原地緊縮的完成辦法,關於進修字符串操作的算法設計有不錯的自創價值。分享給年夜家供年夜家參考。詳細辦法以下:
字符串原地緊縮示例: "eeeeeaaaff"緊縮為"e5a3f2"
詳細功效代碼以下:
/*
* Copyright (c) 2011 alexingcool. All Rights Reserved.
*/
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
char array[] = "eeeeeaaaff";
char array2[] = "geeeeeaaaffg";
const int size = sizeof array / sizeof *array;
const int size2 = sizeof array2 / sizeof *array2;
void compression(char *array, int size)
{
int i = 0, j = 0;
int count = 0;
while(j < size) {
count = 0;
array[i] = array[j];
while(array[j] == array[i]) {
count++;
j++;
}
if(count == 1) {
i++;
}
else {
array[++i] = '0' + count;
++i;
}
}
array[i] = 0;
}
void main()
{
compression(array, size);
cout << array << endl;
compression(array2, size2);
cout << array2 << endl;
}
信任本文所述對年夜家C法式算法設計的進修有必定的自創價值。