编程题:连接两个字符串
输入第一个字符串: google 输入第二个字符串: noobdream 连接后: googlenoobdream
#include <stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include <windows.h> #define N 50 int main(void) { char string[N],string1[N],string2[N]; puts("请输入第一个字符串"); gets(string1); int n1 = strlen(string1); string1[n1] = '\0'; strcpy(string, string1);//直接把第一个字符串复制给合并的 puts("请输入第二个字符串"); gets(string2); int n2 = strlen(string2); string2[n2] = '\0'; int i = 0; while (string2[i] != '\0') {//第二个字符串排在后面 string[n1] = string2[i]; n1++; i++; } string[n1] = '\0'; puts("合并字符串为:"); puts(string);
return 0; }
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char string1[1024]={0};
char string2[1024]={0};
cin>>string1>>string2;
strcpy(string1+strlen(string1),string2);
cout<<string1<<endl;
}
int lengthOfString(char * source)
if(*source==0)
return 0;
return 1+lengthOfString(source+1);
void append(char * string1,char * string2)
char * start=string1+lengthOfString(string1);// '\0'的位置
while(*string2)
*start++=*string2++;
*start=*string2;
char s1[1024];
char s2[1024];
cin>>s1>>s2;
append(s1,s2);
cout<<s1<<endl;
if(!*source)
void copyString(char * source,char * destination)
if(source==NULL || destination==NULL)
return;
*destination=*source;
copyString(source+1,destination+1);
char * concatenate(char * s1,char * s2)
int length=lengthOfString(s1)+lengthOfString(s2);
char * resultString=new char[length+1];
copyString(s1,resultString);
copyString(s2,resultString+lengthOfString(resultString));
return resultString;
char * result=concatenate(s1,s2);
cout<<result<<endl;
#include <stdi...
用户登录可进行刷题及查看答案
#include <stdio.h> #include <string.h> int main() { char s1[100], s2[100]; printf("输入第一个字符串: "); scanf("%s", s1);
printf("输入第二个字符串: "); scanf("%s", s2);
int len1 = strlen(s1); int len2 = strlen(s2); //printf("%d %d", len1, len2); for (int i = 0; i <= len2; i++) { s1[i + len1] = s2[i]; }
printf("%s\n", s1); // system("pause"); return 0; }
登录后提交答案