0

Here is 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 && check_distinct_char(argv[1]))
    {
    int n = strlen(argv[1]);
    if (n == 26 && !check_char(argv[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;
    }
    else
    {
        printf("Key must contain 26 characters.");
        return 1;
    }
    }
    else if (!check_distinct_char(argv[1]))
    {
        return 1;
    }
    else
    {
        printf("Usage: ./substitution key");
        return 1;
    }
}


bool check_char(string s)
{
    for (int i = 0, len1 = strlen(s); i < len1 ; i++)
    if (isdigit(s[i]))
    {
        return 1;
    }
    return 0;
}

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

I think it should work as there is a return value incase the letters are repeated in the string.

1

I think that you should change the check_distinct_char function's return type to int. Then if (argc == 2 && check_distinct_char(argv[1]) == 0)

If you don't want to do this the you can change return 1 to return FALSE and return 0 to return TRUE

If this helps check the tickmark

2
  • i could try that but what's wrong right now? Oct 10 '20 at 13:28
  • I don't know how a return type of bool is comprehending the integer. I will just edit my answer.
    – Vsjain
    Oct 10 '20 at 14:17

You must log in to answer this question.

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