vigenere - Pset2 Need help encrypting words -
vigenere encrypting message incorrectly example if key "hello" , message hello, encrypted text "eipsv" when should "hello".
i appreciate tips on fixing bug.
string message = getstring(); int m = strlen(message); int = 0; if(isalpha(message[i])) { for(int j = 0; j < n; i++) { key[j] = tolower(key[j]) - 97; j++; (i = 0; < m; i++) { char c = message[i]; if (islower(c)) { c = (((c - 'a' + key[j%n])%26) +'a'); j++; printf("%c", c); } if (isupper (c)) { c = (((c - 'a' + key[j%n])%26) +'a'); j++; printf("%c", c); } else if (!isupper(c) && !islower(c)) { printf("%c", c); j++; } } } } printf("\n"); }
ok, handful of changes:
your first loop convert key values of 0 - 25 should in separate loop encoding loop. convert key, , move on encoding loop.
in encoding loop, need 2 incrementers, "i" , "j", both of need reinitialized 0 in ( ...;...;... ) line. can't reuse "j" key conversion loop because out of scope.
remove j++ after printf() line else if statement in encoding loop. not increment "j" if letter isn't encoded.
by way, once update program , run through check50, find several error-check errors because have error conditions check don't see in current code. but, should encode correctly above changes.
Comments
Post a Comment