9, నవంబర్ 2012, శుక్రవారం

C PROGRAM FOR CAESAR CIPHER.




#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

    int encode(int ch, int key) {
            if (islower(ch)) {
                    ch = (ch-'a' + key) % 26 + 'a';
                    ch += (ch < 'a') ? 26 : 0;
            }
            else if (isupper(ch)) {
                    ch = (ch-'A' + key) % 26 + 'A';
                    ch += (ch < 'A') ? 26 : 0;
            }
            return ch;
    }

    int decode(int ch, int key) {
            return encode(ch, -key);
    }

    int main(int argc, char **argv) {
            int ch;
            int key;

            if (argc < 2) {
                    printf("USAGE: cipher <integer key> <encode | decode>\n");
                    printf("Then, just type your text and it will automatically output the en/de crypted text! :)\n");
                    return 1;
            }

            key = atoi(argv[1]);
            if (key < 1 || key > 25) {
                    printf("Key is invalid, or out of range. Valid keys are integers 1 through 25.\n");
                    return 1;
            }

            int (*f)(int, int) = (argv[2][0] == 'd') ?
                    decode :
                    encode;

            while (EOF != (ch=getchar()))
                    putchar(f(ch, key));

            return 0;
    }

Another version of ceaser cipher program     

I'm a beginner-intermediate C++ programmer, and I never used or understood C input & validation. I just always used C++ streams.
So, wanting to learn at least the basics of C before I continue with C++, I'm writing some small programs to understand it better. (And soon, a small Text-Based Adventure Game, for the purpose of learning how to cope without Objects! LOL)
Anyway, I just want code critique, as I have never used the C input functions (I admit, I have used and like printf()! Even made my own sprintf(), compatible with the C++ string class)
I hope my code is good. I tend to get code to work properly, completely ignoring formatting and performance, then later once it works I make it look nice. (And run nice.)
Here it is! Hope I did well :)
UPDATE: Fixed a couple minor bugs
UPDATE 2: Improved code syntax - Thanks for the pointer about char pointers! :)
UPDATE 3: Total reconstruction of the syntax, minimizing the code size - Thanks!!!!
main.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
 
    int encode(int ch, int key) { 
            if (islower(ch)) {
                    ch = (ch-'a' + key) % 26 + 'a';
                    ch += (ch < 'a') ? 26 : 0;
            }
            else if (isupper(ch)) {
                    ch = (ch-'A' + key) % 26 + 'A';
                    ch += (ch < 'A') ? 26 : 0;
            }
            return ch;
    }
 
    int decode(int ch, int key) { 
            return encode(ch, -key);
    }
 
    int main(int argc, char **argv) { 
            int ch;
            int key;
 
            if (argc < 2) {
                    printf("USAGE: cipher <integer key> <encode | decode>\n");
                    printf("Then, just type your text and it will automatically output the en/de crypted text! :)\n");
                    return 1;
            }
 
            key = atoi(argv[1]);
            if (key < 1 || key > 25) {
                    printf("Key is invalid, or out of range. Valid keys are integers 1 through 25.\n");
                    return 1;
            }
 
            int (*f)(int, int) = (argv[2][0] == 'd') ? 
                    decode : 
                    encode;
 
            while (EOF != (ch=getchar()))
                    putchar(f(ch, key));
 
            return 0;
    }

కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి