Using ASCII Values

This is a discussion on Using ASCII Values within the C Programming forums, part of the General Programming Boards category; I am currently working on a project where I must translate a paragraph of english, into a different alphabet (basically ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    7

    Unhappy Using ASCII Values

    I am currently working on a project where I must translate a paragraph of english, into a different alphabet (basically our normal alphabet, but all letters are shifted down 15 spaces.)

    For example, 'A' would be 'P', 'B' is 'Q', 'C' is 'R'...and so forth.

    From what I've learned so far this semester, I believe I can use a giant switch statment to use after get char to "translate", but would that be the easiest way?

    I figure, there would be a pattern of adding 15 to the ASCII value to get the new, translated letter, but what happens when yuo get 'Z'? You couldn't add 15 do it, or it would give you a lowercase 'i'.

    Speaking of lowercase, if the original text has lowercase, than the translated letter must also be lowercase. Same for upper. (This I know I can just use isupper and islower, but just wanted to make sure that won't interfere with the rest of the code to translate).

    So what's the smartest and easiest way to translate this?

    Thanks in advance for your help.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Use an array...the psuedocode for that might be:

    - get input
    - subtract some specific value to obtain a zero-based index
    - if input is out of range, adjust to zero
    - input is now index into an array (where zeroth is 'error' index)
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Try two arrays, one with the normal alphabet and one with the shifted alphabet. Here is one with a single shift:
    Code:
    char *alph = "abcdefghijklmnopqrstuvwxyz";
    char *shif = "zabcdefghijklmnopqrstuvwxy";
    Find the right character, then use the corresponding character from the shifted array. Do the same thing for casing, or use isupper, islower, toupper and tolower to maintain consistency.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    7

    Stuck on Pointers

    OK, so I'm a bit confused on where to go from here. So far I have this code...

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #define	EOF	(-1)
    
    
    int main (void)
    {
    	int c;
    	char *alph 	= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	char *shift = "GHIJKLMNOPQRSTUVWXYZABCDEF";
    	
    	while( (c=getchar()) !=EOF '\n')



    but I'm not understanding 1.) how to read in a file character by character, and 2.) how i can use the pointers to change to the shift alphbet.

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    hmmm

    This does not have so much to do with your questions, but I was wondering why you defined EOF when EOF will already be defined with your compiler?

  6. #6
    Registered User
    Join Date
    Jan 2004
    Posts
    7
    OOPS. I saw it in my book for school in an example, and thought it had to be defined.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161

    Re: Stuck on Pointers

    Originally posted by ga836044
    OK, so I'm a bit confused on where to go from here. So far I have this code...

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #define	EOF	(-1)
    
    
    int main (void)
    {
    	int c;
    	char *alph 	= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	char *shift = "GHIJKLMNOPQRSTUVWXYZABCDEF";
    	
    	while( (c=getchar()) !=EOF '\n')



    but I'm not understanding 1.) how to read in a file character by character, and 2.) how i can use the pointers to change to the shift alphbet.
    1) open the file with fopen()
    in the loop, read a character from the file with fgetc()

    Start with that and just output the file to the screen. Then we can work in part 2
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > 2.) how i can use the pointers to change to the shift alphbet.
    Use a for loop to compare each alph[i] with the char you've just input.
    When they match, output shift[i]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading values from a file
    By megastar in forum C Programming
    Replies: 4
    Last Post: 06-25-2007, 03:08 AM
  2. Computing Large Values
    By swbluto in forum C++ Programming
    Replies: 8
    Last Post: 04-07-2005, 04:04 AM
  3. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 07:30 AM
  4. adding ASCII
    By watshamacalit in forum C Programming
    Replies: 0
    Last Post: 12-26-2002, 05:25 PM
  5. How to read in empty values into array from input file
    By wpr101 in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2002, 10:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21