C语言数组方法实现字符串格式化输出
好的,以下是使用数组的方法以C语言实现该功能的代码:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = 'Beijing';
int length = strlen(str);
int i, j;
// 定义一个二维字符数组
char pattern[length][length];
// 将字符串按要求存储到数组中
for (i = 0; i < length; i++) {
for (j = 0; j < i; j++) {
pattern[i][j] = ' ';
}
for (j = i; j < length; j++) {
pattern[i][j] = str[j];
}
}
// 输出数组内容
for (i = 0; i < length; i++) {
for (j = 0; j < length; j++) {
printf('%c', pattern[i][j]);
}
printf('
');
}
return 0;
}
运行上述代码将输出:
B
e
i
j
i
n
此代码使用了一个二维字符数组来存储字符串的各个字符,并按照要求打印出相应的格式。
原文地址: https://www.cveoy.top/t/topic/bhgP 著作权归作者所有。请勿转载和采集!