Ok, I've only changed simpleEncrypt, medium and ultra are still like when I started.
Header:
Code:
/*!
* Replaces each letter in the character array wih its next lexicographical
* letter.
* Sample: "Good" becomes "Hppe"
!*/
void simpleEncrypt(char *input);
/*!
* Add to each letter with its index in the given input.
* Sample: "Good" becomes "Gpqg"
*
!*/
char mediumEncrypt(char input[]);
/*!
* Add to each letter with the square of its index in the given
* input.
* Sample: "Good" becomes "Gpsm"
!*/
char ultraEncrypt(char input[]);
encrypt.c
Code:
#include"encrypt.h"
#include<stdio.h>
void simpleEncrypt(char *input)
{
int i, len = strlen(input);
//for loop goes through all the characters in input and adds 1 to the ascii value
//of each character
for(i = 0;i < len; i++)
{
input[i]++;
//if the character is now ascii value 91 then we must loop around to ascii
//value 65. In other words goes from Z to A.
if(input[i] == 91)
{
input[i] = 65;
}
//if the character is now ascii value 123 then we must loop around to ascii
//value 97. In other words goes from z to a.
else if(input[i] == 123)
{
input[i] = 97;
}
}
}
char mediumEncrypt(char input[])
{
int i, len = strlen(input);
//for loop goes through all the letters in the input
for(i = 0;i < len; i++)
{
//casting to the corresponding ascii value
input[i] = (int)input[i];
//incrementing the ascii value by its index in the sentence
input[i] = input[i] + i;
//since we are assuming sentences less than 20 characters, all we have to
//do is subtract the ascii value by 26 to assure we wrap around
if(input[i] >= 91)
{
input[i] = input[i]-26;
}
if(input[i] >= 123)
{
input[i] = input[i]-26;
}
//casting back to a character
input[i] = (char)input[i];
}
return *input;
}
char ultraEncrypt(char input[])
{
int i, len = strlen(input);
//for loop goes through all the characters in the input sentence
for(i = 0;i < len; i++)
{
input[i] = (int)input[i];
//this integer serves as a boolean flag, if it is 1 then the character is uppercase
int upper;
if(input[i] >= 65 && input[i] <= 90)
{
upper++;
}
//incrementing the ascii value by the square of its position in the sentence
input[i] = input[i] + (i*i);
//here we need a while loop to assure we wrap around to the correct value
if(upper == 1)
{
//the while loop decrements input[i] to its corresponding ascii value
while(input[i] > 90)
{
int count;
count = input[i] - 90;
input[i] = 65 + count;
}
}
else
{
//while loop to assign the correct ascii value if the character was lowercase
while(input[i] > 122)
{
int count;
count = input[i] - 122;
input[i] = 97 + count;
}
}
input[i] = (char)input[i];
}
return *input;
}
main
Code:
#include"encrypt.h"//make your function declaration in this .h file
#include<stdio.h>
int main()
{
char input[20], encrypt;
printf("Please enter a sentence to encrypt: ");
scanf("%s", &input);
printf("Please enter encryption level, 's' for simple, 'm' for medium, or 'u' for ultra:");
scanf("%c", &encrypt);
if(encrypt == 's')
{
printf("Original word is: ");
printf("%s\n", input);
printf("Encrypted word is: ");
simpleEncrypt(*input);
printf("%s",input);
}
else if(encrypt == 'm')
{
printf("Original word is: ");
printf("%s\n", input);
printf("Encrypted word is: ");
mediumEncrypt(*input);
printf("%s",input);
}
else
{
printf("Original word is: ");
printf("%s\n", input);
printf("Encrypted word is: ");
ultraEncrypt(*input);
printf("%s",input);
}
return 1;
}