对于我来说太难了。。看来C语言对新人不是太友好 脑子有想法 但是写不出来有没有 有没有
题目分析
一个空格一个一个单词 本题考查运用二位数组来存贮单词
代码#include
#include
int n,count;
char arr[1050];
char temp[1000][1000];
void different()
{
int i,j;
for(i=0;i<=n;i++)
{
for(j=i+1;j<=n;j++) //单词的比较
{
if(strcmp(temp[i],temp[j])==0)
{
strcpy(temp[j],""); //单词相同 加注相同单词标记
}
}
if(strcmp(temp[i],"")!=0) //判断如果没有有标记
count++; //不同单词+1
}
printf("%d\n",count);
}
void word()
{
char *p=arr;//获得字符串首地址
int j;
while(*p) //当指针没有指到末端时
{
if(*p==' ' && *(p+1)!=' ') 是否为一个单词
{
temp[n][j]='\0'; //二维数组加一个结束方便strcmp比较
j=0;
n++; //一个单词记录完毕
}
if(*p!=' ') //当输入的不是空格时
{
temp[n][j++]=*p;//记入单词
}
p++;
}
}
int main()
{
while(gets(arr))
{
n=0;
count=0;
word();
different();
}
return 0;
}
ps 摘自百度 有兴趣的同学可以看看strcok这个函数