So I need to write a program which ciphers text adding x (argv[1] = x
) to a text prompted to the user. i.e:
./program 1 //running the program with argv[1] = 1
plaintext: abcd //prompting the user to write characters he want to cipher
ciphertext: bcde // returning plaintext ciphered with the argv[1] "key" = 1
This is my code
int main (int argc, string argv[])
{
if (argc != 2)
{
printf("Usage: ./ceasar key\n");
return 1;
}
else if (argc ==2)
{
int k = atoi(argv[1]);
for (int j = 0, len = strlen(argv[1]); j < len; j++)
{
if (!isdigit(argv[1][j]))
{
printf("Usage: ./ceasar key\n");
return 1;
}
}
for (int j = 0, len = strlen(argv[1]); j < len; j++)
{
if (isdigit(argv[1][j]))
{
string s = get_string("plaintext: ");
printf("ciphertext: ");
for (int i = 0, n = strlen(s); i <= n; i++)
{
if ('@' < s[i] && s[i] < '[')
{
printf("%c", (s[i] - 'A' + k) % 26 + 'A');
}
else if('`' < s[i] && s[i] < '{')
{
printf("%c", (s[i] - 'a' + k) % 26 + 'a');
}
else
{
printf("%c", s[i]);
}
printf("\n");
return 0;
}
}
}
}
}```
The first lines checks if argc !=2, and if argv[1][j] has a non numeric character. Once that is done it will get argv[1] and add it to each character given from the user. but it wont work correctly.
**Any sugestions?**