I am also trying to learn C programming, but so far i have not been very successful. The main reason is that i find the assignments in many C programming books very boring, and therefore fail to solve them. The projekt posted in this thread seemed very interesting, and i could not resist to try solving it. The program is properly not very well written, but i am quite happy with it. I did not create a separate function for input as i could not figure out to return a array pointer to main. The message can maximum contain 100 characters. I had alot of fun with solving the shift cyclic.
decrypt_message.c
Code:
#include <stdio.h>
#define MESSAGE_MAX_SIZE 100
void caesar_shift(char encrypted_message[MESSAGE_MAX_SIZE], int character_counter)
{
int result = 0;
int result_counter = 0;
int shift = encrypted_message[0] - 'a';
for(result_counter = 0; result_counter < character_counter; result_counter++)
{
result = encrypted_message[result_counter] - shift;
if(result < 'a')
{
result = ('z' + 1) - ('a' - result);
}
printf("%c", result);
}
printf("\n");
}
main()
{
char encrypted_message[MESSAGE_MAX_SIZE];
int character = 0;
int buffer_overflow_check = 0;
int character_counter = 0;
while(character != '\n' && buffer_overflow_check < MESSAGE_MAX_SIZE)
{
character = getchar();
encrypted_message[buffer_overflow_check++] = character;
character_counter++;
}
caesar_shift(encrypted_message, character_counter);
}
The program do have a small flaw, as an ekstra character is appended to the message:
Sample run 1:
Code:
coggvogcvvgpwpfgtdtkfig
ameetmeattenunderbridge"
Does anyone know why this is happening?
If anyone has links to C learning projekts like the one in this thread, please post them.