CS50替换,检查重复项和检查所有字符是否都是字母的问题

问题描述 投票:0回答:1

此代码尚未完成,但是我遇到两个问题。首先,当键中包含非字母字符时,错误消息“键必须仅包含字母”的打印次数与非字母字符ex的打印次数相同:如果键中有四个数字,则会显示错误消息四次。其次,当密钥包含重复的字母时,不会打印错误消息,而是程序会像密钥一样起作用。

谢谢您的任何建议。

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

int main(int argc, string argv[])

{
if (argc != 2)
{
    printf("Usage: ./caesarsub key\n");
    return 1;
}
// Check key contains only letters
for (int i = 0, n = strlen(argv[1]); i < n; i++)
    {
        if (!isalpha(argv[1] [i]))
        {
            printf("Key must only contain letters\n");
        }
        // Check letters are not repeated
        else
        {
            int count = 0;
            for (int m = i + 1; n < strlen(argv[1]); m++)
            {
                if (argv[1][i] == argv[1][m])
                {
                    count++;
                }
            }
            if (count > 0)
            printf("Key must not contain repeated characters\n");
            }
        }
// Check key contains 26 letters
if (strlen(argv[1]) != 26)
    {
        printf("Key must be 26 letters\n");
    }
}
duplicates key substitution cs50 isalpha
1个回答
0
投票
另一个问题在这里看起来像错字for (int m = i + 1; n < strlen(argv[1]); m++)。因为n < strlen...永远都不为真,所以for循环不会执行。
© www.soinside.com 2019 - 2024. All rights reserved.