题目:
输入一串英文单词,不同单词之间使用一个逗号,或者多个隔开,统计该串中单词首尾字母相同的单词,不区分大小写,要求时间复杂度为O(n)。
输入描述:
输入为一行单词,例如:
Students,are,studying,Asia,history
输出描述:
输出为一个数,例如:
2
个人所写,代码如下:
#include <ctype.h>
#include <stdio.h>
int get_count()
{
char chn[1024];
int i=0;
char ch;
int count=0;
int j1=0;
int j2=0;
int temp;
while((ch=getchar())!='\n')
{
chn[i]=ch;
if(ispunct(ch))
{
temp=j1-j2;
j2=j1;
char ch1=tolower(chn[i-1]);
char ch2=toupper(chn[i-1]);
if(ch1==chn[i-temp]||chn[i-temp]==ch2)
{
count++;
}
continue;
}
i++;
j1++;
}
temp=j1-j2;
j2=j1;
char ch1=tolower(chn[i-1]);
char ch2=toupper(chn[i-1]);
if(ch1==chn[i-temp]||chn[i-temp]==ch2)
count++;
return count;
}
int main()
{
puts("enter a string");
int i=get_count();
printf("count is %d",i);
return 0;
}
如有错误,欢迎指正、交流。