Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 4220
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 3, 20222022-06-03T15:10:28+00:00 2022-06-03T15:10:28+00:00

c – CS50 – pset2 – Substitution – “output not valid ASCII text”

  • 0

[ad_1]

My program is producing what is seems like the correct output but i still get the 🙁 message when i run check50. I have already read other awnsers to similar questions but none of them seems actually similar to my problem.

check50 output:

:) substitution.c exists

:) substitution.c compiles

:) encrypts "A" as "Z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key

:) encrypts "a" as "z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key

:) encrypts "ABC" as "NJQ" using NJQSUYBRXMOPFTHZVAWCGILKED as key

:) encrypts "XyZ" as "KeD" using NJQSUYBRXMOPFTHZVAWCGILKED as key

:) encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZTEOGXHCIPJSQD as key

->:( encrypts "This is CS50" as "Cbah ah KH50" using yukfrnlbavmwzteogxhcipjsqd as key
    output not valid ASCII text

->:( encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZteogxhcipjsqd as key
    output not valid ASCII text

:) encrypts all alphabetic characters using DWUSXNPQKEGCZFJBTLYROHIAVM as key

:) does not encrypt non-alphabetical characters using DWUSXNPQKEGCZFJBTLYROHIAVM as key

:) handles lack of key

:) handles too many arguments

:) handles invalid key length

:) handles invalid characters in key

:) handles duplicate characters in key

:) handles multiple duplicate characters in key

I put the -> before the error messages for easier visualization

It is weird because right before the two errors there is an almost identical input/output that has been checked as correct

Here is my code:

#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

string subs(string plain, string key);

int key_check(string key);

int main(int argc, string argv[]) {

    // CHECK IF IT HAS AN INPUT
    if (argc < 2) {
        printf("Usage: ./substitution key\n");
        return 1;
    }

    // CHECK IF IT HAS MORE THAN 1 INPUT
    if (argc > 2) {
        printf("Usage: ./substitution key\n");
        return 1;
    }

    // IF KEYCHECK FUNCTION DETECTS AN ERROR, RETURN 1
    if (key_check(argv[1]) == 1) {
        return 1;
    }
    // ELSE KEY = USER ARGV INPUT
    string key = argv[1];

    // GET USER PLAINTEXT INPUT
    string plain = get_string("plaintext:  ");

    string cipher = subs(plain, key);

    // PRINT RESULT
    printf("ciphertext: %s\n", cipher);

}

int key_check(string key) {
    // STRING LENGHT
    int leng = strlen(key);

    // CHECK IF KEY HAVE 26 CHARACTERS
    if (leng < 26) {
        printf("Key must contain 26 characters.\n");
        return 1;
    }


    for (int i = 0; i < leng; i++) {

        // CHECK IF KEY ONLY HAVE ALPHABET CHARACTERS
        if (isalpha(key[i]) == 0) {
            printf("Key must contain only alphabet characters\n");
            return 1;
        }

        // CHECK IF KEY HAVE REPEATED CHARACTER
        for (int i2 = 0; i2 < 26; i2++) {

            if (i != i2) {

                if (key[i] == key[i2]) {
                    printf("Key must have each character exactly one time\n");
                    return 1;
                }
            }
        }
    }

    return 0;
}

string subs(string plain, string key) {
    // GET PLAINTEXT LENGHT
    int leng = strlen(plain);

    // CREATES CIPHER STRING
    string cipher = plain;

    // CREATES AN ARRAY FOR UPPER KEY
    int UPPER[26];
    for (int i2 = 0; i2 < 26; i2++) {
        if (isupper(key[i2]) > 0 ) {
            UPPER[i2] = key[i2];
        }
        else {
            UPPER[i2] = key[i2] - 32;
        }
    }

    // CREATES AN ARRAY FOR LOWER KEY
    int LOWER[26];
    for (int i3 = 0; i3 < 26; i3++) {
        if (islower(key[i3] > 0)) {
            LOWER[i3] = key[i3];
        }
        else {
            LOWER[i3] = key[i3] + 32;
        }
    }

    for (int i = 0; i < leng; i++) {
        if (isupper(plain[i]) > 0) {
            cipher[i] = UPPER[plain[i] - 65];
        }
        else if (islower(plain[i]) > 0) {
            cipher[i] = LOWER[plain[i] - 97];
        }
        else {
            cipher[i] = plain[i];
        }
    }

    return cipher;

}

It all leads me to think that it is a check50 problem, but with my lack of experience with coding and problem solving it can be anything.

Thanks in advance.

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 8087 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1871 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1957 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.