Caesar Cypher in C giving incorrect output -


trying create caesar cypher in c , getting troublesome output lowercase letters exceed 'z' when shifting letters

#include <stdio.h>  void confab(const char* intext, int shift, char* outtext)  {     int = 0;     int j = 0;     char character = '\0';     while (intext[i] != '\0') {         if (intext[i] >= 'a' && intext[i] <= 'z') {             character = intext[i] + shift;             if (character > 'z') {                 character = character - 'z' + 'a' - 1;             }             outtext[j] = character;             j += 1;         } else if (intext[i] >= 'a' && intext[i] <= 'z') {             character = intext[i] + shift;             if (character > 'z') {                 character = (character - 'z' + 'a' - 1);             }             outtext[j] = character;             j += 1;         } else {             outtext[j] = intext[i];             j += 1;         }         += 1;     } }   int main(void) {     char buffer[60] = {0};     char* s = "the quick brown fox jumps on lazy dog.";     confab(s, 13, buffer);     printf("%s\n", buffer); } 

is giving output:

gur d\202vpx oeb\204a sb\205 w\202zc\200 b\203re \201ur yn\207\206 qbt. 

instead of:

gur dhvpx oebja sbk whzcf bire gur ynml qbt. 

suspecting ascii arithmetic off in if statement lowercase letters.

the problem code character variable declared signed 1-byte number.

so looking @ ascii table, a-z in range of 97-122. problem when you're trying shift more 5, overflow because signed 1-byte number can't exceed 127.

so changing char character = '\0'; unsigned char character = '\0'; job.

you can refer here maximum number of integer byte - https://en.wikipedia.org/wiki/integer_(computer_science)#common_integral_data_types


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -