转换字符串格式为原来字符串里的字符+该字符连续出现的个数

/************************************************************************
转换字符串格式为原来字符串里的字符+该字符连续出现的个数,
例如:1233422222 转换为1121324125(1出现1次,2出现1次,3出现2次...... 
*************************************************************************/
#include<iostream> 
#include<cstring>
#include<cstdlib>
using namespace std; 
void convert(char input[],char output[]) 
{ 
	  
	int count = 1; 
	char *fast = input; 
	char *slow = input; 
 
	while(*(++fast)!='\0') 
	{ 
		if(*fast == *slow)//如果当前读入的字符和正在统计的一样,那么出现次数要++ 
		{ 
			count++; 
			continue; 
		} 
		// 当快慢指针不同时就执行下边语句
		if(slow<fast)  //注意不能写成while 形式 因为要求重复的数字只出现一次
		{
			*output++=*slow++;  
		}
		*output++=count+'0';  //将出现次数添加至数字后边
		slow = fast;  //  慢指针指向快指针 重新统计
		count = 1;  //计数器重置为初始值
	} 
	*output++=*slow;  // 快指针跳出循环时慢指针仍没有跳出循环但是count计数正确
	*output++=count+'0';//因为读到'\0'循环退出,最后一个连续的字符序列没有输出,所以在这里追加输出 
	*output='\0';
} 
int main()
{
	char input[]="1233422222";
	char output[30]={0};
	convert(input,output);
	cout<<"output is "<<output<<endl;
}
 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值