Help, please. The program works fine until I want to add the loops and when I do that the if statements inside it won't work and it will just ask for the key and will output the same text as the input but if instead I don't use the loops and set i = 0 (a possible value), it will work just fine but only with the first letter because I still need the loops to move onto the different characters in the string. Here is the code:
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
if (argv[1] == '\0')
{
printf ("Usage: ./caesar key\n");
return 0;
}
int d = 0;
while (strlen(argv[1]) > d)
{
if (argv[1][d] < 48 || argv[1][d] > 57 )
{
printf("Usage: ./caesar key\n");
return 0;
}
else
{
d++;
}
}
if (argc != 2)
{
printf("Usage: ./caesar key\n");
}
else
{
char *p;
//ArgvX is the same value as argv[1] but converted to an int
int ArgvX = strtol(argv[1], &p, 10);
string Plaintext = get_string("Plaintext: ");
for (int i = 0;Plaintext[i] < strlen(Plaintext) ;i++)
{
//These are the parameters I did using ASCCI values of uppercase letters.
while (Plaintext[i] <= 90 && 65 <= Plaintext[i])
{
if (Plaintext[i] + (ArgvX % 26) > 90)
{
// This is the function if the value exceeds the ASCCI for uppercase letters.
Plaintext[i] = (Plaintext[i] - 26 + (ArgvX % 26));
}
else
{
Plaintext[i] = (Plaintext[i] + (ArgvX % 26));
}
}
//These are the parameters I did using ASCCI values of lowercase letters.
while (Plaintext[i] <= 122 && 97 <= Plaintext[i])
{
if (Plaintext[i] + (ArgvX % 26) > 122)
{
// This is the function if the value exceeds the ASCCI for lowercase letters.
Plaintext[i] = (Plaintext[i] - 26 + (ArgvX % 26));
}
else
{
Plaintext[i] = (Plaintext[i] + (ArgvX % 26));
}
}
}
printf("%s\n",Plaintext);
}
}