c uva

[UVA490]Rotating Sentences

Posted by John on 2015-12-13
Words 330 and Reading Time 1 Minutes
Viewed Times

In ``Rotating Sentences,’’ you are asked to rotate a series of input sentences 90 degrees clockwise. So instead of displaying the input sentences from left to right and top to bottom, your program will display them from top to bottom and right to left.

Input and Output

As input to your program, you will be given a maximum of 100 sentences, each not exceeding 100 characters long. Legal characters include: newline, space, any punctuation characters, digits, and lower case or upper case English letters. (NOTE: Tabs are not legal characters.) The output of the program should have the last sentence printed out vertically in the leftmost column; the first sentence of the input would subsequently end up at the rightmost column.

Sample Input

Rene Decartes once said,
"I think, therefore I am."

Sample Output

"R
Ie
n
te
h
iD
ne
kc
,a
r
tt
he
es
r
eo
fn
oc
re
e
s
Ia
i
ad
m,
.
"

想法:

蠻簡單的題目,改變一下輸出順序即可。要注意的是如果前一行的長度比後面的句子短,在倒轉後會造成缺少空白的情形,所以要隨時記錄最大長度的句子,並把所有比他小的句子都補齊空白。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char s[101][101];
int i = 0;
int len = 0;
memset(s,0,sizeof(s));
while (gets(s[i])) {
len = len < strlen(s[i]) ? strlen(s[i]) : len;
i++;
}
for(int j = 0 ; j < i ; ++j) {
for(int k = strlen(s[j]) ; k < len ; ++k) {
s[j][k] = ' ';
}
}
for(int k = 0 ; k < len ; ++k) {
for(int j = i-1 ; j >= 0 ; --j) {
printf("%c",s[j][k]);
}
printf("\n");
}
return 0;
}

>