程序设计之字符串2
字符串
在字符串操作后切记最后得加上字符串结束标识符 ′ \ 0 ′ '\backslash0' ′\0′ 。
用数组作为函数的形参时,可以不定义数组的行数,但一定要定义数组的列数。
问题2_1
函数 f u n fun fun 的功能是:删除指针 p p p 所指字符串中的所有空白字符(包括制表符、回车符及换行符)。输入字符串时用 # 号结束输入。
代码2_1
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void fun(char *p){
int i, t;
char c[80];
for(i=0,t=0; p[i]; i++){
if(!isspace(*(p+1)))
c[t++] = p[i];
}
c[t] = '\0';
strcpy(p, c);
}
void main(void){
char c, s[80];
int i=0;
printf("Input a string:\n");
c = getchar();
while(c!='#'){
s[i] = c;
i++;
c = getchar();
}
s[i] = '\0';
fun(s);
printf("\nThe result:\n");
puts(s);
}
结果2_1

问题2_2
函数 f u n fun fun的功能是:求形参 s s s 所指字符串中最后一次出现的 t t t 所指字符串的地址,并通过函数值返回,在主函数中输出从此地址开始的字符串;若未找到,则函数值为 NULL。
例如: 形参 s s s 所指的字符串为

最低0.47元/天 解锁文章
490

被折叠的 条评论
为什么被折叠?



