0

I can't detect where I am leaking memory. Sometimes I get an unexpected output at the end of my array for cypher text and can't pass all test cases :(. Can someone point to me where I am wrong? Can someone point me where i am wrong

include

include

include

include

include

int main(int argc, string argv[]){ if(argc != 2){ printf("Usage:./caeser key\n"); return 1; } int k = atoi(argv[1]); if(k < 0){ printf("Usage: ./caeser key\n"); return 1; }

string check = argv[1];
for(int i =0,n =strlen(check);i<n;i++){
    //printf("%c\n",check[i]);
    if(!isdigit(check[i])){
        printf("Usage:./caeser key\n");
        return 1;
    }
}


//printf("%d",k);



string userInput;
userInput = get_string("plaintext: ");
//printf("%d\n",k);
//printf("%s\n", userInput);
int count = strlen(userInput);
//printf("\nInput length = %d",count);
char cipher[count+1];
cipher[count+1] = (char) 0;
int buffer[count+1];
int convertToAlphIndex;
int res;
for(int i = 0; i < count; i++){
    if(isalpha(userInput[i])){
        if(isupper(userInput[i])){
            convertToAlphIndex = userInput[i] - 65;
            res = (convertToAlphIndex+k)%26;
            buffer[i] = res+65;
            //printf("%c", buffer[i]);
            cipher[i] = (char)buffer[i];

        }

        if(islower(userInput[i])){
            convertToAlphIndex = userInput[i] - 97;
            res = (convertToAlphIndex+k)%26;
            buffer[i] = res+97;
           // printf("%c", buffer[i]);
            cipher[i] = (char)buffer[i];
        }
    }
    else{
        buffer[i] = userInput[i];
        //printf("%c",buffer[i]);
        cipher[i] = (char)buffer[i];
    }
}


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






return 0;

}

1

Try using '\0' in place of (char) 0 for null terminating character of cipher.

If this helps, click the tick mark.

You must log in to answer this question.

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