华为编程-删除字符串中与前面重复的字符串

本文详细介绍了C++中字符串处理的优化技巧,并通过实例展示了如何高效地使用字符串操作函数,包括输入输出、字符串匹配、去重等关键操作。
例如:输入字符串:abceaacfeg  输出:abcefg
int GetResult(const char *input, char *output)
*input 是输入字符串,*output是输出字符串


#include<iostream>
#include <string>
using namespace std;

int GetResult(const char *input, char *output)
{
	int i;
	int j;
	int t;
	int k = 0;
	char *temp = new char[1000];
	cout<<"temp= ";
	while (*input)
	{
		*(temp + k) = *input;
		cout<<*(temp + k);
		k++;
		input++;
	}
	cout<<endl;
	cout<<"k="<<k<<endl;
	for (i=0; i<k; i++)
	{
		for (j=i+1; j<k; j++)
		{
			if (*(temp+i) == *(temp+j))
			{
				for (t=j; t<k; t++)
				{
					*(temp+t) = *(temp+t+1);
				}
				k--;   //长度减1
				j--;   //第j+1个元素移到j位置上
				
			}
		}
		
	}
	cout<<"kk="<<k<<endl;
   cout<<"output= ";
	for (int i = 0; i < k; i++)
	{
		*(output + i) = *temp;
		temp++;
		cout<<*(output + i);
	}
	return -1;
}

int main()
{
	char *input = "abceaacfeg";
	char *output = new char[1000] ;
	GetResult(input, output);
}


注意点:

1.调用函数里面的形参input是指向常字符串,所以不能直接改变input的值

2.关于移位的逻辑要弄明白

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值