[英]Issue with comparing character values inside of a string in C for CS50 subsitution problem
程序規格是
設計和實現一個程序,替代,使用替代密碼加密消息。
在名為substitution 的目錄中的一個名為substitution.c 的文件中實現您的程序。
您的程序必須接受一個命令行參數,即用於替換的鍵。 鍵本身應該不區分大小寫,因此鍵中的任何字符是大寫還是小寫都不應影響程序的行為。
如果您的程序在沒有任何命令行 arguments 或多個命令行參數的情況下執行,您的程序應該打印您選擇的錯誤消息(使用 printf)並從 main 返回值 1(這往往表示錯誤) 立即地。
如果密鑰無效(如不包含 26 個字符、包含任何非字母字符或每個字母不只包含一次),您的程序應該打印您選擇的錯誤消息(使用 printf)並從 main 返回立即值為 1。
您的程序必須 output 明文:(不帶換行符)然后提示用戶輸入明文字符串(使用 get_string)。
你的程序必須 output 密文:(不帶換行符)后跟明文對應的密文,用明文中的每個字母字符替換密文中的對應字符; 非字母字符應原樣輸出。
您的程序必須保留大小寫:大寫字母必須保持大寫字母; 小寫字母必須保持小寫字母。
輸出密文后,應打印換行符。 然后你的程序應該通過從 main 返回 0 來退出。
除了重復輸入之外,我能夠完成所有這些
我想這部分代碼有問題,但我不明白為什么。
當我輸入終端時,我不斷得到一個重復的值。= ./substitution YTNSHKVEFXRBAUQZCLWDMIPGJO
我不明白為什么,因為代碼本身似乎沒問題。 它檢查數組內部的值是否重復,然后增加值計數器,但它不起作用。
當我在 vscode 調試中進行測試運行時,我得到了重復的最終值等於 25,這超出了我的理解。 在此處輸入圖像描述
我下面的代碼是
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
//Check is to keep track if it fulfills all requirements
int check = 0;
// Check if the agrc values are equal to 2 or not, if not have to ask for it again
if (argc != 2)
{
printf("./substitution key\n");
return 1;
}
else
{
check++;
}
// Get the length of the string
int n = strlen(argv[1]);
// Check if the length of inputted characters is 26 or not
if (n != 26)
{
printf("Key must contain 26 characters\n");
return 1;
}
else
{
check++;
}
// Check if there are 26 letters
int letterCheck = 0;
// To get number of digits
// Loop through out the second input through terminal to see if the values are correct
for (int i = 0; i < n; i++)
{
if (isalpha(argv[1][i]))
{
letterCheck++;
}
}
if (letterCheck != 26)
{
printf("Key must contain 26 letters\n");
return 1;
}
else
{
check++;
}
// Check for duplicates
int duplicate = 0;
for (int a = 0; a < n; a++)
{
for (int b = 1; b < n; b++)
{
if (toupper(argv[1][a]) == toupper(argv[1][b]))
{
duplicate++;
}
}
}
if (duplicate != 0)
{
printf("Key must not contain repeated characters\n");
return 1;
}
else
{
check++;
}
if (check == 4)
{
string plaintext = get_string("plaintext: ");
// Get the length of the string
int length = strlen(plaintext);
for (int i = 0; i < length; i++)
{
if (isupper(plaintext[i]))
{
plaintext[i] = plaintext[i] - 65;
plaintext[i] = argv[1][(int)plaintext[i]];
plaintext[i] = toupper(plaintext[i]);
}
else
{
plaintext[i] = plaintext[i] - 97;
plaintext[i] = argv[1][(int)plaintext[i]];
plaintext[i] = tolower(plaintext[i]);
}
}
printf("ciphdertext: %s\n", plaintext);
return 0;
}
}```
'''
int duplicate = 0;
for (int a = 0; a < n; a++)
{
for (int b = 1; b < n; b++)
{
if (toupper(argv[1][a]) == toupper(argv[1][b]))
{
duplicate++;
}
}
}
if (duplicate != 0)
{
printf("Key must not contain repeated characters\n");
return 1;
}
else
{
check++;
}
'''
您已將“b”值設置為“1”,因此每次循環時都從 1 開始。 因此,當您的外部循環為 1 時,您的內部循環也將從 1 開始,並且您的重復標志將增加。 您的循環應該類似於下面的循環
for (int a = 0; a < n; a++)
{
for (int b = a + 1; b < n; b++)
{
if (toupper(argv[1][a]) == toupper(argv[1][b]))
{
duplicate++;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.