我更喜欢char array
和stdio
:
#include <cstdio>
// #include <cstdlib>
#define SCREEN_SIZE_STR "80"
int main(){
const char s1[] =
"12345678901234567890123456789012345678901234567890123456789012345678901234567890"
"12345678901234567890123456789012345678901234567890123456789012345678901234567890"
"12345678901234567890123456789012345678901234567890123456789012345678901234567890"
"12345678901234567890123456789012345678901234567890123456789012345678901234567890"
"12345678901234567890123456789012345678901234567890123456789012345678901234567890";
const char s2[] =
"abcdefghijklmnopabcdefghijklmnopabcdefghijklmnopabcdefghijklmnopabcdefghijklmnop"
"abcdefghijklmnopabcdefghijklmnopabcdefghijklmnopabcdefghijklmnopabcdefghijklmnop"
"abcdefghijklmnopabcdefghijklmnopabcdefghijklmnopabcdefghijklmnopabcdefghijklmnop"
"abcdefghijklmnopabcdefghijklmnopabcdefghijklmnopabcdefghijklmnopabcdefghijklmnop"
"abcdefghijklmnopabcdefghijklmnopabcdefghijklmnopabcdefghijklmnopabcdefghijklmnop";
int i1 = 0, i2 = 0;
while(s1[i1] != '\0' && s2[i2] != '\0'){ // print both string
i1 += printf("%."SCREEN_SIZE_STR"s", s1 + i1);
i2 += printf("%."SCREEN_SIZE_STR"s", s2 + i2);
}
while(s1[i1] != '\0') // print the remaining of s1
i1 += printf("%."SCREEN_SIZE_STR"s", s1 + i1);
while(s2[i2] != '\0') // print the remaining of s2
i2 += printf("%."SCREEN_SIZE_STR"s", s2 + i2);
// system("pause");
return 0;
}
如果您正在使用std::string
:
const char *s1 = string1.c_str();
const char *s2 = string2.c_str();