When I run check50 i get this:
:) caesar.c exists.
:) caesar.c compiles.
:) encrypts "a" as "b" using 1 as key
:( encrypts "barfoo" as "yxocll" using 23 as key
output not valid ASCII text
:) encrypts "BARFOO" as "EDUIRR" using 3 as key
:) encrypts "BaRFoo" as "FeVJss" using 4 as key
:( encrypts "barfoo" as "onesbb" using 65 as key
output not valid ASCII text
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
output not valid ASCII text
:) handles lack of argv[1]
:) handles non-numeric key
:) handles too many arguments
I am not sure what it is meant by "not valid ASCII text" or what the error is. When I run the question which I got "wrong" , I get the correct output in the terminal, but check50 marks it as wrong.
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <math.h>
int main(int argc, string argv[])
{
if (argc != 2)
{
return 1;
}
string bob=argv[1];
int lower_letters[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int upper_letters[]={'A', 'B', 'C', 'D', 'E','F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
for (int e=0; e<strlen(bob); e )
{
for (int g=0; g<26; g )
{
if ((bob[e])==(lower_letters[g]))
{
printf("do not enter something that is not a number \n");
return 1;
}
}
}
int total = atoi(argv[1]);
if (total<0)
{
printf("do not input something that is a negative value\n");
return 1;
}
string word1 =get_string("plaintext: ");
printf("ciphertext: ");
for (int i =0; i<strlen(word1); i )
{
if ((word1[i]==' ') || (word1[i]==',') || (word1[i]=='.') || (word1[i]=='!'))
{
printf("%c",word1[i]);
}
for (int a =0; a<26; a )
{
if ((word1[i]==lower_letters[a]) && (a total>26))
{
printf("%c",lower_letters[(a total)-26]);
}
if (word1[i]==lower_letters[a])
{
printf("%c",lower_letters[a total]);
}
if ((word1[i]==upper_letters[a]) && (a total>26))
{
printf("%c",upper_letters[(a total)-26]);
}
if (word1[i]==upper_letters[a])
{
printf("%c",upper_letters[a total]);
}
}
}
printf("\n");
}
Can I please have help getting rid of the error "not valid ASCII text" please?
CodePudding user response:
if ((word1[i]==lower_letters[a]) && (a total>26)) { printf("%c",lower_letters[(a total)-26]); } if (word1[i]==lower_letters[a]) { printf("%c",lower_letters[a total]); }
Though you account for values of a total
greater than 26, you erroneously allow the second if
body, which doesn't account for this, to execute for those values, too. Also the subtraction -26
is inappropriate for values 52 or higher.
You can replace the above code with
if (word1[i]==lower_letters[a])
putchar(lower_letters[(a total)%26]);
- of course the same applies to upper_letters
.