设计C语言程序:字符串处理

来源:百度知道 编辑:UC知道 时间:2024/05/22 14:57:09
要求是;从键盘输入一个英文句子并保存在字符数组中;能删除多余空格(单词之间只留一个空格,句子前后无空格); 能统计某单词出现的频率;能替换某个单词。

#include "stdio.h"
#include "string.h"
#include "ctype.h"
void Delete(char str[])
{
int i,j,Length,StartBlank=0,EndBlank=0,MiddleBlank=0;
Length=strlen(str);
for(i=0; i<Length; i++) /* 删除前导空格 */
if(str[i]==' ') StartBlank++;
else break;
for(j=0; j<Length; j++)
str[j]=str[j+StartBlank];
Length-=StartBlank;
for(i=Length-1; i>=0; i--) /* 删除后导空格 */
if(str[i]==' ') EndBlank++;
else break;
Length-=EndBlank;
while(EndBlank>0)
{
str[Length-1+EndBlank]=str[Length+EndBlank];
EndBlank--;
}
i=0; /* 删除中间多余空格 */
while(i<Length)
{
if(str[i]==' ')
{
if(str[i+1]==' ')
{
for(j=i+1; j<Length; j++)
str[j]=str[j+1];
MiddleBlank++;
Length--;
}
else i++;
}
else i++;
}
}<