Bolded是我试图让程序在输出时忽略纯文本空格的地方。我对如何做到这一点很困惑。当我运行程序时,它不会忽略空格。相反,它运行就好像没有加粗的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()
返回的字符串,只将非空白字符复制到新的内存块,在完成后终止新字符串并返回新字符串。
它可能很简单:
#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库析构函数释放,您将需要分配和复制。您的函数不应返回可能/可能不需要的字符串根据函数内的条件释放 - 你永远不知道你是否应该负责调用free
。)
一个简短的例子可能是:
#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
仔细看看,如果这是你上次评论的意图,请告诉我。如果没有,我很乐意进一步提供帮助。