C语言..strcat
来源:百度知道 编辑:UC知道 时间:2024/05/14 16:42:05
要求是这样的..
string1 string2
int n
要求用strcat把string1string2组合起来 但是输入的n决定用string2的字符数..
比如..string1为'hello 'string2为 'world!' n为3
那么要输出hello wor
如果n为5 则:
hello world
谢谢大家啦..
在线等..
string1 string2
int n
要求用strcat把string1string2组合起来 但是输入的n决定用string2的字符数..
比如..string1为'hello 'string2为 'world!' n为3
那么要输出hello wor
如果n为5 则:
hello world
谢谢大家啦..
在线等..
不是有个现成的strncat的库函数么?
原型:char *strncat(char *dest,char *src,int n);
用法:#include <string.h>
功能:把src所指字符串的前n个字符添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0'。
说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。返回指向dest的指针。
举例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char d[20]="Golden Global";
char *s="View WinIDE Library";
strncat(d,s,8);
printf("%s",d);
return 0;
}
结果为:
Golden GlobalView Win
Process returned 0 (0x0) execution time : 0.063 s
Press any key to continue.
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
char *strcat(char *str1,char *str2,int n)
{
char *str = (char *)malloc(sizeof(str1)+n*sizeof(char));
char *p = str1,*q = str;
while