c - Got stuck in the final part of a program for encryption in Vigenere -


i attending online course cs50 on edx , have assignment in have create program user enters keyword ( used encryption ) , string needs encrypted in vigenere cipher.

vigenere works encrypting text following keyword: example if string "hello" , keyword "abc" : a equal 0 in alphabetical characters, b 1, , c 2; letter h in string encrypted without switching characters ( s letter a in keyword = 0), letter e switched 1 position , encrypted f, , on. if keyword has length less of string (like in case) encryption has use again first character of keyword , problem.

in fact, think implemented whole program not sure how take consideration if keyword has less characters string entered. program returning first char of string encrypted , first char not encrypted , stops.
i not ask complete solution, want understand how can solve program problem.

here program:

#include <stdio.h> #include <cs50.h> #include <ctype.h> #include <stdlib.h> #include <string.h>  int vigenere_low( char c ) {     int v = c - 'a';     return v; }  int vigenere_up( char c ) {     int v = c - 'a';     return v; }  int keyword_low( char c ) {     int k = c - 'a';     return k; }  int keyword_up( char c ) {     int k = c - 'a';     return k; }   int main( int argc, string argv[] ) {     string p;     string keyword = argv[1];      if ( argc != 2 )     {         printf("usage: ./vigenere keyword\n");         return 1;     }      ( int = 0, n = strlen(keyword); < n; i++ )     {         if ( !isalpha( keyword[i]) )         {             printf("usage: ./vigenere keyword(alphabetical) \n");             return 1;         }     }      p = getstring();     int j = 0;       ( int = 0, n = strlen( p ); < n; i++ )      {          if ( isalpha( p[i]) )             {                 if ( islower( p[i]) )                {                  if ( islower( keyword[j]) )                   {                     int = (( vigenere_low( p[i]) + keyword_low( keyword[j]) ) % 26 ) + 'a';                     printf("%c", a);                     j++;                   }                   else                   {                     int = (( vigenere_low( p[i]) + keyword_up( keyword[j]) ) % 26 ) + 'a';                     printf("%c", a);;                     j++;                   }                  }                if ( isupper( p[i]) )                 {                  if ( islower( keyword[j]) )                    {                      int = (( vigenere_up( p[i]) + keyword_low( keyword[j]) ) % 26 ) + 'a';                      printf("%c", a);                      j++;                    }                  else                    {                     int = (( vigenere_up( p[i]) + keyword_up( keyword[j]) ) % 26 ) + 'a';                     printf("%c", a);                     j++;                    }                  }         else         {             printf("%c", p[i] );         }        }      return 0;     } } 

... not sure how take consideration if keyword has less characters string entered.

accessing keyword[j] beyond end of keyword[] bad. (undefined behavior). happens op's code when key shorter alpha part of message.

simply re-use keyword[] string needed. hint: reset j.

mouse on answer.

int j_length = strlen(keyword);
...
j++;
if (j >= j_length) j = 0;


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -