标签:
算法思想:以‘\0‘结束标志,遍历字符串元素surchr,让每个字符与输出output字符串每个元素做比较。一旦output中
最后一个元素与chr不等,output增加元素,否则,中间就用break跳出,开始判断下一个surchr
/***************1***************/
#include <iostream>
#include <string>
using namespace std;
int GetResult(const char* input, char* output);
int main()
{
string str;
getline(cin, str);
cout << str<<endl;
int len = str.length();
char* output = new char[len + 1];
if (output == NULL)
{
cout<<"heap allocator failed"<<endl;
return 0;
}
memset(output, 0, len+1);
int ret = 0;
ret = GetResult(str.c_str(), output);
for (int k=0; k<ret; k++)
{
cout<<output[k];
}
cout <<endl;
return 0;
}
int GetResult(const char* input, char* output)
{
if (input == NULL || output == NULL)
{
return 0;
}
const char* surStr = input;
output[0] = surStr[0];
int index = 1;
surStr++;
while(*surStr != ‘\0‘)
{
for (unsigned int i=0; i<strlen(output); i++)
{
if (*surStr == output[i])
{
break;
}
////output的最后一个非‘\0‘字符与*surStr不同时执行
if (i == strlen(output) - 1)
{
++index;
output[++i] = *surStr;
}
}
surStr++;
}
return index ;
}
标签:
原文地址:http://www.cnblogs.com/niupan369/p/4465418.html