C编程:将两个字符串输出为相邻的两列
C programming: ouput two strings as two columns next to each other
我对我用 C 编写的程序有疑问。我将在两列中并排写两个不同的字符串。我没有找到我的问题的明确答案,因为他们几乎总是给出长度或数量已知的数字示例。
我有两个字符串,最大长度为 1500 个字符,但对我来说长度未知。让我们为了学习而给他们这些值:
char string1[] = "The independent country is not only self-governed nation with own authorities.";
char string2[] = "This status needs the international diplomatic recognition of sovereignty.";
我想把它们并排写,列宽二十个字符。我已将列之间的差异设置为常规 'tab'。像这样:
The independent coun This status needs th
try is not only self e international dipl
-governed nation wit omatic recognition o
h own authorities. f sovereignty.
我试过以下代码,但它没有效果,因为我不知道如何调整它以适应字符串的长度。它也刚好适合写五行。我也收到以下错误。
谁能给我一个例子说明如何做到这一点,也许可以使用预定义的 c 函数以避免使用 for 循环。
void display_columns(char *string1, char *string2);
int main()
{
char string1[] = "The independent country is not only self-governed nation with own authorities.";
char string2[] = "This status needs the international diplomatic recognition of sovereignty.";
display_columns(string1,string2);
}
void display_columns(char *string1, char *string2)
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0+20*i;j<20+20*i;j++)
{
printf("%c",string1[j]);
}
printf("\t");
for(j=0+20*i;j<20+20*i;j++)
{
printf("%c",string2[j]);
}
}
}
要打印单个字符,请使用:
printf("%c",string1[j]);
或
putchar(string1[j]);
这就是警告和分段错误的原因。
通过此修复,程序可以正常运行,您只需在循环的最后部分打印一个换行符:
for(i=0;i<5;i++)
{
for(j=0+20*i;j<20+20*i;j++)
{
putchar(string1[j]);
}
printf("\t");
for(j=0+20*i;j<20+20*i;j++)
{
putchar(string2[j]);
}
putchar('\n');
}
更新:对于使用可变长度字符串的函数,试试这个:
void display_columns(char *string1, char *string2)
{
int i,j;
int len1 = strlen(string1);
int len2 = strlen(string2);
int maxlen = (len1 > len2) ? len1 : len2;
int numloops = (maxlen + 20 - 1) / 20;
for(i=0; i<numloops; i++)
{
for(j=0+20*i;j<20+20*i;j++)
{
if (j < len1)
putchar(string1[j]);
else
putchar(' '); // Fill with spaces for correct alignment
}
printf("\t");
for(j=0+20*i;j<20+20*i;j++)
{
if (j < len2)
putchar(string2[j]);
else
break; // Just exit from the loop for the right side
}
putchar('\n');
}
}
我想这是更通用的方法。
void print_line(char *str, int *counter) {
for (int i = 0; i < 20; i++) {
if (str[*counter] != '[=10=]') {
printf("%c", str[*counter]);
*counter += 1;
}
else { printf(" "); }
}
}
void display_columns(char *string1, char *string2)
{
int counter = 0, counter2 = 0;
while (1) {
print_line(string1, &counter);
printf("\t");
print_line(string2, &counter2);
printf("\n");
if (string1[counter] == '[=10=]' && string2[counter2] == '[=10=]') {
break;
}
}
}
我对我用 C 编写的程序有疑问。我将在两列中并排写两个不同的字符串。我没有找到我的问题的明确答案,因为他们几乎总是给出长度或数量已知的数字示例。
我有两个字符串,最大长度为 1500 个字符,但对我来说长度未知。让我们为了学习而给他们这些值:
char string1[] = "The independent country is not only self-governed nation with own authorities.";
char string2[] = "This status needs the international diplomatic recognition of sovereignty.";
我想把它们并排写,列宽二十个字符。我已将列之间的差异设置为常规 'tab'。像这样:
The independent coun This status needs th
try is not only self e international dipl
-governed nation wit omatic recognition o
h own authorities. f sovereignty.
我试过以下代码,但它没有效果,因为我不知道如何调整它以适应字符串的长度。它也刚好适合写五行。我也收到以下错误。
谁能给我一个例子说明如何做到这一点,也许可以使用预定义的 c 函数以避免使用 for 循环。
void display_columns(char *string1, char *string2);
int main()
{
char string1[] = "The independent country is not only self-governed nation with own authorities.";
char string2[] = "This status needs the international diplomatic recognition of sovereignty.";
display_columns(string1,string2);
}
void display_columns(char *string1, char *string2)
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0+20*i;j<20+20*i;j++)
{
printf("%c",string1[j]);
}
printf("\t");
for(j=0+20*i;j<20+20*i;j++)
{
printf("%c",string2[j]);
}
}
}
要打印单个字符,请使用:
printf("%c",string1[j]);
或
putchar(string1[j]);
这就是警告和分段错误的原因。
通过此修复,程序可以正常运行,您只需在循环的最后部分打印一个换行符:
for(i=0;i<5;i++)
{
for(j=0+20*i;j<20+20*i;j++)
{
putchar(string1[j]);
}
printf("\t");
for(j=0+20*i;j<20+20*i;j++)
{
putchar(string2[j]);
}
putchar('\n');
}
更新:对于使用可变长度字符串的函数,试试这个:
void display_columns(char *string1, char *string2)
{
int i,j;
int len1 = strlen(string1);
int len2 = strlen(string2);
int maxlen = (len1 > len2) ? len1 : len2;
int numloops = (maxlen + 20 - 1) / 20;
for(i=0; i<numloops; i++)
{
for(j=0+20*i;j<20+20*i;j++)
{
if (j < len1)
putchar(string1[j]);
else
putchar(' '); // Fill with spaces for correct alignment
}
printf("\t");
for(j=0+20*i;j<20+20*i;j++)
{
if (j < len2)
putchar(string2[j]);
else
break; // Just exit from the loop for the right side
}
putchar('\n');
}
}
我想这是更通用的方法。
void print_line(char *str, int *counter) {
for (int i = 0; i < 20; i++) {
if (str[*counter] != '[=10=]') {
printf("%c", str[*counter]);
*counter += 1;
}
else { printf(" "); }
}
}
void display_columns(char *string1, char *string2)
{
int counter = 0, counter2 = 0;
while (1) {
print_line(string1, &counter);
printf("\t");
print_line(string2, &counter2);
printf("\n");
if (string1[counter] == '[=10=]' && string2[counter2] == '[=10=]') {
break;
}
}
}