# Using ASCII Values

• 03-08-2004
ga836044
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?

• 03-08-2004
Sebastiani
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)
• 03-08-2004
Prelude
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.
• 03-16-2004
ga836044
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.
• 03-16-2004
kermit
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?
• 03-16-2004
ga836044
OOPS. I saw it in my book for school in an example, and thought it had to be defined.
• 03-16-2004
WaltP
Re: Stuck on Pointers
Quote:

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
• 03-17-2004
Salem
> 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]