Tamam. Yani, bir Vigenere cypher yapmalıyım. Metin ve anahtar her iki küçük harfin de büyük harf olduğu zaman, kod iyi derlenir. Ancak metin ve anahtar durumdan farklı olduğunda, kod çalışmıyor. O zaman bir şey yazmaz. Örneğin anahtar olduğunda: aaAA. Ve metin aBcD'dir. Sonuç: aD. Birisi bana bir ipucu verebilir mi lütfen? :)CS50 Pset2. Vigenere. Alt tuşa üst metin ve tersi problemler
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int main (int argc, string argv[])
{
string key = argv [1]; //argv [1] is the key. 0 is compile program
{
if (argc != 2)
{
printf ("Please give one key: "); //if there are more or less then 2 argc, then have to try again
}
for (int j = 0, n = strlen (key); j < n; j++)
if (!isalpha (key [j]))
{
printf ("Please give a key in alphabetic characters: ");
//key must be alphabetic. For loop to check every character of the key.
return 1;
}
}
string text = GetString(); //Get secret message from user
int j = 0;
for (int i = 0, n = strlen (text); i < n; i++)
{
if (isupper (text [i]))
{
if (isupper (key [j]))
{
/*Minus 65 to make count till 26 from text and key. Use modulo to wrap around key. And modulo to wrap around alphabet.
Plus 65 to go to correct ASCII character. */
int u = ((((text [i] - 65) + (key [j % strlen (key)] - 65)) % 26) + 65);
printf ("%c", u);
}
}
else if (islower (text [i]))
{
if (islower (key[j]))
{
int l = ((((text [i] - 97) + (key [j % strlen (key)] - 97)) % 26) + 97);
printf ("%c", l);
}
}
else if (islower (text [i]))
{
if (isupper (key[j]))
{
int lu = ((((text [i] - 97) + (key [j % strlen (key)] - 65)) % 26) + 97);
printf ("%c", lu);
}
}
else if (isupper (text [i]))
{
if (islower (key[j]))
{
int ul = ((((text [i] - 65 + (key [j % strlen (key)] - 97)) % 26) + 65);
printf ("%c", ul);
}
}
else
{
// When character is non alphabetic print it in its original form.
printf ("%c", text [i]);
}
j++;
}
{
printf ("\n");
return 0;
}
}
Tamam değilse çok sayesinde
j
artan edilmemelidir düşünüyorum. Şimdi üst-alt anahtar doğru çalışıyor. Ama anahtarın etrafına sarılmıyor. Kod ilk kez iyi çalışıyor. Ancak metin daha uzun olduğunda, düzgün bir şekilde sarılmıyor. işe yaramaz gibi görünmüyor. Baska öneri? – Kim@Kim Üzgünüm, sorun anahtarın 'if' ifadelerinde. 'Key [j% strlen (key)]' yi kullanmalısın yoksa sınırların dışına çıkarsın. Ben –
Ahaa cevabını güncelleyeceğim, anlıyorum. Yardımın için çok teşekkürler. Sonunda bitirdi :) – Kim