c语言 编写函数用于进行俩个字符串的连接,编写函数用于统计一个字符串的长度,并在主函数中调用。求大神 编写一个函数将两个字符串连接起来,用C语言

c\u8bed\u8a00\u7f16\u5199\u7528\u4e8e\u4e24\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u7684\u51fd\u6570,\u5e76\u5e94\u7528\u5230\u4e3b\u51fd\u6570 \u4e3a\u4ec0\u4e48\u6211\u8fd0\u884c\u4f1a\u51fa\u73b0\u505c\u6b62\u5de5\u4f5c\u5462

#include #include void catStr(char str1[],char str2[],char str3[]){ int i=0,j=0; while(str1[i]!='\0') str3[i]=str1[i++]; while(str2[j]!='\0') str3[i+j]=str2[j++]; str3[i+j]='\0';}void main(){ char str1[100],str2[100],str3[100]; printf("Input the first string\uff1a"); gets(str1); printf("Input the second string\uff1a"); gets(str2); puts(str1); printf("\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f%d\n",strlen(str1)); puts(str2); printf("\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f%d\n",strlen(str2)); catStr(str1,str2,str3); puts(str3); printf("\u5904\u7406\u540e\u7684\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f%d\n",strlen(str3));}\u539f\u4ee3\u7801\u6709\u591a\u5904\u9519\u8bef\uff0c\u5c31\u4e0d\u4e00\u4e00\u6307\u51fa\uff0c\u8fd0\u884c\u51fa\u9519\u4e3b\u8981\u539f\u56e0\u662f\u8fdb\u5165\u4e86\u6b7b\u5faa\u73af\uff0c\u6211\u6839\u636e\u697c\u4e3b\u5199\u7684\u7a0d\u505a\u4e86\u4e00\u70b9\u4fee\u6539\uff0c\u697c\u4e3b\u53ef\u4ee5\u5bf9\u7167\u4e00\u4e0b~

\u9009\u81ea\uff1a\u300a\u7a0b\u5e8f\u903b\u8f91\u53caC\u8bed\u8a00\u300b\uff0c\u672c\u89c6\u9891\u4e3a\u5b57\u7b26\u4e32\u7684\u7b2c04\u5c0f\u8282\u3002\u4e3b\u8981\u8bb2\u89e3\u56db\u4e2a\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u5904\u7406\u51fd\u6570\uff0c\u5305\u62ec\uff1a\u6c42\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3001\u5b57\u7b26\u4e32\u7684\u8d4b\u503c\u3001\u5b57\u7b26\u4e32\u7684\u8fde\u63a5\u5df2\u7ecf\u5b57\u7b26\u4e32\u5927\u5c0f\u7684\u6bd4\u8f83\u3002\u5177\u4f53\u6709\u6848\u4f8b\u8bb2\u89e3\u3002

#include "stdio.h"

int catstr(char *a,char *b,char *c)
{
int i=0,j=0;
for(;*(a+i);i++)*(c+i)=*(a+i);
for(;*(b+j);i++,j++)*(c+i)=*(b+j);
*(c+i)=*(b+j);
return 0;
}

int lenstr(char *a)
{
int i=0;
for(;*(a+i);i++);
return i;
}

void main()
{
    char str1[]="Every one is ",str2[]="leaning C.",str12[50];
    int len1,len2,len12;
    len1=lenstr(str1);
    len2=lenstr(str2);
    catstr(str1,str2,str12);
    len12=lenstr(str12);
    printf("第一个字符串是:
%s
长度是:%d
",str1,len1);
    printf("第二个字符串是:
%s
长度是:%d
",str2,len2);    
    printf("合并后字符串是:
%s
长度是:%d
",str12,len12);    
}

已经运行过。



#include <stdio.h>

int lenStr (char str[])
{
int i = 0;
for (i = 0; str[i] != '\0'; i++)
{

}
return i;
}
char str[2000];
void catStr (char stra[],char strb[])
{

int len_a = lenStr(stra);
int len_b = lenStr(strb);
int i;
for (i = 0; i < len_a;i++)
{
str[i] = stra[i];
}
for (int j =0;j < len_b;j++)
{
str[i++] = strb[j];
}

}

int main ()
{
char stra[1000],strb[1000];

scanf ("%s%s",stra,strb);

int len_a = lenStr (stra);
int len_b = lenStr (strb);

printf("%s长度是:%d\n",stra,len_a);
printf("%s长度是:%d\n",strb,len_b);

catStr(stra,strb);

int len = lenStr(str);
printf ("%s长度是:%d\n",str,len);

return 0;
}

扩展阅读:c++编程 ... c语言gets 的用法 举例 ... c语言常用的32个关键字 ... c语言怎么写函数 ... c语言必背100代码 ... c#是什么语言 ... c++编程题经典100例 ... c语言自己编写函数 ... c语言常用函数大全超详细 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网