0

i'm having these errors when i check my code. This is it:

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


int main(int argv, string argc[]) {
    int count = 0;
    for (int i = 0; i < 26; i++){
        if ((argc[1][i] < 'A' || argc[1][i] > 'Z') && (argc[1][i] < 'a' || argc[1][i] > 'z')){
            count = count + 1;
        }
    }    

    if ((argv != 2) || (count > 0)){   
        printf("Usage: ./substitution key\n");
        return 1;
    }
    else if (strlen(argc[1]) != 26){
        printf("Key must contain 26 characters.\n");
        return 1;
    }
    else {
        string plain = get_string("plaintext: ");
        int sub[26];
        for (int j = 0; j < 26; j++){
          if (argc[1][j] >= 'a' && argc[1][j] <= 'z'){
            sub[j] = argc[1][j];  
          }
          else if (argc[1][j] >= 'A' && argc[1][j] <= 'Z'){
            sub[j] = argc[1][j];  
          }      

        }

        string outro[1][strlen(plain)];
        for (int k = 0; k < strlen(plain); k++){
        char cypher;        
            if (plain[k] >= 'a' && plain[k] <= 'z'){
                cypher = sub[(int) plain[k] - 97];
                (char) outro[0][k] = (char) tolower(cypher);

            }
            else if (plain[k] >= 'A' && plain[k] <= 'Z'){
                cypher = sub[(int) plain[k] - 65]; 
                 (char) outro[0][k] = (char) toupper(cypher);

            }
            else {
                (char) outro[0][k] = (char) plain[k];
            }

            if (k == (strlen(plain) - 1)){
                string output = (string) outro[0];
                printf("ciphertext: %s\n", output);
            }
    }return 0;
  } 

}

Here's the error log:

:) substitution.c exists
Log
checking that substitution.c exists...
:) substitution.c compiles
Log
running clang substitution.c -o substitution -std=c11 -ggdb -lm -lcs50...
:( encrypts "A" as "Z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key
output not valid ASCII text
Log
running ./substitution ZYXWVUTSRQPONMLKJIHGFEDCBA...
sending input A...
checking for output "ciphertext: Z\n"...
:( encrypts "a" as "z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key
output not valid ASCII text
Log
running ./substitution ZYXWVUTSRQPONMLKJIHGFEDCBA...
sending input a...
checking for output "ciphertext: z\n"...
:( encrypts "ABC" as "NJQ" using NJQSUYBRXMOPFTHZVAWCGILKED as key
output not valid ASCII text
Log
running ./substitution NJQSUYBRXMOPFTHZVAWCGILKED...
sending input ABC...
checking for output "ciphertext: NJQ\n"...
:( encrypts "XyZ" as "KeD" using NJQSUYBRXMOPFTHZVAWCGILKED as key
output not valid ASCII text
Log
running ./substitution NJQSUYBRXMOPFTHZVAWCGILKED...
sending input XyZ...
checking for output "ciphertext: KeD\n"...
:) encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZTEOGXHCIPJSQD as key
Log
running ./substitution YUKFRNLBAVMWZTEOGXHCIPJSQD...
sending input This is CS50...
checking for output "ciphertext: Cbah ah KH50\n"...
checking that program exited with status 0...
:) encrypts "This is CS50" as "Cbah ah KH50" using yukfrnlbavmwzteogxhcipjsqd as key
Log
running ./substitution yukfrnlbavmwzteogxhcipjsqd...
sending input This is CS50...
checking for output "ciphertext: Cbah ah KH50\n"...
checking that program exited with status 0...
:) encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZteogxhcipjsqd as key
Log
running ./substitution YUKFRNLBAVMWZteogxhcipjsqd...
sending input This is CS50...
checking for output "ciphertext: Cbah ah KH50\n"...
checking that program exited with status 0...
:) encrypts all alphabetic characters using DWUSXNPQKEGCZFJBTLYROHIAVM as key
Log
running ./substitution DWUSXNPQKEGCZFJBTLYROHIAVM...
sending input The quick brown fox jumps over the lazy dog...
checking for output "ciphertext: Rqx tokug wljif nja eozby jhxl rqx cdmv sjp\n"...
checking that program exited with status 0...
:( handles lack of key
failed to execute program due to segmentation fault
Log
running ./substitution...
:) handles invalid key length
Log
running ./substitution QTXDGMKIPV...
checking that program exited with status 1...
:) handles invalid characters in key
Log
running ./substitution ZWGKPMJRYISHFEXQON2DLUACVT...
checking that program exited with status 1...
:( handles duplicate characters in key
timed out while waiting for program to exit
Log
running ./substitution YFDTSMPBVIERGHEWONUAKLQXCZ...
:( handles multiple duplicate characters in key
timed out while waiting for program to exit
Log
running ./substitution BBCCEFGHIJKLMNOPQRSTUVWXYZ...

Can anyone please help me find my (main) errors? At the end i created a string variable and assigned the values of the outro[] array thinking that the error was that it lacked "\0", but that wasn't the problem ( i think). When i run it myself with their values, it appear to be working just fine.

Thanks in advance!

2 Answers 2

1

Please do not use argv[1] before you checked argc == 2. That's the segmentation fault.

I don't see you check for duplicate letters (consider different case). Two more failing test cases.

Weirdest thing I see is

string outro[1][strlen(plain)];

This is an array of strlen(plain) pointers (basically 8-byte variables holding a memory address). I have no idea how this can work, and how it passes any tests. Make it a

char outro[strlen(plain) + 1];

and don't forget to set the last byte to zero ("null terminator"), as that's what makes it a string in C.

5
  • Thank you, i fixed the segmentation error and the duplicate letters, but i still can't understand how that null terminator works. At best it gives me a runtime error while showing the correct output. This is the updated last bit of code: char outro[strlen(plain) + 1]; outro[strlen(plain) + 1] = (char) NULL; for (int k = 0; k < strlen(plain); k++){ char cypher; if (plain[k] >= 'a' && plain[k] <= 'z'){ cypher = sub[(int) plain[k] - 97]; outro[k] = (char) tolower(cypher);
    – Fra.9501
    Mar 5, 2020 at 18:57
  • outro[strlen(plain)] = '\0';
    – Blauelf
    Mar 5, 2020 at 19:11
  • Brilliant! Thank you very much! I still don't understand why the absence of the NULL character was a problem just for the first few instances and not for the longer sentences. Is it internal to the cs50 check system, to the way it searches for problems?
    – Fra.9501
    Mar 5, 2020 at 19:37
  • There might have been a byte of zero in that location. Local variables and heap allocations (week 4) are not initialised by default, their initial values could be anything, values of a previous variable that used to be in the same location, but also just zero.
    – Blauelf
    Mar 5, 2020 at 19:42
  • Very clear, thank you once again! Cheers!
    – Fra.9501
    Mar 5, 2020 at 19:57
1

I had the same problem (error for 2nd to sixth tests), added a null char ('\0') to my ciphertext array and everything went right.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .