使用函数处理字符串
程序要求:给定程序中,函数fun的功能是:将形参s所指字符串中的所有字母字符顺序前移,其他字符顺序后移,处理后新字符串的首地址作为函数值返回。
例如,s所指字符串为:asd123fgh543df,处理后新字符串为:asdfghdf123543。请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。
注意:源程序存放在考生文件夹下的BLANK1.C中。不得增行或删行,也不得更改程序的结构!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *fun(char *s)
{
int i, j, k, n;
char *p, *t;
n = strlen(s) + 1;
t = (char *)malloc(n * sizeof(char));
p = (char *)malloc(n * sizeof(char));
j = 0;
k = 0;
for (i = 0; i < n; i++)
{
if (((s[i] >= 'a') && (s[i] <= 'z')) || ((s[i] >= 'A') && (s[i] <= 'Z')))
{ /**********found**********/
t[j] = s[i];
j++;
}
else
{
p[k] = s[i];
k++;
}
}
/**********found**********/
for (i = 0; i < k ; i++)
t[j + i] = p[i];
/**********found**********/
t[j + k] = '\0';
return t;
}
main()
{
char s[80];
printf("Please input: ");
scanf("%s", s);
printf("\nThe result is: %s\n", fun(s));
运行结果
第一组:
Please input: asd123fgh543df
The result is: asdfghdf123543
第二组:
Please input: dqd554wdq48
The result is: dqdwdq55448