490 - Rotating Sentences

这道题目啊!!!!!!!!
花了一天……

首先是输入问题:
1. scanf肯定是不好用的,因为有空格输入;
2. getchar()不好用,因为不好终止循环;
3. 用fgets()吧!

这里特别要注意的一点是,fgets()是以’\n’为最后有效字符的,再往后就是一个‘\0’,注意这里最后是有‘\0’的!!!

另外,fgets()不会覆盖字符串长度不够的领域,也就是说把数组初始化为全空格之后,如果输入的字符串,没有数组长,那么剩下的地方依旧是空格,不会变成其他的东西。

还有fgets()遇到[读入]End of File(EOF)会返回NULL,注意NULL和’\0’的ascii码都是0.
另外补充一下:EOF在C语言中是-1。

另外一个易错点:
一定要注意输入完成后的每一行字符穿都是以‘\n’和’\0’结尾的,但是这两个最后不可以打印出来,所以在数组中一个一个循环取值的时候,要注意处理这两个字符,如果遇到这两个字符,就输出空格,否则输出原字母。

写了程序验证一下:

int N, maxLen = 0;
    memset(passage, ' ', sizeof(passage));

    for (N = 0; fgets(passage[N], 110, stdin); N++) {
        if (strlen(passage[N]) > maxLen)
            maxLen = strlen(passage[N]);
    }

    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < maxLen; ++j) {
            if (passage[i][j] == '\n')
                printf("*");
            else if (passage[i][j] == '\0')
                printf("#");
            else
                printf("%c",passage[i][j]);
        }
        printf("END\n");
    }

如果我输入:
I am a girl.
I am from China.

将输出:
I am a girl.*# END
I am from China.*END

第二行没有输出#是因为,j < maxLen,而maxLen = ‘\n’ + 字符个数,没有包括’\0’,而第一行因为比maxLen短好多,所以会把’\0’输出来。

另外还有一事要注意—-最后输出的格式:最长的那一行竖着输出时最后是不会输出空格的,这就是为什么代码中最后是i < maxLen - 1的原因了。看例子中的格式来判断自己怎么输出:
这里写图片描述
第一例最后没有空格。

悲惨的我,这次也算是弄明白了这几种输入法了……
晒一下,悲惨的list:
这里写图片描述


题目:

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 <string.h>

char passage[100 + 10][100 + 10];

int main() {
    int N, maxLen = 0;
    memset(passage, ' ', sizeof(passage));

    for (N = 0; fgets(passage[N], 110, stdin); N++) {
        if (strlen(passage[N]) > maxLen)
            maxLen = strlen(passage[N]);
    }

    for (int i = 0; i < maxLen - 1; ++i) {
        for(int j = N-1; j>=0;--j) {
            if (passage[j][i] == '\n' || !passage[j][i]) {
                printf(" ");
            }
            else {
                printf("%c", passage[j][i]);
            }
        }
        printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值