プログラムを作成し、文を入力し、その文の異なる単語の数を統計します.


プログラムを作成し、文を入力し、その文の異なる単語の数を統計します.
#include 
#include
using namespace std;

int main()
{
    char str[1000];
   
    
    gets(str);
    int n = strlen(str);//                ,  n=0,        
    int count = 1;
    

    for(int i = 0;i < n;i++)
    {
        
        if(str[i]==' ')
            count++;
    }
    cout << count;


}

男の人の解答:set容器を使って、
#include
#include
#include
#include
using namespace std;
int main()
{
    char ch[105];
    gets(ch);
    string sh[105];
    int len1=strlen(ch);
    set<string> S;
    int k=0;
    int temp=0;
    for(int i=0;i<=len1;i++)
    {
        if(ch[i]==' '||ch[i]=='\0')
        {

            for(int j=k;j<i;j++)
            {
                sh[temp]=sh[temp]+ch[j];
            }
            temp++;
            k=i+1;
        }
    }
    for(int i=0;i<temp;i++)
        S.insert(sh[i]);
    cout<<S.size()<<endl;
    return 0;
}