作业帮 > 综合 > 作业

数据结构的题目,用c语言编写下面的程序

来源:学生作业帮 编辑:灵鹊做题网作业帮 分类:综合作业 时间:2024/04/23 18:43:07
数据结构的题目,用c语言编写下面的程序
删除该英文句子的前导空格,厚道空格和句子中多余的空格(每两个单词只留一个空格)
统计该句子中单词出现的频率
查找并替换某个单词
删除该英文句子的前导空格,厚道空格和句子中多余的空格(每两个单词只留一个空格)
统计该句子中单词出现的频率
查找并替换某个单词
数据结构的题目,用c语言编写下面的程序
#include
#define max 100
void findx(char shuru[], char T)
{
int x=0,i=0;
while(shuru[i]!='\0')
{
if(shuru[i]==T)
x++;
i++;
}
printf("该句子中有%d个%c\n",x,T);
}
void chang(char shuru[],char T,char B)
{
int i=0;
while(shuru[i]!='\0')
{
if(shuru[i]==T)
shuru[i]=B;
i++;
}
printf("%s\n",shuru);
}
void main()
{
char shuru[max],t,b;
int i=0,j=0;
printf("请输入字符串:");
gets(shuru);

while(shuru[0]==' ')//删除前导空格
{
i=0;
while(shuru[i+1]!='\0')//字符串往前移动一次.
{
shuru[i]=shuru[i+1];
i++;
}
shuru[i]='\0';
}

i=1;
while(shuru[i]!='\0')//字符间保持只有一个空格
{
if(shuru[i]==' '&&shuru[i+1]==' ')
{
j=i;
while(shuru[j+2]!='\0')
{
shuru[j+1]=shuru[j+2];
j++;
}
shuru[j+1]='\0';
i--;
}
i++;
}
i=0;
while(shuru[i]!='\0')//删除后导空格
{
i++;
}
while(shuru[i-1]==' ')
{
shuru[i-1]='\0';
i--;
}
printf("%s\n",shuru);
while(1)
{
printf("请选择 1、查找;2、替换.\n");
scanf("%d",&j);
if(j==10)
scanf("%d",&j);
switch(j)
{
case 1:
printf("请输入要搜索的单词:");
scanf("%c",&t);
if(t==10)
scanf("%c",&t);
findx(shuru,t);
break;
case 2:
printf("请输入要替换的单词:");
scanf("%c",&t);
if(t==10)
scanf("%c",&t);
printf("请输入要替换后的单词:");
scanf("%c",&b);
if(b==10)
scanf("%c",&b);
chang(shuru,t,b);
break;
default:
printf("输入错误,请重新输入!\n");
}
}
}