【问题标题】:CS50/Substitution – Why does the tolower() function not work in my code?CS50/Substitution – 为什么 tolower() 函数在我的代码中不起作用?
【发布时间】:2021-10-25 15:57:36
【问题描述】:

如果我切换,谁能解释一下

// 3.3 Must not contained repeated letters |
if (toupper(argv[1][j] == toupper(argv[1][k])))

if (tolower(argv[1][j] == tolower(argv[1][k])))

为什么它不再起作用了,即使字母可能是相同的计算机无法识别它?

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

int main(int argc,string argv[])  
{
    int count_key_char = 0;
    int count_repeated_char = 0;

    //Exactly 1 command line argument
    if (argc == 2)
    {
        //Must all be letters
        for (int i = 0; i < strlen(argv[1]); i++)
        {
            if (isalpha(argv[1][i]))
            {
                count_key_char++;
            }
        }
        // 3.3 Must not contained repeated character/s
        for (int j = 0, n = strlen(argv[1]); j < n; j++)
        {
            for (int k = j + 1; k < n; k++)
            {
                if (toupper(argv[1][j] == toupper(argv[1][k])))
                {
                    count_repeated_char++;
                }
            }
        }
        // Must be 26 characters (Fail criteria)
        if (strlen(argv[1]) != 26)
        {
            printf("Key must contain 26 characters.\n");
            return 1;
        }
        // Must be all letters  (Fail criteria)
        else if (count_key_char != strlen(argv[1]))
        {
            printf("Key must only contain alphabetic characters.\n");
            return 1;
        }
        // Must not contained repeated character/s (Fail criteria)
        else if (count_repeated_char != 0)
        {
            printf("Key must not contain repeated characters.\n");
            return 1;
        }
        // 4. Get plaintext; Prompt user for plaintext
        else
        {
            string plaintext = get_string("Plaintext: ");
            printf("ciphertext: ");

            for (int w = 0; w < strlen(plaintext); w++)
            {
                // If character are letter & uppercase
                if (isalpha(plaintext[w]) && isupper(plaintext[w]))
                {
                    int upper = (plaintext[w] -65);
                    printf("%c", toupper(argv[1][upper]));
                }
                // If character are lower & lowercase
                else if (isalpha(plaintext[w]) && islower(plaintext[w]))
                {
                    int lower = (plaintext[w] - 97);
                    printf("%c", tolower(argv[1][lower]));
                }
                // If character are not letter, print as it is
                else
                {
                    printf("%c", plaintext[w]);
                }
            }
            printf("\n");
            return 0;
        }
    }
    else
    {
        printf("Usage: ./substitution key\n");
        return 1;
    }
}

【问题讨论】:

  • 你为什么要调用toupper inside 的参数来另一个调用toupper
  • 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。

标签: c cs50


【解决方案1】:

此行中有一个错误的右括号 )

if (toupper(argv[1][j] == toupper(argv[1][k])))

如果字母为小写,toupper 将失败;如果字母为大写,tolower 将失败。 它正在评估这个

argv[1][j] == toupper(argv[1][k])

如果两个字符都是相同的大写字母,则返回 1 (true)。如果 key 以小写形式输入,则返回 0(false),程序不会认为它是重复字符。

toupper参数正确括在括号中,问题就解决了。

【讨论】:

  • 是的,你是对的,现在一切都说得通了。谢谢老哥
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-16
  • 1970-01-01
  • 2014-01-23
  • 2013-08-06
相关资源
最近更新 更多