-2

It's supposed to do the following

  1. Ask for input : Stack (e.g)
  2. Ask for key : qwertyuioplkjhgfdsazxcvbnm (e.g)
  3. Convert into cipher text : Azqel

Here's the new updated code:

//Encrypting user input by using 26 character long alphabetic key provided by user
#include <stdio.h>
#include <string.h>
int main(){
char key[26];
char text[1000];
//Prompt user for key
do{
    printf("Provide a 26 character long key : ");
    scanf("%s", key);
    //If key is empty, exit code
    if(strlen(key) == 0){
        printf("Error: Empty input");
        return 1;
    }
    //If key is incomplete, prompt user again for complete key
    if(strlen(key)!= 26){
        printf("Error: Incomplete key\n");
    }
}while(strlen(key) != 26);
//If key has values other than alphabets, exit code
for(int i= 0, n= strlen(key); i<n; i++){
    if((key[i]< 'a' || key[i]> 'z') && (key[i]< 'A' || key[i]> 'Z')){
        printf("Error: Invalid key");
        return 2;
    }
}
//If key has repeated values, exit code
for(int i= 0; i< strlen(key); i++){
    for(int j= i+ 1; key[j]!= '\0'; j++){
        int x, y;
        if(islower(key[i])){
            x = key[i] - 'a';
        }
        else {
            x = key[i] - 'A';
        }
        if(islower(key[j])){
            y = key[j] - 'a';
        }
        else {
            y = key[j] - 'A';
        }
    if(x == y){
    printf("Error: Repeated characters in key");
    return 3;
    }
    }
}
//Prompt user for input
printf("Plaintext : ");
scanf("%s", text);
//If input is empty, exit code
if(strlen(text) == 0){
    printf("Error: Empty input");
    return 4;
}
printf("Ciphertext: ");
for(int i= 0, n= strlen(text); i< n; i++){
    //Encrypting small letters
    if(text[i] >= 'a' && text[i] <= 'z'){
         printf("%c", key[text[i]-'a']);
    }
    //Encrypting capital letters
    else if(text[i] >= 'A' && text[i] <= 'Z'){
         printf("%c", key[text[i]-'A']-('a'-'A'));
    }
    //Printing characters other than alphabets
    else{
        printf("%c", text[i]);
    }
}
return 0;
}

The bug where code was treating lower case characters and upper case characters differently even if they were same has been fixed now. If the key is for example : qwertyuioplkjhgfdsazxcvbmM (it has the letters 'm' and 'M' but they were being treated as different characters)

  • 1
    As a side note: `gets()` is dangerous and shouldn't be used, read the reason [here](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). – JASLP doesn't support the IES Aug 04 '21 at 04:46
  • Infinite innermost `do`-`while` loop. It continues while `c == j`, but the body of the loop never changes `c` or `j`. Also, rather than using magic number-based tests like `c>= 97 && c<= 122` (which are technically non-portable) try using standard functions such as `islower()` to check for a lowercase letter. – Peter Aug 04 '21 at 05:00

1 Answers1

-1

you can try this:

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

int main(){
   char s[1000];
   char t[26];
   printf("Text: ");
   scanf("%s", s);
   printf("Key: ");
   scanf("%s", t);
   for(int i= 0, n= strlen(s); i< n; i++){
      if(s[i] >= 'a' && s[i] <= 'z') printf("%c", t[s[i]-'a']);
      else printf("%c", t[s[i]-'A'] + ('A'-'a'));
   }
   return 0;
}

and as said in the comments, try not to use numbers like 97 and 122, use 'a' and 'z' instead.

and for the bug mentioned in the updated code, instead of a simple check like key[j] == key[i] do this

int x, y;
if(islower(key[i])) x = key[i] - 'a';
else x = key[i] - 'A';
if(islower(key[j])) y = key[j] - 'a';
else y = key[j] - 'A';

if(x == y){
    printf("Error: Repeated characters in key");
    return 3;
}

or you can use a bool check[26], traverse the key, make the value of a character true if(islower(key[i])) check[key[i]-'a'] = true; else check[key[i]-'A'] = true; and lastly check whether the entire bool array is true.

susanth29
  • 356
  • 1
  • 2
  • 17