Need help in Caesar Cipher in C -


i new programming , trying write program in c caesar cipher.

input consists of integer ilength equal length of string, followed string str , integer encrypt.

my input is:

11 middle-outz 2 

output:

okffng-qwv@ 

required output is:

okffng-qwvb 

below code have written. me why getting last character wrong in output!

i'm totally clueless.

#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h>  int main() {   int ilength = 0, encrypt = 0, = 0, j = 0;    char alph_base[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};    scanf("%d", &ilength);   char str[ilength + 1];   scanf("%s", str);   scanf("%d", &encrypt);   //printf("%c\n", str[5]);    char outputstring[ilength + 1];   char temp[ilength + 1];    (j = 0; j <= ilength; j++)   {     temp[j] = str[j];     = 0;      if (str[j] == '\0')     {       outputstring[j] = '\0';     }      while ((i >= 0) && (i < 26))     {       if (temp[j] == alph_base[i])       {         if (i == 25 && encrypt == 0)         {           outputstring[j] = alph_base[25];         }          if ((i + encrypt) == 26)         {           outputstring[j] = alph_base[(i + encrypt) % 26];         }         else           outputstring[j] = alph_base[(i + encrypt) % 26];       }       if ((temp[j] < 65 || temp[j] > 90) && temp[j] < 97)         outputstring[j] = temp[j];        if ((temp[j] < 97 || temp[j] > 122) && temp[j] > 90)         outputstring[j] = temp[j];        i++;     }      while ((i > 25) && (i < 52))     {       if (temp[j] == alph_base[i])       {         if (i == 51 && encrypt == 0)         {           outputstring[j] = alph_base[51];         }          if ((i + encrypt) == 51)         {           outputstring[j] = alph_base[51];         }          if ((i + encrypt) > 51)         {           outputstring[j] = alph_base[((i + encrypt) % 51) + 25];         }         else           outputstring[j] = alph_base[(i + encrypt) % 51];       }       if ((temp[j] < 65 || temp[j] > 90) && temp[j] < 97)         outputstring[j] = temp[j];        if ((temp[j] < 97 || temp[j] > 122) && temp[j] > 90)         outputstring[j] = temp[j];       i++;     }   }   printf("%s\n", outputstring);   return 0; } 

your code complex want do.

your issue seems related looping 'z' 'a'.

a simple function can work character:

#include <ctype.h>  char caesar_encrypt(char input, int key) {     char output = input;     char base, offset;     // if not letter, return char unmodified     if (! isalpha(input))     {         return output;     }      base = isupper(input) ? 'a' : 'a'; // check if upper/lower case     offset = input - base; // take offset 'a'     offset += key; // add key offset     offset %= 26; // wrap offset 26 letters      output = base + offset;     return output; } 

many ideas here:

  1. use functions <ctype.h> (isalpha, isupper), avoids many comparaisons in code.

  2. consider characters 'offset' letter (uppercase or lowercase a). you're working numbers in range [0;25], , can wrap using simple modulus

  3. charaters 'integers', can add or subtract them. third letter of uppercase alphabet, can char c = 'a' + 2;, simpler huge array.

disclaimer: code written here, not tested, may contain typos ;)


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -