水平方向
#include <stdio.h>
#include <stdbool.h>
#define MAXWORDCOUNT 10
int main(int argc, char **argv) {
int arr[MAXWORDCOUNT];
char c;
for (int i = 0; i< MAXWORDCOUNT; ++i) {
int count = 0;
bool start = false;
while ((c = getchar()) != EOF) {
if ( c != ' ' && c != '\n' && c != '\t' && start == false) {
++count;
start = true;
} else {
if (c ==' ' || c == '\n' || c == '\t') {
arr[i] = count;
break;
} else {
++count;
}
}
}
}
for (int i = 0; i < MAXWORDCOUNT; ++i) {
printf("%d line: ", i);
for (int j = 0; j < arr[i]; ++j) {
printf("*");
}
printf("\n");
}
}
helicopter
watermallon
apple
close
book
egg
eat
water
flower
computer
0 line: **********
1 line: ***********
2 line: *****
3 line: *****
4 line: ****
5 line: ***
6 line: ***
7 line: *****
8 line: ******
9 line: ********
垂直方向
#include <stdio.h>
#include <stdbool.h>
#define MAXWORDLEN 10
#define MAXWORDCOUNT 10
char record[MAXWORDLEN][MAXWORDCOUNT];
int wordLenArr[MAXWORDCOUNT];
int main(int argc, char **argv) {
for(int i = 0; i < MAXWORDCOUNT; ++i) {
char c;
while ((c = getchar()) != ' ' && c != '\t' && c != '\n' && c != EOF) {
++wordLenArr[i];
}
}
for (int i = 0; i < MAXWORDLEN; ++i) {
for (int j = 0; j < MAXWORDCOUNT; ++j) {
if (wordLenArr[i] >= 0) {
record[MAXWORDLEN - j][i] = '*';
--wordLenArr[i];
} else {
record[MAXWORDLEN - j][i] = ' ';
}
}
}
for (int i = 0; i < MAXWORDLEN; ++i) {
for (int j = 0; j < MAXWORDCOUNT; ++j) {
printf("%c\t", record[i][j]);
}
printf("\n");
}
for (int i = 0; i <= MAXWORDCOUNT; ++i) {
printf("line%d ", i);
}
printf("\n");
return 0;
}
apple
book
first
second
third
room
water
floor
pen
mirror
* *
* * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
line0 line1 line2 line3 line4 line5 line6 line7 line8 line9 line10