-3
CS50のマイコードpset2 Vigenere cypherは次のとおりです。バグを見つけるのを手伝ってください。キーに文字 'a'が含まれている場合、このコードは基本的に何も行いません。それは "浮動小数点例外"と言います。私はそれが何を意味するのか分かりません。コードを見て、バグが何であるか教えてください。私のVigenere Cypherで何が問題になったのですか?
#include<stdio.h>
#include<cs50.h>
#include<ctype.h>
#include<string.h>
int main(int argc, string argv[])
{
//check if there are only two command line arguments//
if (argc!=2)
{
printf("Please enter a valid input!\n");
return 1;
};
string key = argv[1];
for(int i=0, n=strlen(key); i < n; i++)
{
//check if there is any number in the key//
if(!isalpha(key[i]))
{
printf("Invalid Key!\n");
return 1;
};
//converting the key into ints.//
if(islower(key[i]))
{
key[i] = key[i] - 'a';
}
else if(isupper(key[i]))
{
key[i] = key[i] - 'A';
};
};
//prompt user for the string//
string s = GetString();
int c;
int k;
int stln = strlen(s);
int kyln = strlen(key);
for(int j = 0, m = strlen(s); j < m; j++)
{
if(islower(s[j]))
{
s[j] = s[j] - 'a';
//for wrapping around the key//
if(stln > kyln)
{
k = j % strlen(key);
c = (s[j] + key[k]) % 26;
s[j] = 'a' + c;
}
else
{
c = (s[j] + key[j]) % 26;
s[j] = 'a' + c;
};
}
else if (isupper(s[j]))
{
s[j] = s[j] - 'A';
//for wrapping around the key//
k = j % strlen(key);
if(stln > kyln)
{
c = (s[j] + key[k]) % 26;
s[j] = 'A' + c;
}
else
{
c = (s[j] + key[j]) % 26;
s[j] = 'A' + c;
};
};
};
printf("%s\n", s);
};
'string'の定義を表示します。 – chux
トピックオフ:中括弧を閉じた後にセミコロンは必要ありません。言い換えれば '};は'} ' – user3386109
chuxでなければなりません。含まれるcs50ライブラリは文字列を定義します。それはちょうどchar *のtypedefです。http://mirror.cs50.net/library50/c/cs50-library-c-3.0/cs50.h – MacedonZero