0

My code -

#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
bool check_distinct_char(string s);
bool check_char(string s);
int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./substitution key");
        return 1;
    }
    if (!check_distinct_char(argv[1]))
    {
        return 1;
    }
    if (!check_char(argv[1]))
    {
        return 1;
    }
    int n = strlen(argv[1]);
    if (n != 26)
    {
        printf("Key must contain 26 characters.");
        return 1;
    }
        string plain = get_string("plaintext: ");
        printf("ciphertext: ");
        for (int j = 0, len = strlen(plain); j < len; j++)
        {
            if (isupper(plain[j]))
            {
            printf("%c", toupper(argv[1][((int)plain[j] - 65) % 26]));
            }
            else if (islower(plain[j]))
            {
            printf("%c", tolower(argv[1][((int)plain[j] - 97) % 26]));
            }
            else
            {
            printf("%c", plain[j]);
            }
         }
         printf("\n");
         return 0;
}
bool check_char(string s)
{
    for (int i = 0, len = strlen(s); i < len ; i++)
    if (isalpha(s[i]))
    {
        return true;
    }
    return false;
}

bool check_distinct_char(string s)
{
    for (int i = 0, len = strlen(s); i < len ; i++)
    {
        for (int j = 1; j < len ; j++)
        {
            if (s[i] == s[j])
            {
                return true;
            }
        }
    }
    return false;
}

I have reworked Substitution so that it is easier to understand however I am unable to understand the cause of the errors. It should be able to handle invalid keys (both keys with numbers and other non alphabetical characters and those with repetition) i believe but i just can't understand anymore what is wrong. Please pinpoint what exactly is wrong with the syntax or the logic

1

The placement of return true and return false have to be reversed. check_char should be used before check_distinct_char in the main program. Change the check_char function like this:

    int count = 0;
    for (int i = 0, len = strlen(s); i < len ; i++){
    if (isalpha(s[i]))
    {
        count++;
    }
    }
    if (count == 26){
        return true;
    }
    else {
        return false;
    }

If this helps, please check the tickmark

3
  • for which loop should return true and false be reversed? Oct 10 '20 at 14:43
  • also if i combine these 2 parts how will i print out 2 distinct error messages? Can you help me with the code in it's current form without combing stuff as then i wouldn't realize where was i going wrong earlier thanks :) Oct 10 '20 at 14:43
  • The reversal is in check_distinct_char function. You don't have to print separate error messages. And do not combine them. The answer I have given is the check_char function in code.
    – Vsjain
    Oct 10 '20 at 15:05

You must log in to answer this question.

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