如何跳过字符数组的空格

How to skip whitespaces for a character array?

本文关键字:空格 数组 字符 何跳过      更新时间:2023-10-16
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
char input[100],crypt[100],decrypt[100];
int key,key1[100],j=0,i;
cout << "Enter plaintxt:";
gets_s(input);
cout << "Enter key:";
cin >> key;
key1[0] = key;
for (int i = 1; i < strlen(input); i++){
    if (input[i - 1] >= 97 && input[i - 1] <= 122)
    key1[i] = ((int)input[i-1])-97;
}
for (int j = 0; j < strlen(input); j++){
    cout << key1[j]<<" ";
}
for ( i = 0; i < strlen(input); i++){
    crypt[i] = (char)((key1[i] + (int)(input[i]-97)) % 26+97);
}
crypt[i] = '';
cout << crypt;
//decrypt[0] = ((int)crypt[0] - 97)-key;
//cout << decrypt[0];
_getch();
}

我的输入是:攻击很好,我的密钥是 12,我的输出应该是:mtmtcm sa ljrdy.....注意:这是一种单字母密码

由于问题仅与空格字符有关,因此在接受用户输入后调用此函数将解决问题,因为它将删除空格:

void RemoveSpaces(char* IN_cpSource)
{
if  (IN_cpSource == NULL)
    {
    exit(1);
    }
char* cpResult = IN_cpSource;
do
{
*cpResult = *IN_cpSource;
if  (*cpResult != ' ')
    {
    cpResult++;
    };
}while (*IN_cpSource++ != 0);
}