Thread: changing letters by rotating them around?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    21

    changing letters by rotating them around?

    i'm suppose to use this code to rotate characters from this input file (blah_input.txt)....

    Now is the time for all good persons to come to the aid of their
    party. Hello world! 1234 !@#$

    to this output file(blah_output.txt)...

    Abj vf gur gvzr sbe nyy tbbq crefbaf gb pbzr gb gur nvq bs gurve
    cnegl. Uryyb jbeyq! 1234 !@#$

    instead i just end up with chinese characters for some reason....help please.


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    
    int (encode(int input_char));
    int main(void)
    {
            FILE * in;
            FILE * out;
            char input_char;
            int char_total = 0;
    
            in = fopen("blah_input.txt", "r");
            if(in == NULL)
            {
                    printf("Sorry, unable to open input file.\n");
                    exit(1);
            }
    
            out = fopen("blah_output.txt", "w");
            if (out == NULL)
            {
                    printf("Sorry, unable to open output file.\n");
                    exit(1);
            }
    
            while ((input_char = fgetc(in)) != EOF)
            {
                    fputc(encode(input_char), out);
                    char_total = char_total + 1;
            }
    
            fprintf(out, "\n%d characters", char_total);
    
    
            fclose(in);
            fclose(out);
    
            return 0;
    }
    int encode(int input_char)
    
            {
                    if (('a' <= input_char) && (input_char <= 'm'))
                    {
                            return input_char + 13;
                    }
    
                    else if (('n' <= input_char) && (input_char <= 'z'))
                    {
                            return input_char - 13;
                    }
    
                    if (('A' <= input_char) && (input_char <= 'M'))
                    {
                            return input_char + 13;
                    }
    
                    else if (('N' <= input_char) && (input_char <= 'Z'))
                    {
                            return input_char - 13;
                    }
            }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You need to make sure you are changing characters into readable characters, not jarble.

  3. #3
    Registered User skringla's Avatar
    Join Date
    Nov 2008
    Posts
    8
    Why don't you have the string.h library included. It would seem like that standard lib would be best. Your while statement is actually going to set that variable. Or am I too much of a noob to read this code correctly?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    What platform are you on?

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Your encode() function doesn't return anything for characters that aren't alphabetical, so any non-alphabetic characters are going to be replaced with garbage -- the garbage probably contains weird escape sequences which are throwing your terminal into a funky mode.

    A non-alphabetic character should just be replaced with itself, so maybe try adding this at the very end of the encode function:

    Code:
    return input_char;
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating random letters
    By ominub in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 01:12 AM
  2. need to count individual letters in input
    By epox in forum C Programming
    Replies: 12
    Last Post: 05-22-2006, 06:32 AM
  3. Counting letters and digits
    By FeNCinGeR in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2006, 11:39 AM
  4. Changing windows without changing?
    By Lionmane in forum Windows Programming
    Replies: 7
    Last Post: 10-19-2005, 11:41 AM
  5. Switching letters
    By XiReDDeViLiX in forum Windows Programming
    Replies: 4
    Last Post: 06-06-2002, 06:48 AM