Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help with a C Program

  1. May 9, 2007 #1
    Ok im having trouble writing this program in C.

    Basically what i have to write is a program that implements a cipher to a string of text.

    So it will ask the user to imput a Numeric key (lets say for instance 4)
    and a string of text (I.E ABCDEFG)

    and the output will be


    it has to do that for upper and lower case stuff, and leave numbers alone.

    Now i have a general idea of how to do it. im guessing you basically have to get the computer to recognize the ACSII code for each character, and develop a function that adds the numeric key value to each character. Problem is i have no clue how to do this.

    Any ideas/help?
  2. jcsd
  3. May 10, 2007 #2
    The ASCII code for upper case letteres are 65 to 90 and lower case letters are 97 to 122, numbers are from 48 to 57. So you should search through the text string by using a loop and look for ASCII codes lying in these intervals and take action depending on which interval you are in.
  4. May 10, 2007 #3
    You read the input in an array of characters right. So think of how you can cast each character to something that you can manipulate as a number and then cast back to a character.

    An elegant solution would be to make a function called for instance shift(char MyCharacter, int Offset) which returns the shifted character. Obviously you would want the function to rotate as well when applicable and also be able to use negative numbers.

    If you enjoy it you could for instance add a reverse (char [] MyString) function that reverses the string. Or a decode(char [] Mystring, char [][] TranslationsTable) function. The TranslationsTable would simply describe the mapping from one letter to another one.

    Have fun!
    Last edited: May 10, 2007
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook