I'm stuck (apologies if I just didn't find the answer to this - I did read a number of answers and didn't see a solution to this problem).
I have a line in my code to print "ciphertext:" before the output of the program. However, my output will correctly create one word and then wrap around to start overwriting the "ciphertext:". For example - keyword is bacon, plaintext is Meet me at the park at eleven am. The output should be: ciphertext: Negh zf av huf pcfx bt gzrwep oz My output is: `wep ozext: Negh It works if I add a \n after every letter...
Any help would be greatly appreciated. I am very new to this, so my code probably has quite a number of errors.
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
// giving the program a heads-up that a function will be defined later
int shift(char c);
// gettting an argument in the command line
int main(int argc, string argv[])
{
// checking the number of words in command line
if (argc==2)
{
// creating a constant for using the second word in command line
const int KEY = 1;
// string length of argv1
int w = strlen(argv[KEY]);
// loop 1 to check for digits in the second argument of the command line
for (int z = 0; z < w; z++)
{
// checking for digits
if isdigit(argv[KEY][z])
{
printf("Usage: ./vigenere keyword\n");
return 1;
}
}
// getting the text to cipher
string s = get_string("plaintext: ");
// Ciphertext will be shown here
printf("ciphertext: ");
//Putting the string length here because it was causing problems with the output
int j = strlen(s);
// loop 2 - let's cipher that text by looping the key and the plaintext
for (int x = 0, i = 0; i < j && x < w; i++)
{
// Creating the integer for converting
int d = (int) s[i];
// Wrapping around the key loop
if (x < (w-1))
{
// numeric value to shift the key
int v = shift(argv[KEY][x]);
// turning character into integer
if isupper(s[i])
{
int up = (((d - 'A') + v) % 26) + 'A';
printf("%c", (char) up);
// Wrapping around the key by going up one in the counter
x++;
}
// Change lower case by X
else if islower(s[i])
{
int low = (((d - 'a') + v) % 26) + 'a';
printf("%c", (char) low);
// Wrapping around the key by going up one in the counter
x++;
}
// Non changed characters
else
{
printf("%c", (char) d);
}
}
// second part of the loop to wrap back to zero
else
{
// numeric value to shift the text
int v = shift(argv[KEY][x]);
// turning character into integer
if isupper(s[i])
{
int up = (((d - 'A') + v) % 26) + 'A';
printf("%c", (char) up);
// Wrapping around the key by going back to zero
x = 0;
}
// Change lower case by X
else if islower(s[i])
{
int low = (((d - 'a') -+ v) % 26) + 'a';
printf("%c", (char) low);
// Wrapping around the key by going back to zero
x = 0;
}
// Non changed characters
else
{
printf("%c", (char) d);
}
}
}
printf("\n");
return 0;
}
else
{
printf("Usage: ./vigenere keyword\n");
}
}
int shift(char c)
{
if isupper(c)
{
int u = c - 'A';
printf("%c", (char) u);
return u;
}
else
{
int l = c - 'a';
printf("%c", (char) l);
return l;
}
}