lzth.net
当前位置:首页 >> 在C++中,如何不用字符串函数就可将两个字符串连接起来? >>

在C++中,如何不用字符串函数就可将两个字符串连接起来?

直接用 string str1,str2,str3; cin>>str1>>str2; str3=str1+str2; cout 评论0 0 0

#include <stdio.h>int main(){ char a[7] = "abc"; char b[4] = "123"; int i; int j; printf(" string a is:\n"); printf("%s\n",a); printf(" string b is:\n"); printf("%s\n\n",b); i = 0; while(a[i]) { i++; } j = 0; while(b[j]) { a[i] = b[j]; i++; j++; } a[i] = '\0'; printf("

str1 + str2;

完整源代码#include&lt;stdio.h&gt;void main(){char s1[80],s2[40];int i=0,j=0;printf("\ninput string1:");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\n",s1);}

Char* strcat ( char * dst , const char * src ){char * cp = dst;while( *cp !='\0')cp++;while( (*cp++ = *src++)!='\0' ) ;return( dst );

输入字符串时记录他的长度即可 char a[]; char c; int n; cin>>c; while(c!=0) { c=a[n]; n++; } n为a的长度,不含字符串的结束符

#include<iostream> using namespace std; void main() { string string1,string2; string1="do all"; string2="by yourself" string1=string1+string2;//连接string1和string2 cout<<string1;//输出为do all by yourself }

#include<stdio.h>void main(){ int i,j; char a[40],b[40],c[80]; printf("input the first string:\n"); gets(a); printf("input the second string:\n"); gets(b); for(i=0;a[i]!='\0';i++) c[i]=a[i]; for(j=0;a[j]!='\0';j++) c[i+j]=b[j]; c[i+j] = '\0'; puts(c);}

将串2拷贝到串1结尾

public String StrCat(String s1,String s2){ return s1+s2;}

网站首页 | 网站地图
All rights reserved Powered by www.lzth.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com