#include <stdio.h>
#define BUFSIZE 21
typedef enum {false=0,true=1} Boolean;
int GetString(char *buf)
{
int ch;
int i=0;
while( (ch=getchar())!=EOF && ch!='/n')
{
if (i==BUFSIZE-1)
{
printf("Are you trying to overflow the buffer? size=%d/n",BUFSIZE-1);
while( (ch=getchar())!=EOF && ch!='/n' ){;}
break;
}
buf[i++]=ch;
}
buf[i]='/0';
if (i==0)
{
printf("You've entered nothing!/n");
//exit(1);
}
return i;
}
int GetInput(char *buf)
{
int stringSize;
//Give user a chance to re-enter the input when they failed for the first time
do {
printf("Please enter a string(less than %d chars)/n",BUFSIZE-1);
stringSize = GetString(buf);
//printf("stringSize = %d/n",stringSize);
}while(stringSize==0);
return stringSize;
}
void reportSame(char *buf,int stringSize)
{
int count;
int i,j;
for (i=0;i<stringSize;++i)
{
if(buf[i]=='/0')
continue;
count=1;
for (j=i+1;j<stringSize;++j)
{
if(buf[i]==buf[j])
{
++count;
buf[j]='/0';
}
}
printf("There are %d '%c's in the string/n",count,buf[i]);
}
}
int main (int argc, int **argv)
{
char inputBuf[BUFSIZE];
int stringSize;
while(true)
{
stringSize = GetInput(inputBuf); //input is a string
//printf("%s/n",inputBuf);
reportSame(inputBuf,stringSize);
}
return 0;
}
c 代码存档: 快速查找相同的元素(数组版)
最新推荐文章于 2021-12-15 00:46:30 发布