Sorry for my English( I am learning. 1.I have make this code, but when I have wrote "isduplicate" function it makes uncatchable bug. Code work as my expectetion only in debugger. So where is it? Function "isduplicate" return 0 in debugger and probably 1 in console.
2.Is it correct way to find double value? '''
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
string code(string text, string key);
int isduplicate(string c);
int main(int argc, string argv[])
{
if ( argc != 2 )
{
printf("Usage: ./substitution key\n");
return 1;
}
int l = strlen(argv[1]);
for (int i = 0; i < l; i++)
{
if ( argv[1][i] < 'A' || (argv[1][i] > 'Z' && argv[1][i] < 'a') || argv[1][i] > 'z' )
{
printf("Usage: ./substitution key\n");
return 1;
}
}
if ( l < 26 )
{
printf("Key must contain 26 characters.\n");
return 1;
}
if (isduplicate(argv[1]))
{
printf("Key must contain each letter exactly once.\n");
return 1;
}
string plaintext = get_string("plaintext: ");
printf("ciphertext: %s\n", code(plaintext, argv[1]));
}
string code(string text, string key)
{
for (int i = 0; i < strlen(text); i++)
{
if (text[i] >= 'A' && text[i] <= 'Z')
{
int u = text[i] - 65;
text[i] = toupper(key[u]);
}
else if (text[i] >= 'a' && text[i] <= 'z')
{
int l = text[i] - 97;
text[i] = tolower(key[l]);
}
}
return text;
}
int isduplicate(string c)
{
int V[26];
for (int i = 0; i < 26; i++ )
{
V[i] = 0;
}
int Number[26];
{
for (int i = 0; i < 26; i++)
{
Number[i] = toupper(c[i]) - 64;
}
}
for (int i = 0; i < 26; i++ )
{
if (V[Number[i]] == 0)
{
V[Number[i]] = 1;
}
else
{
return 1;
}
}
return 0;
}