编一程序,将两个字符串连接起来,不要用strcat函数.

2025-03-01 18:34:55
推荐回答(3个)
回答1:

#define LENGTH 40
#include
void main(void)
{
 char str1[LENGTH + 1],str2[LENGTH + 1];
 char result[2 * LENGTH + 1];
 int len1,len2;
 cout<<"Input the first string:"< cin>>str1;
 cout<<"Input the second string."< cin>>str2;
 len1 = 0;
 while(str1[len1] != '\0')
 {
  result[len1] = str1[len1];
  len1 ++;
 }
 len2 = 0;
 while(str2[len2] != '\0')
 {
  result[len1] = str2[len2];
  len1 ++;
  len2 ++;
 }
 result[len1] = '\0';
 cout<}

  运行该程序并输入:
  Input the first string:
  Good↙
  Input the second string:
  bye↙
  运行结果为:
  Goodbye
  程序中第一个循环把str1的内容送到result中,但没有送'\0',从第一个字符串的末尾位置开始,第二个循环把str2送到result中,同样没有送'\0',因此在最后我们为新的字符串加一个'\0'表示字符串的结束,最后用printf输出这个字符串。

回答2:

#include
int main()
{char a[10],b[10],c[21];
int i,j;
scanf("%s",a);
scanf("%s",b);
for(i=0;a[i]!='\0';i++)
c[i]=a[i];
for(j=0;b[j]!='\0';j++)
c[i+j]=b[j];
c[i+j]='\0';
printf("%s\n",c);}

回答3:

等楼下解决