C++查找字符串中同样的字符,并将其删除不改变字符串的顺序。

输入一个字符串,找到相同的字符,将后面出现的字符删除,不改变字符串的顺序。

例如:

Hello    -》Helo

人山人海   -》人山海

代码实现:

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


int main ()
{
	string str="hello world";//这里自己设置输入信息
   
	for(int i=0;i<str.length();i++)
	   for(int j=i+1;j<str.length();j++)
	   {
			if(str[i]==str[j])
				str[j]=NULL; 
	   }

	for(int j=0;j<str.length();j++)
	   {
			if(str[j]==NULL)
				continue;
			else cout<<str[j];
   
   }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/donkey_1993/article/details/82767379
今日推荐