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

2025-02-25 13:34:04
推荐回答(1个)
回答1:

/*
* 此文件可以任意复制修改使用,scenbuffalo不负任何责任
*/
#include
#include
#include
#include
#include

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;
}
char*
subnstr(char* str, int maxlen, const char* str2sub, const char* str4sub)
{
char *strt = str;
char *st = NULL;
char *stt;
char *here = NULL;
int len = maxlen;
const int s2len = strlen (str2sub);
const int s4len = strlen (str4sub);

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

here = strstr (str, str2sub);
while (here != NULL)
{
int i = 0;

while ((maxlen > 0) && (str < here))
{
*st++ = *str++;
maxlen--;
}
if ((here == strt) || (isspace(here[-1]) && isspace(here[s2len])))
{
str += s2len;
while ((maxlen > 0) && (i {
*st++ = str4sub[i];
i++;
maxlen--;
}
}
here = strstr (here+s2len, str2sub);
}
while ((maxlen > 0) && (*str))
{
*st++ = *str++;
maxlen--;
}

if (maxlen <= 0)
return strt;
strncpy (strt, stt, len);
free (stt);
stt = NULL;
return strt;
}

int
wordcmp (const char* str1, const char* str2)
{
while ((*str1) && (*str2) && (*str1 != ' ') && (*str2 != ' ') && (*str1++ == *str2++))
;
if (((*str1 == ' ') && (*str2 == ' ')) || ((*str1 == 0) && (*str2 == 0)))
return 0;
else
return (str1[-1] - str2[-1]);
}

int
wordlen (const char* str)
{
int count = 0;
while ((str[count] != '\0') && (!isspace(str[count])))
count++;
return count;
}

void
word_stat (const char* str)
{
const char *sp = str;
struct wstat
{
int count;
char* pword;
};
struct wstat* pwstat = NULL;
int i;
int wnum = 0;
int wnummax = 100;

pwstat = calloc(wnummax, sizeof (struct wstat));
if (pwstat == NULL)
{
fprintf (stderr, "calloc failed.\n");
exit (1);
}
memset (pwstat, 0, wnummax*sizeof (struct wstat));
while (*sp)
{
while (isspace(*sp))
sp++;
if (*sp)
{
for (i=0; i {
if (0 == wordcmp(sp, pwstat[i].pword))
{
pwstat[i].count += 1;
break;
}
}
if (i == wnum)
{
pwstat[wnum].count = 1;
pwstat[wnum].pword = (char *)sp;
wnum++;
}
sp += wordlen(sp);
}
}
for (i=0; i {
int j;
printf ("[");
for (j=0; j printf ("%c", pwstat[i].pword[j]);
printf ("]");
printf ("--------%d\n", pwstat[i].count);
}
free (pwstat);
pwstat = NULL;
}

int
main(int argc, char **argv)
{
char test[100] = " this is a is is is a test test this test ";
char ttest[100] = " ";
printf ("ret: [%s]\n", discard_space(test));
printf ("rett: [%s]\n", discard_space(ttest));
subnstr (test, sizeof (test), "is", argv[1]);
printf ("ret: [%s]\n", discard_space(test));
subnstr (test, sizeof (test), "is", argv[2]);
printf ("ret: [%s]\n", discard_space(test));
word_stat(test);
return 0;
}