haha yeah i fixed it. Ive been trying to reset the shift value for every word entered. This is what i have so far but it isnt working.
Code:
#include <stdio.h>
#include <string.h>
// Declare the function
char ShiftCharacter(char unencrypt,int shift_value,int offset);
int main(int argc, char* argv[])
{
char buff[1024];
int shift_value;
int encrypt_text_length;
int i;
int x;
int y = 0;
// Say hello
puts("**initializing.....**\n\n**linking..........**\n\n");
puts("Welcome agent Brown\n\n");
puts("You have accessed the Caesar Cipher program\n\n");
puts("Please enter the text you wish to encrypt: ");
// Get input from the user
// It is possible that the user may exceed our allocated space.
// We could address that but it would cloud the issue. TODO: for later
fgets(buff, sizeof buff, stdin);
puts("\nEnter your encryption shift value (anything from +-1 to 25): ");
scanf("%d",&shift_value);
// We now have a string and a shift value
// TODO: Check for empty string and out of range value
// Determine how long the string is
encrypt_text_length = strlen(buff);
// Encrypted the text
for(i = 0; i < encrypt_text_length; i++)
{
buff[ i] = ShiftCharacter(buff[ i],shift_value,i);
}
// Encrypted the text
for(x = 0; x < encrypt_text_length; x++)
{
// if the current character is a space reset the offset to zero
// otherwise add one to the offset
if(buff[i] == ' ')
{
y = 0;
}
else
{
y++;
}
buff[i] = ShiftCharacter(buff[i],shift_value,i);
}
// Display the encrypted text for the user
printf("\nYour encrypted text is: %s\n",buff);
return 0; /* indicate successful completeion */
}
// This function will shift the character by the determined amount
// For our purpose we are only concerned with the letters a - z
char ShiftCharacter(char unencrypt,int shift_value,int offset)
{
int alphanumber; // The character as a number ( A = 1, B = 2, Z = 26 etc.)
char encrypted_char = unencrypt;
// Capital A - Z
if(unencrypt >= 'A' && unencrypt <= 'Z')
{
alphanumber = unencrypt - 'A';
alphanumber += shift_value + offset;
alphanumber = (alphanumber % 26);
encrypted_char = (char)(alphanumber + 'A');
}
// Lowercase a - z
else if(unencrypt >= 'a' && unencrypt <= 'z')
{
alphanumber = unencrypt - 'a';
alphanumber += shift_value + offset;
alphanumber = (alphanumber % 26);
encrypted_char = (char)(alphanumber + 'a');
}
return encrypted_char;
}