2016-08-02 16 views
-3

Ich stehe vor dem Problem, während ich den unten stehenden Code für vigenere cipher laufen lasse. Nachdem ich gründlich gegangen bin, kann ich das Problem nicht beheben. es zeigt den Fehler an: getötet von server.please help.Vigenere Cipher

/** 
* 
* vigenere.c 
* 
* Abhishek kumar 
* encrypts entered string using vigenere cipher 
* */ 
#include <stdio.h> 
#include <stdlib.h> 
#include <cs50.h> 
#include <ctype.h> 
#include <string.h> 

int main(int argc, string argv[]) 
{ 
    if (argc != 2) 
    { 
     printf("Usage: /home/cs50/pset2/vigenere <keyword>"); 
     return 1; 
    } 

    if (argc == 2) 
    { string key = argv[1]; 
     for(int k = 0,l = strlen(key);k < l; k++) 
     { 
      if(!isalpha(key[k])) 
      { 
       printf("Keyword must only contain letters A-Z and a-z"); 
       exit(1); 
      } 

     } 


     string txt = GetString(); 
     int i = 0,j = 0,c = 0; 
     int n = strlen(txt); 
     int m = strlen(key); 
     while(i < n) 
     { 
      if (isupper(txt[i])) 
      { 
       if(isupper(key[j])) 
       { 
        c = ((((int) txt[i] - 65 + (int) key[j] -65)%26) + 65); 
        printf("%c", (char) c); 
        i++; 
        j++; 
       } 
       if(islower(key[j])) 
       { 
        c = ((((int) txt[i] - 65 + (int) key[j] -97)%26) + 65); 
        printf("%c", (char) c); 
        i++; 
        j++; 
       } 

      } 
      else if (islower(txt[i])) 
      { 
       if(isupper(key[j])) 
       { 
        c = ((((int) txt[i] - 97 + (int) key[j] -65)%26) + 97); 
        printf("%c", (char) c); 
        i++; 
       } 
       if(islower(key[j])) 
       { 
        c = ((((int) txt[i] - 97 + (int) key[j] -97)%26) + 97); 
        printf("%c", (char) c); 
        j++; 
       } 


      } 
      else 
      { 
       printf("%c",txt[i]); 
       i++; 

      } 
      if (j == m-1) 
      { 
       j = 0; 
      } 
     } 




    } 
} 

unten sind einige der Testfälle, in denen es versagt.

:) vigenere.c exists 
:) vigenere.c compiles 
:(encrypts "a" as "a" using "a" as keyword 
    \ killed by server 
:(encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword 
    \ killed by server 
:(encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword 
    \ expected output, but not "CGSFpp" 
:(encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword 
    \ expected output, but not "CASFPO" 
:) handles lack of argv[1] 
:) handles argc > 2 
:) rejects "Hax0r2" as keyword 
+0

Bitte fügen Sie einige Beispieleingabe und gewünschte Ausgabe hinzu. – merlin2011

+2

bitte eine Frage stellen – dustinroepsch

+0

bearbeitet, Kasse jetzt .. –

Antwort

0

Im islower(txt[i]) Abschnitt, scheitern Sie i und j in allen Fällen zu erhöhen. An der Stelle, wo Sie nicht i inkrementieren, d. H. Wo das erste Zeichen des Schlüssels und der Text beide Kleinbuchstaben sind, enden Sie mit einer Endlosschleife.

Im isupper(txt[i]) Abschnitt, erhöhen Sie i und j im isupper(key[j]) Teil, dann Sie den islower(key[j]) Teil eingeben, da Sie if statt else if verwenden.

Bei der obigen beiden if(islower(key[j])) zu else if(islower(key[j])), ändern und j++ und die printf nach jedem inneren if Block bewegen. Ändern Sie für iwhile zu einem for und erhöhen Sie i als Teil davon.

Wenn Sie überprüfen, ob Sie j zurücksetzen sollten, sind Sie um 1. m-1 ist ein gültiger Index für key, so dass Sie noch nicht zurücksetzen möchten. Tun Sie es, wenn j == m.

Ersetzen Sie auch die ASCII-Codes durch die tatsächlichen Zeichen, die sie darstellen, so dass es klarer ist, was Sie tun. Die Abgüsse werden auch nicht benötigt.

for (i=0; i < n; i++) 
    { 
     if (isupper(txt[i])) 
     { 
      if(isupper(key[j])) 
      { 
       c = (((txt[i] - 'A' + key[j] -'A')%26) + 'A'); 
      } 
      else if(islower(key[j])) 
      { 
       c = (((txt[i] - 'A' + key[j] -'a')%26) + 'A'); 
      } 
      printf("%c", c); 
      j++; 
     } 
     else if (islower(txt[i])) 
     { 
      if(isupper(key[j])) 
      { 
       c = (((txt[i] - 'a' + key[j] -'A')%26) + 'a'); 
      } 
      else if(islower(key[j])) 
      { 
       c = (((txt[i] - 'a' + key[j] -'a')%26) + 'a'); 
      } 
      printf("%c", c); 
      j++; 
     } 
     else 
     { 
      printf("%c",txt[i]); 
     } 
     if (j == m) 
     { 
      j = 0; 
     } 
    } 
+0

Danke ein Ton Mann.! –