c语言编程将两个字符串连接起来 用C语言编程:编写一个函数,将两个字符串连接起来

c\u8bed\u8a00\u7f16\u7a0b \u8981\u7528\u6307\u9488\u628a\u4e24\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u8d77\u6765\uff0c \u6211\u8fd9\u4e48\u7f16\u7684\u4e0d\u77e5\u9053\u54ea\u9519\u4e86\u3002\u6c42\u6307\u6559\uff0c\u6c42\u6b63\u89e3\u3002\u8c22\u8c22

\u8bed\u6cd5\u6709\u9519\u8bef\uff0c\u53ef\u4ee5\u6309\u7167\u5982\u4e0b\u65b9\u6cd5\u5b9e\u73b0\u7528\u6307\u9488\u628a\u4e24\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u8d77\u6765\uff1a
1\u3001\u7b2c\u4e00\u6b65\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u9879\u76ee\u548c.c\u6587\u4ef6\uff0c\u89c1\u4e0b\u56fe\uff0c\u8f6c\u5230\u4e0b\u9762\u7684\u6b65\u9aa4\u3002



2\u3001\u7b2c\u4e8c\u6b65\uff0c\u6267\u884c\u5b8c\u4e0a\u9762\u7684\u64cd\u4f5c\u4e4b\u540e\uff0c\u5b9a\u4e49\u53d8\u91cf\u7c7b\u578b\uff0c\u89c1\u4e0b\u56fe\u7684\u4ee3\u7801\uff0c\u8f6c\u5230\u4e0b\u9762\u7684\u6b65\u9aa4\u3002



3\u3001\u7b2c\u4e09\u6b65\uff0c\u6267\u884c\u5b8c\u4e0a\u9762\u7684\u64cd\u4f5c\u4e4b\u540e\uff0c\u8c03\u7528cpy\u51fd\u6570\uff0c\u89c1\u4e0b\u56fe\u7684\u4ee3\u7801\uff0c\u8f6c\u5230\u4e0b\u9762\u7684\u6b65\u9aa4\u3002



4\u3001\u7b2c\u56db\u6b65\uff0c\u6267\u884c\u5b8c\u4e0a\u9762\u7684\u64cd\u4f5c\u4e4b\u540e\uff0c\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u4e86\u89e3\u5e76\u5b9a\u4e49\u53d8\u91cf\u7c7b\u578b\uff0c\u89c1\u4e0b\u56fe\u7684\u4ee3\u7801\uff0c\u8f6c\u5230\u4e0b\u9762\u7684\u6b65\u9aa4\u3002



5\u3001\u7b2c\u4e94\u6b65\uff0c\u6267\u884c\u5b8c\u4e0a\u9762\u7684\u64cd\u4f5c\u4e4b\u540e\uff0c\u4f7f\u7528For\u8bed\u53e5\u548cif\u8bed\u53e5\u6765\u8fdb\u884c\u5224\u65ad\uff0c\u89c1\u4e0b\u56fe\u7684\u4ee3\u7801\uff0c\u8f6c\u5230\u4e0b\u9762\u7684\u6b65\u9aa4\u3002




6\u3001\u7b2c\u516d\u6b65\uff0c\u6267\u884c\u5b8c\u4e0a\u9762\u7684\u64cd\u4f5c\u4e4b\u540e\uff0c\u6dfb\u52a0\u4e00\u4e2a\u5b57\u7b26\u4e32\u7ec8\u6b62\u7b26\uff0c\u5e76\u5c06\u5176\u8f93\u51fa\u5230main\u51fd\u6570\u4e2d\uff0c\u89c1\u4e0b\u56fe\u7684\u6807\u8bb0\u7684\u4ee3\u7801\uff0c\u8f6c\u5230\u4e0b\u9762\u7684\u6b65\u9aa4\u3002




7\u3001\u7b2c\u4e03\u6b65\uff0c\u6267\u884c\u5b8c\u4e0a\u9762\u7684\u64cd\u4f5c\u4e4b\u540e\uff0c\u6267\u884c\u5e76\u5f97\u51fa\u7ed3\u679c\uff0c\u4e24\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u8d77\u6765\u4e86\uff0c\u89c1\u4e0b\u56fe\u3002\u8fd9\u6837\uff0c\u5c31\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898\u4e86\u3002



1\u3001\u65b0\u5efa\u4e00\u4e2a\u5de5\u7a0b\u548c.c\u6587\u4ef6\uff0c\u8f93\u5165\u5934\u6587\u4ef6\u548c\u4e3b\u51fd\u6570\u3002

2\u3001\u5b9a\u4e49\u53d8\u91cf\u7c7b\u578b\u3002

3\u3001\u8c03\u7528cpy\u51fd\u6570\u3002

4\u3001\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u5b9a\u4e49\u53d8\u91cf\u7c7b\u578b\u3002

5\u3001\u7528\u4e00\u4e2aFor \u8bed\u53e5\u548cif\u8bed\u53e5\u5224\u65ad\u662f\u5426\u4e3a\u5143\u97f3\u3002

6\u3001\u6700\u540e\u52a0\u4e00\u4e2a\u5b57\u7b26\u4e32\u7ed3\u675f\u7b26\uff0c\u5e76\u5728\u4e3b\u51fd\u6570\u4e2d\u8f93\u51fa\u3002

7\u3001\u7f16\u8bd1\u3002\u8fd0\u884c\u3002

// void fun(char p1[], char p2[]) 的最后一行
p2[j]='\0';
// 改为:
p1[n]='\0';

 

#include <stdio.h>
void fun(char p1[], char p2[])
{
int i=0,j=0,n=0;
while(p1[i]!=0)
{
i++;
n++;
}
while(p2[j]!=0)
{
p1[n]=p2[j];
n++;
j++;
}
p1[n]='\0';
}
main()
{char s1[80], s2[40] ;void NONO ();
     printf("Enter s1 and s2:
");
     scanf("%s%s", s1, s2);
     printf("s1=%s
", s1);
     printf("s2=%s
", s2);
     printf("Invoke fun(s1,s2):
");
     fun(s1, s2);
     printf("After invoking:
");
     printf("%s
", s1);
     //NONO();
}


void fun(char p1[], char p2[])函数倒数第二行错误
将p2[j]='\0';改为p2[i+j]='\0';

推荐我写的一种将两个字符串连接在一起的函数
#include <assert.h>
void fun(char *p1, char *p2)
{
assert(p1 != NULL); //断言p1不为空
assert(p2 != NULL); //断言p2不为空
while(*p1 != NULL)
p1++;
while((*p1++ = *p2++) != NULL)
;
}
希望对你有所帮助!

将while(p1[i]!=0)改为while(p1[i]!=’\0‘)
将while(p2[j]!=0)改为while(p2[j]!='\0')

  • C璇█闂 缂栧啓涓绋嬪簭灏嗕袱涓瓧绗︿覆杩炶捣鏉
    绛旓細锛 娉ㄦ剰瑕佺暀1浣嶄繚瀛樼粨鏉熺'\0' strcpy(str1,"abcd");strcpy(str2,"efgh"); printf("鍘瀛楃涓鍒嗗埆涓猴細%s %s\n\n",str1,str2); strcat(str1,str2); printf("鐢╯trcat鎷兼帴鍚庡瓧绗︿覆锛%s\n\n",str1); strcpy(str1,"abcd");strcpy(str2,"efgh"); p=&str1[...
  • 缂栧啓涓涓绋嬪簭,灏嗕袱涓瓧绗︿覆杩炴帴璧锋潵,骞惰緭鍑(涓嶈浣跨敤strcat鍑芥暟)銆傜敤C...
    绛旓細include<stdio.h> void main(){ char s1[80],s2[40];int i=0,j=0;printf("\ninput stringl:");scanf("%s",s1);printf("input string2:");scanf("%s",s2);while(s1[i]!='\0')i++;while(s2[j]!='\0')s1[i++]=s2[j++];s1[i]='\0';printf("The new string is:%s...
  • C璇█涓庝箞鏍灏嗕袱涓瓧绗︿覆杩炴帴璧锋潵
    绛旓細1)绠鍗曟潵锛岀洿鎺ョ敤 strcat 鍑芥暟锛岄渶瑕佸寘鍚ご鏂囦欢 string.h2)鑷繁瀹炵幇鐨勮瘽涔熶笉楹荤儲锛屼絾鏄鑰冭檻涓浜涚粏鑺傦細鍋囪涓や釜瀛楃涓鎸囬拡涓 str1锛宻tr2 锛岀幇鍦ㄨ璁 str1 鍜 str2 杩炴帴鎴愪竴涓柊鐨勫瓧绗︿覆銆俛.鑰冭檻鎸囬拡 str1,str2 鏄惁闈炵┖b.濡傛灉灏唖tr2鐨勫唴瀹圭洿鎺ヨ繛鎺ュ埌str1鐨勬湯灏撅紝瑕佽冭檻str1鏄惁鏈夎冻澶熺殑鍓╀綑...
  • 鐢C璇█缂栫▼:灏嗕袱涓瓧绗︿覆杩炴帴璧锋潵,涓嶈鐢╯trcat鍑芥暟
    绛旓細include <stdio.h> int main(){ char a[10],b[10],c[20];int i,j;gets(a);gets(b);i=j=0;while (a[i]!='\0'){ c[i]=a[i];i++;} while (b[j]!='\0'){ c[i]=b[j];i++;j++;} c[i]='\0';printf("a: %s,b: %s cat--%s\n",a,b,c);return 0;}...
  • C璇█鎬庝箞鍚堝苟涓や釜瀛楃涓?
    绛旓細鍏堝姞澶存枃浠#include\x0d\x0achar a[10]="123";\x0d\x0a char b[10]="abc";\x0d\x0a strcat(a,b); //杩炴帴涓や釜瀛楃涓锛岃繛鎺ュ悗鐨勫瓧绗︿覆瀛樻斁鍦╝涓紝鏁扮粍a涓湁瓒冲绌洪棿\x0d\x0a printf("%s",a); //杈撳嚭杩炴帴鍚庣殑瀛楃涓瞈x0d\x0a\x0d\x0a鎴栵細\x0d\x0a#include...
  • c璇█缂栫▼ 瑕佺敤鎸囬拡鎶婁袱涓瓧绗︿覆杩炴帴璧锋潵, 鎴戣繖涔堢紪鐨勪笉鐭ラ亾鍝敊浜...
    绛旓細璇硶鏈夐敊璇紝鍙互鎸夌収濡備笅鏂规硶瀹炵幇鐢ㄦ寚閽鎶婁袱涓瓧绗︿覆杩炴帴璧锋潵锛1銆佺涓姝ワ紝鍒涘缓涓涓柊椤圭洰鍜.c鏂囦欢锛岃涓嬪浘锛岃浆鍒颁笅闈㈢殑姝ラ銆2銆佺浜屾锛屾墽琛屽畬涓婇潰鐨勬搷浣滀箣鍚庯紝瀹氫箟鍙橀噺绫诲瀷锛岃涓嬪浘鐨勪唬鐮侊紝杞埌涓嬮潰鐨勬楠ゃ3銆佺涓夋锛屾墽琛屽畬涓婇潰鐨勬搷浣滀箣鍚庯紝璋冪敤cpy鍑芥暟锛岃涓嬪浘鐨勪唬鐮侊紝杞埌涓嬮潰鐨勬楠ゃ4銆佺鍥...
  • 缂栧啓涓涓嚱鏁瀹炵幇涓や釜瀛楃涓鐨杩炴帴(涓嶄娇鐢ㄥ簱鍑芥暟strcat).杩欎釜鐢C璇...
    绛旓細void fun (char s1[],char s2[]){ int i,j;for (i=0;s1[i] !=鈥橽0鈥; i++); /*姹傚嚭鐨刬涓簆A瀛楃鐨勬婚暱搴︼紝鍖呮嫭缁撴潫鏍囪浣*/ for (j=0;s2[j] !=鈥橽0鈥; j++)s1[i++]=s2[j]; /*灏唒B瀛楃涓茶繛鍦╬A瀛楃涓茬殑鍚庨潰*/ s1[i]='\0鈥; /*鍦ㄥ瓧绗︿覆鏈鍚庡姞涓婄粨鏉熸爣璁扮*...
  • C璇█:灏嗕袱涓瓧绗︿覆杩炴帴璧锋潵銆
    绛旓細int main(){ char s1[80],s2[40];int i=0,j=0;printf("\nInput the first string:");scanf("%s",s1);printf("\nInput the second string:");scanf("%s",s2);while (s1[i] !='\0')i++;while (s2[j] !='\0')s1[i++]=s2[j++]; /* 鎷兼帴瀛楃鍒皊1 */ s1[i] ...
  • C璇█缂栫▼:杈撳叆2涓瓧绗︿覆,灏嗗叾杩炴帴鍚庤緭鍑.
    绛旓細鎬濊矾锛涓や釜瀛楃涓鐨鎷兼帴鍙互浣跨敤strcat鍑芥暟銆俿trcat鍑芥暟鍘熷瀷锛 char *strcat(char *s1,char *s2);闇瑕佸紩鍏ュご鏂囦欢锛#include <string.h> 鍔熻兘锛氭妸s2鎵鎸囧瓧绗︿覆娣诲姞鍒皊1缁撳熬澶勫苟娣诲姞'\0'銆傛敞鎰忥細s1蹇呴』鏈夎冻澶熺殑绌洪棿鏉ュ绾硈1鍜宻2鐨勫瓧绗︿覆銆傚弬鑰冧唬鐮侊細include "stdio.h" #include "string.h" int ...
  • c璇█缂栫▼棰:瀹氫箟鍑芥暟con()灏嗕袱涓瓧绗︿覆杩炴帴,涓诲嚱鏁拌緭鍏ヤ袱涓瓧绗︿覆,璋...
    绛旓細include <stdio.h> void con(char*s1,char*s2){int i,j;for(i=0;s1[i];i++);for(j=0;s1[i++]=s2[j++];);} int main(){ char s1[200],s2[100];gets(s1);gets(s2);con(s1,s2);puts(s1);return 0;}
  • 扩展阅读:c十十编程要学多久 ... 输入两个字符串并连接 ... c语言字符拼接在一起 ... c语言自动生成器 ... c++入门程序代码 ... c++如何连接两个字符串 ... c语言搜题神器app ... 两个字符串的连接起来 ... c语言将两个字符串拼接 ...

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