把 m(1≤m≤10)个字符串连接起来,组成一个新串,放入pt中

来源:百度知道 编辑:UC知道 时间:2024/06/30 04:26:32
/*
请勿改动主程序main及其他给定函数中的任何内容,
仅在指定函数内的花括号中填入你编写的若干语句。

函数fun的功能是:把 m(1≤m≤10)个字符串连接起来,组成一个新串,放入pt中。
例如:把3个串:"abc","CD","EF"串连起来,结果是"abcCDEF"。
*/

#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
int fun ( char str[][10], int m, char *pt )
{

}
main( )
{
int n, h ;
char s[10][10], p[120] ;
system("cls");
printf( "\nPlease enter n:" ) ;
scanf("%d", &n) ;
printf( "\nPlease enter %d string:\n", n ) ;
for ( h = 0; h < n; h++ ) scanf("%s", s[h]) ;
fun(s, n, p) ;
printf( "\nThe result is : %s\n", p) ;
}

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *fun(const char **sour, int m, char *dest)
{
int len;
char *d;

if (NULL == dest || NULL == sour)
return NULL;

for (d = dest; 0 < m-- && NULL != *sour; ++sour, d += len){
len = strlen(*sour);
memcpy(d, *sour, len);
}
*d = '\0';

return dest;
}

int main()
{
char *s[] = {"1653", "hong", "(char*)&test"};
char r[128];

system("color f0");
fun(s, 3, r);
printf("%s\n", r);
}

//在fun()内判断*sour是否为NULL,这样当传递过来的参数**sour是以NULL结尾时,如果sour中字符串个数小于m时,能避免程序崩溃。