我正在制作一個基本上“加密”文本的程式,通過用其他字母替換字母。所以你基本上運行程式并輸入一個分布,然后輸入你想要加密的文本,它會給你密碼。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <cs50.h>
#include <ctype.h>
string make_lower(string word);
int main (int argc, string argv[])
{
//we examine the input
if (argc < 2 || argc > 2)
{
printf("Usage: ./substitution key\n");
return (1);
}
if (strlen(argv[1]) != 26)
{
printf("Key must contain 26 characters.\n");
return (1);
}
if (strlen(argv[1]) == 26)
{
for (int i = 0; i < 26; i )
{
if (isdigit(argv[1][i]))
{
printf("Key must onlyontain alphabetic characters.\n");
return (1);
exit(0);
}
}
for (int i = 0; i < 26; i )
{
for (int o = i 1; o < 26; o )
{
if (argv[1][i] == argv[1][o])
{
printf("Key must not contain repeated characters.\n");
return (1);
exit(0);
}
}
}
//we prompt the user for the words to transcribe
const string text = get_string("plaintext: ");
//we set everithing in lower caso to compare it, and set a new variable to store the original text so we can convert the
// upper cases back later
string ntext = text;
printf("%s\n", text);
argv[1] = make_lower(argv[1]);
ntext = make_lower(ntext);
printf("%s\n", text);
string alphabet = "abcdefghijklmnopqrstuvwxyz";
//we substitute the text to the desired outcome
for (int i = 0, n = strlen(ntext); i < n; i )
{
for (int o = 0; o < 26; o )
{
if (ntext[i] == alphabet[o])
{
ntext[i] = argv[1][o];
}
}
}
printf("%s\n", text);
printf("ciphertext: ");
for (int i = 0, n = strlen(ntext); i < n; i )
{
if (isupper(text[i]))
{
printf("%c", toupper(ntext[i]));
}
else
{
printf("%c", ntext[i]);
}
}
printf("%s\n", text);
printf("\n");
return(0);
exit(0);
}
}
string make_lower(string word)
{
for (int i = 0; i < strlen(word); i )
{
word[i] = tolower(word[i]);
}
return(word);
}
所以我的問題是代碼的輸出,因為問題是如果你想要密碼的文本是例如“你好”,它應該像“Ktlly”一樣出現,但我的輸出是“ktlly”,所以結果中不顯示大寫字母。
當您輸入要加密的代碼時,程式會將其存盤在一個常量字串中,然后創建一個新變數并將其設定為等于您輸入的文本,然后程式將該新變數轉換為小寫,以便進行比較,最后,當我們對文本進行加密時,我嘗試通過創建 if 陳述句(在第 82 行)將我想要的那些字符再次大寫,但事實并非如此。我試圖找出原因,這就是為什么我設定在第 56、62、78 和 94 行列印常量,看看為什么它不起作用,結果發現變數正在改變,即使是成為常數。起初在第 56 行它仍然是原始文本;然后在第 62 行,它的文本相同,但小寫;然后在第 78 行和第 94 行,結果證明它自己被修改為文本的加密版本。
所以基本上就是這樣,我不知道為什么會這樣。至少對我來說,代碼似乎是正確的,我的理論是它與函式有關,或者與內置漏洞代碼的大“if”陳述句有關。感謝您通讀所有這些。
uj5u.com熱心網友回復:
以下評論是錯誤的:
//we set everithing in lower caso to compare it, and set a new variable to store the original text so we can convert the
// upper cases back later
string ntext = text;
中string定義的識別符號cs50.h只不過typedef是資料型別的 a char *。這意味著型別變數string實際上并不包含字串,而只是指向一個字串。
在上面參考的行中,您只是復制指標,而不是實際的字串。由于ntext和text現在都指向同一個字串,因此當您修改 指向的字串時ntext,您也在修改 指向的字串text。這似乎不是您想要的。
如果要創建字串的副本,而不是僅復制指標,則必須首先使用 為新字串分配記憶體malloc,然后可以使用 復制字串strcpy:
//allocate memory for copy of the string
string ntext = malloc( strlen(text) 1 ); //add 1 for the terminating null character of the string
//copy the string
strcpy( ntext, text );
大多數編譯器還支持 function strdup,它在單個函式呼叫中處理記憶體分配和字串復制。如果您的編譯器支持該功能,那么您可以改用它:
string ntext = strdup( text );
uj5u.com熱心網友回復:
make_lower覆寫了它的論點。您需要復制string. 考慮使用strdup().
我有點驚訝這并沒有引發編譯器警告。
string make_lower(string word)
{
word = strdup(word);
//...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/378647.html
上一篇:給定本地linux機器上的IP地址,如何檢查它是否被占用?
下一篇:矩陣中不斷增長的子串的長度
