Help with C Program for Implementing Cipher to String

  • Thread starter Thread starter SNOOTCHIEBOOCHEE
  • Start date Start date
  • Tags Tags
    Program
AI Thread Summary
The discussion revolves around creating a C program that implements a cipher to shift characters in a string based on a numeric key. The program should accept a numeric key and a string input, then output a modified string where each letter is shifted by the key value, while leaving numbers unchanged. Key points include the use of ASCII codes to identify character ranges for uppercase (65-90), lowercase (97-122), and numbers (48-57). A suggested approach involves reading the input as a character array and creating a function, such as shift(char MyCharacter, int Offset), to handle the character manipulation. This function should account for character rotation and support negative offsets. Additional features like reversing the string or implementing a decoding function with a translation table are also proposed for enhanced functionality.
SNOOTCHIEBOOCHEE
Messages
141
Reaction score
0
Ok I am 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

EFGHIJK

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. I am 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?
 
Technology news on Phys.org
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.
 
SNOOTCHIEBOOCHEE said:
Now i have a general idea of how to do it. I am 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.
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:
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Back
Top