字符串处理:用一个字符数组保存着一个英文句子,

来源:百度知道 编辑:UC知道 时间:2024/05/09 08:25:22
要求:1.删除该英文句子的前导空格,后倒空格。并删除句子中多余的空格(单词与单词间只留一个空格);
2.统计该句子中,单词出现的频率;
3.查找并替换某个单词。
要用C语言写的程序
跪求各路高手!!!
能帮忙的请跟我联系
谢谢各路大侠!!
QQ277604140

/*
* 此文件可以任意复制修改使用,scenbuffalo不负任何责任
*/
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
#include <stdlib.h>

char*
discard_space(char* str)
{
char *st = NULL;
char *stt;
char *strt = str;;
int len = strlen (str);

if ((st = malloc (len+1)) == NULL)
{
fprintf (stderr, "malloc failed");
exit(1);
}
stt = st;
memset (st, 0, len+1);

while (*str)
{
while (isspace(*str))
str++;
while ((*str)&&(! isspace(*str)))
*st++ = *str++;
if (*str)
*st++ = ' ';
else
break;
}
if ((str > strt) && (isspace(str[-1])))
{
st[-1] = '\0';
}
strncpy (strt, stt, len);
free (stt);
stt = NULL;
return strt;
}