0

I have my function to convert a string.
The goal is to encrypt the user input.
My function is working but sometimes, it adds characters.

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

bool convert_string(string text, string cypher);
bool checkCypher(string cypher);

// Main function
int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./substitution key\n");
        return 1;
    }
    else if (strlen(argv[1]) != 26)
    {
        printf("Key must contain 26 characters.\n");
        return 1;
    }
    else
    {
        // Check cypher before process
        bool cypherGood = checkCypher(argv[1]);
        if(!cypherGood) return 1;

        // All ok, process
        string text = get_string("Enter string \n");
        convert_string(text, argv[1]);
    }

    return 0;
}

// Function to check if the cypher is valid
bool checkCypher(string cypher)
{
    int cypherLength = 26;
    char testedLetters[26];

    for (int i = 0; i<26; i++)
    {
        char letter = toupper(cypher[i]);
        if((letter >= 65 && letter <= 90))
        {
            char match = testedLetters[letter - 65];
            if(match == 1)
            {
                printf("Duplicate letter");
                return false;
            }
            else
            {
                testedLetters[letter - 65] = 1;
            }
        }
        else
        {
            printf("Non valid character");
            return 1;
        }
    }

    return true;
}

// Function to convert the string if all the criteria pass
bool convert_string(string text, string cypher)
{
    // Take the cypher and convert it to an array
    char string_array[26];

    for (int i = 0; i < 26; i++)
    {
        string_array[i] = toupper(cypher[i]);
    }

    // Look for the text and convert
    char text_converted[strlen(text)];

    for (int j = 0; j < strlen(text); j++)
    {

        int text_pos = text[j];
        char letter;

        if (text_pos >= 65 &&  text_pos <= 90)
        {
            letter = string_array[text_pos - 65];
            text_converted[j] = letter;
        }
        else if (text_pos >= 97 &&  text_pos <= 122)
        {
            letter = string_array[text_pos - 97];
            text_converted[j] = tolower(letter);
        }
        else
        {
            letter = text[j];
            text_converted[j] = letter;
        }
    }

    printf("ciphertext: %s\n", text_converted);

    return true;
}

Input examples:
argv[]: NJQSUYBRXMOPFTHZVAWCGILKED, Input: ABC, Output: NJQ => Good
argv[]: NJQSUYBRXMOPFTHZVAWCGILKED, Input: XyZ, Output: KeD => Good
argv[]: YUKFRNLBAVMWZteogxhcipjsqd, Input: This is CS50, Output: Cbah ah KH50bU => Should be: Cbah ah KH50
argv[]: YUKFRNLBAVMWZteogxhcipjsqd, Input: HelloWorld, Output: NJQ => Good
enter image description here

Do you have an idea why it does that?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
AnthoVdo
  • 102
  • 6
  • 2
    `text_converted` is not properly terminated and is also too small. `char text_converted[strlen(text) + 1];` and `text_converted[strlen(text)] = 0;` – Retired Ninja Sep 09 '22 at 19:41
  • 1
    Note that `char testedLetters[26];` does **not** initialize `testedLetters[]` so `match = testedLetters[letter - 65];` is a problem. – chux - Reinstate Monica Sep 09 '22 at 19:41
  • 1
    Whenever you construct a string by hand (as you're doing here, with `text_converted`), you always always always have to remember to take care of that null terminator. – Steve Summit Sep 09 '22 at 19:42

0 Answers0