7-11 藏尾诗(T2)

本题要求编写一个解密藏尾诗的程序。

注:在 2023 年 1 月 17 日 15 点 14 分以后,该题数据修改为 UTF-8 编码。

输入格式:

输入为一首中文藏尾诗,一共四句。每句一行,但句子不一定是等长的,最短一个汉字,最长九个汉字。注意:一个汉字占三个字节。

输出格式:

取出每句的最后一个汉字并连接在一起形成一个字符串并输出。同时在末尾输入一个换行符。

输入样例:

悠悠田园风
然而心难平
兰花轻涌浪
兰香愈幽静

输出样例:

风平浪静

 code

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

int main() {
    char poem[4][30];
    char result[13];
    int i, j;

    // Input strings using fgets instead of gets
    for (i = 0; i < 4; i++) {
        fgets(poem[i], sizeof(poem[i]), stdin);
        // Remove the newline character from fgets if present
        poem[i][strcspn(poem[i], "\n")] = '\0';
    }

    j = 0;
    for (i = 0; i < 4; i++) {
        int len = strlen(poem[i]);
        if (len >= 3) {
            result[j] = poem[i][len - 3];
            result[j + 1] = poem[i][len - 2];
            result[j + 2] = poem[i][len - 1];
            j += 3;
        } else {
            // Handle case where a string has fewer than 3 characters
            // For example, you could fill in with a placeholder like 'X'
            result[j] = result[j + 1] = result[j + 2] = 'X';
            j += 3;
        }
    }

    result[12] = '\0'; // Null-terminate the result string
    printf("%s\n", result);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值