Bài này em không nghĩ ra được thuật toán ạ, em có lên mạng tra thử thì thấy bài như thế này. Tuy nhiên em không hiểu thuật toán của đề bài nên mong các anh/chị/bạn chỉ dẫn giúp em ạ. Em xin cảm ơn nhiều ạ.
#include<iostream>
#include <cstring>
using namespace std;
main()
{
string str;
cout<<"Enter your String:";
getline(cin,str);
int in=0,out=0,p=0,len1=0;
char sub_str[100][100]={0};
//splitting Input String into sub string
while(str[p]!='\0')
{
out=0;
while(str[p]!=' ' && str[p]!='\0')
{
sub_str[in][out]=str[p];
p++;
out++;
}
sub_str[in][out]='\0';
in++;
if(str[p]!='\0')
{
p++;
}
}
cout<<"After removing duplicate words from a given String is:";
len1=in;
for(in=0;in<len1;in++)
{
for(out=in+1;out<len1;)
if(strcmp(sub_str[in],sub_str[out])==0)
{
for(p=out;p<len1;p++)
{
strcpy(sub_str[p],sub_str[p+1]);
}
len1--;
}
else
{
out++;
}
}
for(in=0;in<len1;in++)
{
cout<<sub_str[in]<<" ";
}
}
Đây là bài code ạ.