刚刚参加完华为机试题,也是心累,在第二道题上翻了大跟头,明明已经出了结果还是错误。
下来后重新写了一遍代码,发现就错在 j<0 条件写成 j=0了,那个悔恨啊。现贴出自己的题目和代码,已做警示。
过滤重复单词
一个英文句子有单词和逗号,句号,空格组成。
要求过滤掉句子中重复单词(保留重复单词中的第一个),按句子顺序输出不重复的单词(不包括标点符号)
单词区分大小写,Where和where不同
输入:仅包含逗号,句号,空格的英文句子,长度小于200
输出:去掉标点符号和重复单词的句子
样例输入:where there is a will, there is a way.
样例输出:where there is a will way
#include<stdio.h>
#include<string.h>
int main()
{
char input[200];
char input1[200];
char temp[20];
char output[200][20]={'\0'};
int i,j=0,len0,len1,k=0,count=0;
gets(input);
len0=strlen(input);
for(i=0;i<len0;i++){
if((input[i]==',')||(input[i]=='.')){
input1[j]=' ';
j++;
}
else{
input1[j]=input[i];
j++;
}
}
//input1[j]='\0';
//printf("%s\n",input1);
len1=strlen(input1);
for(i=0;i<len1;i++){
if(input1[i]!=' '){
temp[k++]=input1[i];
}
else{
for(j=0;j<k;j++){
//printf("%c",temp[j]);
output[count][j]=temp[j];
}
//printf("\n");
count++;
k=0;
}
}
printf("%s",output[0]);
for(i=1;i<count;i++){
/*if(strlen(output[i])){
printf("%s\n",output[i]);
}*/
for(j=i-1;j>=0;j--){
if(!strcmp(output[i],output[j]))break;
}
if(j<0)
{
if(strlen(output[i])){
printf(" %s",output[i]);
}
}
}
// printf("%d\n",count);
return 0;
}