Ivo Oostwegel
Ivo Oostwegel

Reputation: 448

C Gives strange output when variable scope changes

C does some spooky things when I put the nonAlphaCount declaration above the for loop. I can't explain why the outputs are different.

For version 1 (int declaration above main method) my input output was: INPUT: ./Vigenere.exe bacon Enter plain text: Meet me at the park at eleven am OUTPUT: Negh zf av huf pcfx bt gzrwep oz

For version 2 ( int declaration above for loop ) INPUT: ./Vigenere.exe bacon Enter plain text: Meet me at the park at eleven am OUTPUT: NRQQ M[L \M^^ KQXXZQZ M

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

const int INPUT_LEN = 255; 
const int ALPHABET_LEN = 26;
int nonAlphaCount = 0;

int main (int count, char *args[])
{

    char plainText[INPUT_LEN];
    char *cipherText;
    char *keyWord;

    if ( count < 2 || count > 2)
    {
        printf("There is no key");
        return 1;
    }

    strcpy(keyWord, args[1]);
    int keyWord_LEN = strlen(keyWord);

    printf("Enter plain text: ");
    fgets (plainText, INPUT_LEN, stdin);

    int strLength = strlen(plainText);
    cipherText = malloc(strLength);

    printf("%s", plainText);

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

        if(plainText[i] == '\0' || plainText[i] == '\n'|| plainText[i] == '\r')
            break;

        if(isalpha(plainText[i]))
        {
            // Default lower 
            int asciiUpperOrLower = 97;   
            int keyUpperOrLower = 97;    

            if(isupper(plainText[i]))
                asciiUpperOrLower = 65;

            if(isupper(keyWord[i % keyWord_LEN]))
                keyUpperOrLower = 65;

            int Key = keyWord[(i - nonAlphaCount) % keyWord_LEN] - keyUpperOrLower;
            int alphabetBaseletter = ((plainText[i] - asciiUpperOrLower + Key) % ALPHABET_LEN);

            cipherText[i] = alphabetBaseletter + asciiUpperOrLower;
        }
        else{
            cipherText[i] = plainText[i];
            nonAlphaCount++;
        }

    }

    // Set string terminator.
    cipherText[strLength - 1] = '\0' ;

    printf("%s", cipherText);

    return 0;

}





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

const int INPUT_LEN = 255; 
const int ALPHABET_LEN = 26;

int main (int count, char *args[])
{

    char plainText[INPUT_LEN];
    char *cipherText;
    char *keyWord;

    if ( count < 2 || count > 2)
    {
        printf("There is no key");
        return 1;
    }

    strcpy(keyWord, args[1]);
    int keyWord_LEN = strlen(keyWord);

    printf("Enter plain text: ");
    fgets (plainText, INPUT_LEN, stdin);

    int strLength = strlen(plainText);
    cipherText = malloc(strLength);

    printf("%s", plainText);

**int nonAlphaCount = 0;**
    for (int i = 0; i < strLength; i++ ){

        if(plainText[i] == '\0' || plainText[i] == '\n'|| plainText[i] == '\r')
            break;

        if(isalpha(plainText[i]))
        {
            // Default lower 
            int asciiUpperOrLower = 97;   
            int keyUpperOrLower = 97;    

            if(isupper(plainText[i]))
                asciiUpperOrLower = 65;

            if(isupper(keyWord[i % keyWord_LEN]))
                keyUpperOrLower = 65;

            int Key = keyWord[(i - nonAlphaCount) % keyWord_LEN] - keyUpperOrLower;
            int alphabetBaseletter = ((plainText[i] - asciiUpperOrLower + Key) % ALPHABET_LEN);

            cipherText[i] = alphabetBaseletter + asciiUpperOrLower;
        }
        else{
            cipherText[i] = plainText[i];
            nonAlphaCount++;
        }

    }

    // Set string terminator.
    cipherText[strLength - 1] = '\0' ;

    printf("%s", cipherText);

    return 0;

}

Upvotes: 0

Views: 48

Answers (1)

Weather Vane
Weather Vane

Reputation: 34585

Both programs exhibit undefined behaviour in

char *keyWord;
...
strcpy(keyWord, args[1]);

which is revealed by the compiler warning: "uninitialized local variable 'keyWord' used". You have not allocated any memory.

If one of the programs happened to work, so be it.

Upvotes: 1

Related Questions