所属年份:2011.9;
请编写函数fun,其功能是将形参s所指字符串放入形参a所指的字符数组中,使a中存放同样的字符串。说明:不得使用系统提供的字符串函数。
#include <stdio.h> #define N 20 void fun( char *a , char *s) { } main() { char s1[N], *s2="abcdefghijk"; fun( s1,s2); printf("%s\n", s1); printf("%s\n", s2); }
【解题思路】
要将s所指的字符串存入a所指的字符串中,程序要求不能使用系统提供的字符串函数,本题可以使用循环语句,依次取出a所指字符串中的元素,将其存入s所指的字符串中,最后为s所指的字符串添加结束标识’\0’。
【参考答案】
void fun( char *a , char *s) { while(*s!='\0') { *a=*s; a++; s++; } *a='\0'; }