30 lines
1.1 KiB
C
30 lines
1.1 KiB
C
|
|
#include <stdio.h>
|
||
|
|
#include <string.h>
|
||
|
|
|
||
|
|
#define MAX_LENGTH 30 // 定义一个最大长度常量
|
||
|
|
int main() {
|
||
|
|
char s1[] = "abcdefg"; char s2[] = "cdefgh";
|
||
|
|
// 获取字符串的长度
|
||
|
|
int len1 = strlen(s1); int len2 = strlen(s2);
|
||
|
|
// 使用固定大小的数组
|
||
|
|
char s1_with_space[MAX_LENGTH]; char s2_with_space[MAX_LENGTH];
|
||
|
|
// 填充带空格的字符串
|
||
|
|
s1_with_space[0] = ' '; // 添加空格
|
||
|
|
strcpy(s1_with_space + 1, s1); // 复制原字符串到新字符串
|
||
|
|
s2_with_space[0] = ' '; // 添加空格
|
||
|
|
strcpy(s2_with_space + 1, s2); // 复制原字符串到新字符串
|
||
|
|
for (int i = 0; i <= len1; i++) { // 打印矩阵
|
||
|
|
for (int j = 0; j <= len2; j++) {
|
||
|
|
if (i == 0 && j <=len2) {
|
||
|
|
printf("%c\t", s2_with_space[j]); // 打印第二行的字符
|
||
|
|
} else if (j == 0 && i <= len1) {
|
||
|
|
printf("%c\t", s1_with_space[i]); // 打印第一列的字符
|
||
|
|
} else if (i > 0 && j > 0) {// 打印组合字符
|
||
|
|
printf("%c%c\t", s1_with_space[i], s2_with_space[j]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
printf("\n");
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|