函数接口定义:
int length ( char *sp ); void convert ( char *sp );
其中 sp
是用户传入的参数。 函数length计算指针sp
所指字符串的串长,并返回。函数convert将指针sp
所指字符串倒置,convert函数无返回值。
主函数输入一个字符串s,分别调用length函数、convert函数,并在主函数中输出字符串s的串长及倒置后的字符串s。请填写完整主函数,并完成length函数和convert函数的定义。注意:不得使用库函数计算字符串的串长。
裁判测试程序样例:
#include <stdio.h> int length(char *sp); void convert( char *sp ); int main() { char s[81]; gets(s); //调用length函数,输出串长 //调用convert函数 //输出字符串s /* 请在这里填写答案 */
输入样例:
abcdef
输出样例:
6
fedcba
注意主函数最后不完整!!!!
int length(char *sp){
int len=0;
while(*sp!='\0'){
len++;
sp++;
}
return len;
}
void convert(char *sp){
int len=length(sp);
char *s=sp;
char *p=sp+len-1;
while(s<p){
char t=*s;*s=*p;*p=t;
s++;
p--;
}
}
printf("%d\n",length(s));
convert(s);
printf("%s\n",s);
}