gpt4 book ai didi

c++ - 字符串不会用 cout 打印

转载 作者:行者123 更新时间:2023-11-30 05:30:14 25 4
gpt4 key购买 nike

<分区>

我想做的是将两个字符串复制到第三个字符串,在第三个字符串中留下与前两个字符串的字符总和一样多的空格,而不使用 strcpy 或任何类似的东西,下面的代码不会打印 stc 字符串。它运行但不会打印它。

#include <iostream>
#include <conio.h>
#include <string.h>

using namespace std;

int main()
{
int i, j = 0, k = 0, str_1 = 0, str_2 = 0, str_3 = 0, space = 0;
char sta[40];
char stb[40];
char stc[100] = { };

cout << "Enter sentence: " << endl << endl;
cin.getline(sta, 39);
cout << "Enter Second Sentence: " << endl << endl;
cin.getline(stb, 39);

for (i = 0; i < 40; i++) {
if (sta[i] == '\0') {
break;
} else {
str_1++;
}
}
cout << "The first sentence has: " << str_1 << " characters" << endl << endl;

for (i = 0; i < 40; i++) {
if (stb[i] == '\0') {
break;
} else {
str_2++;
}
}
cout << "The second sentence has: " << str_2 << " characters" << endl << endl;

space = str_1 + str_2;
for (i = 0; sta[i] != '\0'; i++) {
stc[space] = sta[i];
space++;
str_3++;
}
for (j = 0; stb[j] != '\0'; j++) {
stc[space] = stb[j];
space++;
str_3++;
}
stc[space] = '\0';
cout << "The third sentence is: " << stc << endl << endl;
cout << "And has " << str_3 << " characters " << endl;
return 0;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com