gpt4 book ai didi

c - Vigenere Cipher - 如何忽略纯文本中的空格(在 C 中)?

转载 作者:太空宇宙 更新时间:2023-11-04 03:12:49 25 4
gpt4 key购买 nike

粗体是我试图让程序在输出纯文本时忽略空格的地方。我对如何执行此操作感到困惑。当我运行程序时,它不会忽略空格。相反,它就像粗体 else if 语句不存在一样运行。我很困惑这是为什么。如果我的代码有点困惑,我很抱歉。我刚开始编程。

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

int shift(char c);

int main(int argc, string argv[])
{
// Check to see if two arguments are enterted at launch
int cipher = 0;
if (argc != 2)
{
// If not return error & return 0
printf("Usage: ./vigenere keyword \n");
return 1;
}
else
{
int strlength = strlen(argv[1]);
// Iterates through characters in second argument (key), checking to see
// if they are digits
for (int k = 0; k < strlength; k++)
{
if (!isalpha(argv[1][k]))
{
// If not return error & return 1
printf("Usage: ./vigenere keyword\n");
return 2;
}
}
//char *c =argv[1];
string plaintext = get_string("Plaintext: ");
int len = (int)strlen(plaintext);
//int b = atoi(c);
char code[len];
strcpy(code, plaintext);
for (int j = 0; j < len; j++)
{
int key = shift(argv[1][j]);

if (isupper(argv[1][0]))
{
cipher = ((((code[j] - 'A') + key) % 26) + 'A');
//printf("%c", (((plaintext[j] - 'A') + key) % 26) + 'A');
//printf("%c",cipher);
}
else if (islower(argv[1][0]))
{
cipher = ((((code[j] - 'a') + key) % 26) + 'a');
//printf("%c", (((plaintext[j] - 'a') + key) % 26) + 'a');
printf("%c",cipher);
}
else if (!isalpha(code[j]))
{
code[j] = 0;
}
/* else
{
printf("%c", code[j] + (cipher));
}
*/
}
printf("\n");
}
}

int shift(char c)
{
int i = c;
if (i <= 'Z' && i >= 'A')
{
return ((i - 'A') % 26);
}
else
{
return ((i - 'a') % 26);
}
}

最佳答案

虽然您从未指出注释是否足以解决您的问题,但如果您仍在纠结如何使用 CS50 get_string() 函数获取输入,同时确保它不包含 空格,您可以简单地为 get_string() 函数编写一个简短的包装器。

在包装函数中,您可以简单地将任何提示直接传递给 get_string() 保存返回。然后分配第二个字符串来保存内容并遍历 get_string() 返回的字符串,仅将非空白字符复制到新的内存块,完成后以 nul 终止新字符串并返回新字符串。

它可以像这样简单:

#include <cs50.h>
...
/* function using CS50 get_string() to fill input w/o spaces */
string get_str_nospc (string p)
{
size_t ndx = 0; /* index */
string s = get_string (p), /* string returned by get_string() */
sp = s, /* pointer to s (must preserve s address) */
s_nospc = NULL; /* pointer to copy with no whitespace */

if (!s) /* validate get_string() return */
return NULL;

/* allocate/validate storage for string without whitespace */
if (!(s_nospc = malloc (strlen (s) + 1))) {
perror ("malloc-s_nospc");
return NULL;
}

do /* copy s to s_nospc omitting whitespace */
if (!isspace (*sp)) /* if it's not a space - copy */
s_nospc[ndx++] = *sp;
while (*sp++); /* post-increment ensures nul-character copied */

return s_nospc; /* return string with no whitespace */
}

(注意:无论 get_string() 返回的字符串是否包含空格,您都需要分配和复制作为 get_string()< 返回的字符串 在退出时被 CS50 库析构函数释放。你的函数不应该返回一个可能/可能不需要根据函数内的条件释放的字符串——你永远不会知道你是否负责调用 免费或不免费。)

一个简短的例子可以是:

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

#include <cs50.h>

/* function using CS50 get_string() to fill input w/o spaces */
string get_str_nospc (string p)
{
size_t ndx = 0; /* index */
string s = get_string (p), /* string returned by get_string() */
sp = s, /* pointer to s (must preserve s address) */
s_nospc = NULL; /* pointer to copy with no whitespace */

if (!s) /* validate get_string() return */
return NULL;

/* allocate/validate storage for srting without whitespace */
if (!(s_nospc = malloc (strlen (s) + 1))) {
perror ("malloc-s_nospc");
return NULL;
}

do /* copy s to s_nospc omitting whitespace */
if (!isspace (*sp)) /* if it's not a space - copy */
s_nospc[ndx++] = *sp;
while (*sp++); /* post-increment ensures nul-character copied */

return s_nospc; /* return string with no whitespace */
}

int main (void) {

string nospc = get_str_nospc ("input : ");

if (nospc) {
printf ("nospc : %s\n", nospc);
free (nospc);
}
}

示例使用/输出

$ ./bin/cs50_getstrnospc
input : my dog has fleas
nospc : mydoghasfleas

仔细检查一下,让我知道您上一条评论是否正是您的意图。如果没有,我很乐意进一步提供帮助。

关于c - Vigenere Cipher - 如何忽略纯文本中的空格(在 C 中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54727622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com