You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

密码学C++程序开头输出空行问题排查与修复请求

密码学C++程序开头空行问题排查与修复

问题描述

该C++程序用于实现密码替换加密,功能基本正常,但运行后会在输出开头多出一行空行。

输入示例

1:   cbjalmvopqruytiezksnxdfghw
    2:   Once upon a time in China, some believe, around the year one double-ought three,
    3:   head priest of the White Lotus Clan, Pai Mei was walking down the road,
    4:   contemplating whatever it is that a man of Pai Mei's infinite power contemplates
    5:   - which is another way of saying "who knows" - when a Shaolin monk appeared,
    6:   traveling in the opposite direction.  As the monk and the priest crossed paths,
    7:   Pai Mei, in a practically unfathomable display of generosity, gave the monk the
    8:   slightest of nods.  The nod was not returned.

预期输出

1: Itjl xeit c npyl pt Joptc, siyl blupldl, ckixta nol hlck itl aixbul-ixvon nokll,
    2:olca␣ekplsn␣im␣nol␣Fopnl␣Uinxs␣Juct,␣Ecp␣Ylp␣fcs␣fcurptv␣aift␣nol␣kica,↲
    3:jitnlyeucnptv␣focnldlk␣pn␣ps␣nocn␣c␣yct␣im␣Ecp␣Ylp's␣ptmptpnl␣eiflk␣jitnlyeucnls↲
    4:-␣fopjo␣ps␣ctinolk␣fch␣im␣schptv␣"foi␣rtifs"␣-␣folt␣c␣Sociupt␣yitr␣ceelckla,↲
    5:nkcdluptv␣pt␣nol␣ieeispnl␣apkljnpit.␣␣Cs␣nol␣yitr␣cta␣nol␣ekplsn␣jkissla␣ecnos,↲
    6:Ecp␣Ylp,␣pt␣c␣ekcjnpjcuuh␣xtmcnoiycbul␣apseuch␣im␣vltlkispnh,␣vcdl␣nol␣yitr␣nol↲
    7:supvonlsn␣im␣tias.␣␣Nol␣tia␣fcs␣tin␣klnxktla.↲

实际输出

1: 
2: Itjl xeit c npyl pt Joptc, siyl blupldl, ckixta nol hlck itl aixbul-ixvon nokll,
    3:olca␣ekplsn␣im␣nol␣Fopnl␣Uinxs␣Juct,␣Ecp␣Ylp␣fcs␣fcurptv␣aift␣nol␣kica,↲
    4:jitnlyeucnptv␣focnldlk␣pn␣ps␣nocn␣c␣yct␣im␣Ecp␣Ylp's␣ptmptpnl␣eiflk␣jitnlyeucnls↲
    5:-␣fopjo␣ps␣ctinolk␣fch␣im␣schptv␣"foi␣rtifs"␣-␣folt␣c␣Sociupt␣yitr␣ceelckla,↲
    6:nkcdluptv␣pt␣nol␣ieeispnl␣apkljnpit.␣␣Cs␣nol␣yitr␣cta␣nol␣ekplsn␣jkissla␣ecnos,↲
    7:Ecp␣Ylp,␣pt␣c␣ekcjnpjcuuh␣xtmcnoiycbul␣apseuch␣im␣vltlkispnh,␣vcdl␣nol␣yitr␣nol↲
    8:supvonlsn␣im␣tias.␣␣Nol␣tia␣fcs␣tin␣klnxktla.↲

问题原因

程序读取26个密钥字符后,没有处理密钥行末尾的换行符。当用户输入完26个字母并按下回车时,这个换行符会被后续的getchar()读取,成为text数组的第一个元素,加密输出时直接打印该换行符,导致开头出现空行。

修复方案

在读取完26个密钥字符后,添加逻辑跳过所有空白字符(包括换行、空格、制表符等),直到遇到非空白的文本内容。该方案能兼容更多输入场景,避免遗漏其他可能的空白干扰。

修改后的代码

#include <iostream>
#include <cctype>  // 新增:用于isspace函数

using namespace std;

bool isletter(char c)
{
    return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
}
bool capital(char c)
{
    return (c >= 'A' && c <= 'Z');
}

bool small(char c)
{
    return (c >= 'a' && c <= 'z');
}

int main()
{
    char a[26];
    for (int i = 0; i < 26; i++) {
        a[i] = getchar();
        char c = a[i];

        if (isletter(c) == false) {
            cout << "error" << endl;
            return (0);
        }
    }

    // 新增:跳过密钥后的所有空白字符(包括换行、空格等)
    char skip_char;
    do {
        skip_char = getchar();
    } while (isspace(skip_char));

    //----------------reading of text------------------
    char text[10000], character;
    int i = 0;
    // 将第一个非空白字符存入text数组
    text[i] = skip_char;
    i++;
    do {
        character = getchar();
        text[i] = character;
        i++;
    } while (character != EOF);
    //-----------------crypt----------------------
    for (int j = 0; j < i - 1; j++) {
        if (small(text[j]))
            text[j] = a[text[j] - 'a'];
        else if (capital(text[j]))
            text[j] = a[text[j] - 'A'] - 32;
        cout << text[j];
    }
}

说明

新增的代码段会读取并跳过密钥行之后的所有空白字符,直到找到第一个有效文本字符,再将其存入text数组开始后续读取,彻底避免了换行符被输出,解决开头空行问题。

内容的提问来源于stack exchange,提问作者user23076731

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 05:35:11