Having read the other posts I am still unable to solve the problem created by white space. Belows code only deals with the key as not to post too much code publically.
The white space is not handled by the final else statement. I also tried inserting an if/else == ' '
, but this did not work either.
int main(int argc, char *argv[]){
if(argc != 2) {
printf("Error on input");
exit(1);
}
string input = get_string("Input:");
string key = argv[1];
int len = strlen(key);
int keys[len];
for (int i = 0; i < strlen(key); i++) {
if(isupper(key[i])) {
keys[i] = key[i] - 65;
} else {
keys[i] = key[i] - 97;
}
}
for(int i = 0; i < strlen(input); i++) {
if(isalpha(input[i])) {
// if upper case to this
if(isupper(input[i])) {
// get keys from array above
int convertKey = keys[i % (strlen(key))];
printf("%i\n", convertKey);
} else if(!isupper(input[i])) {
int convertKey = keys[i % (strlen(key))];
printf("%i\n", convertKey);
}
} else if(isalpha(input[i]) == false) {
// or !isalpha(input[i])
// or (isalpha == ' ')
printf("%c", input[i]);
}
}
}
When I enter:
./vigenere bacon
Meetme => 10214131
Meet me => 10214 10
Above shows wrap skips the 13, but this is not even the white space (*edit- I see that it is now actually). I think the i
is accessing the incorrect element in the keys array. I don't know how to stop the wrap from moving during the space though, or know how to fix.