PDA

View Full Version : Halp with C code assignment!



jwstohr
Jan 26, 2009, 07:27 PM
So I have not programmed in a couple years and last week my professor gave the class an assignment that is kinda tricky. I got most of the program to do what it is supposed to do, but there are a couple problems.

First of a short summary of the assignment:
Problem: Simple encryption/decryption of text will be investigated in this assignment.

Background: Alphabet rotation
• Rotate characters within the alphabet sequence
o For example, A rotated forward 4 is E
o For example, b rotated backward 2 is z
• Applied only to letters. All other characters remain unchanged
• Uppercase remains upper, lowercase remains lower

Encryption algorithm v0:
• +1, -2, +3, -4, +1, -2, +3, -4, … repeating sequence of rotates for the entire string
• Non-letters still take up the space in the sequence even though the rotation is not applied
• For example:
o My pc!  Nw ld!


Basically, the user enters a string and it is then encrypted to the above algorithm. Encryption is not applied to the non letters, but they still take up a place in the algorithm.

My Problem:
I got the encryption to work unless, for example, the letter 'a' were to fall in the -2 slot. I don't know how to make it roll back and read 'y'. Also when I tried the "My pc!" example the '!' does not show up, everything else worked perfect. Hopefully it is just some simple mistake.

After typing all this I found out VMware wont let me copy my code from my virtual box, I will reply to this with the code I have.

Edit:
Some reason the ".h" after stdio and string wont show up. They are in my code though. Actually alot of the code is broken after pasting it in here. Even when using the code tags. :S Fixed it I think, had to un-check some boxes that were conflicting with the post. The code should be correct now.

jwstohr
Jan 26, 2009, 07:29 PM
#include <stdio.h>
#include <string.h>

int main()
{
char orig[256];
char hide[256];
char str[256];
char c;
int len, i, n;

printf("Enter String: ");

fgets( orig, 255, stdin );

printf("Unencrypted String is: %s\n", orig);

len = strlen(orig);
len--;
orig[len] = '\0';
printf("String length: %d\n", len);
n=0;

printf("Encrypted String: \n");
for (i=0; i < len; ++i)
{
n = n + 1;
if (n > 4)
n = 1;

if ((orig[i] < 'a' && orig[i] > 'z') || (orig[i] < 'A' && orig[i] > 'Z'))
hide[i] = orig[i];
else if (orig[i] == ' ')
hide[i] = ' ';
else if (n == 1)
hide[i] = orig[i]+1;
else if (n == 2)
hide[i] = orig[i]-2;
else if (n == 3)
hide[i] = orig[i]+3;
else if (n == 4)
hide[i] = orig[i]-4;


printf("%c", hide[i]);




}
printf("\n");



return 0;
}

Drunken F00l
Jan 27, 2009, 04:09 AM
off the top of my head



#include <stdio.h>
#include <ctype.h>
#include <string.h>

char rotate_char(char c, int r)
{
int ret;

// deal with everything in upper case to make life easier
// this gets put back to the correct case later
ret = toupper(c);

// check for an invalid character
if(ret < 'A' || ret > 'Z')
{
// just return that char
return c;
}

ret += r;

// put the character back in range
while(ret < 'A')
ret += 26;
while(ret > 'Z')
ret -= 26;

// put the character in the correct case
if(islower(c))
ret = tolower(ret);

return ret;
}

int main(int argc, char *argv[])
{
char plaintext[256];
char ciphertext[256];
int len;
int rot = 0;
int i;

// get input from user
fgets(plaintext, sizeof(plaintext), stdin);
plaintext[sizeof(plaintext)-1] = '\0';
len = strlen(plaintext);

// encrypt
for(i = 0; i < len; i ++)
{
rot++;

// maximum rotation is 4 and then it repeats
if(rot > 4)
rot = 1;

// even rot is negative, odd rot is positive
// this gives the sequence +1, -2, +3, -4
if(rot % 2 == 0)
ciphertext[i] = rotate_char(plaintext[i], -rot);
else
ciphertext[i] = rotate_char(plaintext[i], rot);

}
ciphertext[len] = '\0';

printf("%s\n", ciphertext);
return 0;
}


I didn't test or compile this, but it should work.

You should put code in code tags, btw. Formatting is preserved that way.

Drunken F00l
Jan 27, 2009, 04:28 AM
Also, if you'd rather fix your code then rewrite something similar to mine, check your logic here.



if ((orig[i] < 'a' && orig[i] > 'z') || (orig[i] < 'A' && orig[i] > 'Z'))
hide[i] = orig[i];
else if (orig[i] == ' ')
hide[i] = ' ';
else if (n == 1)
hide[i] = orig[i]+1;
else if (n == 2)
hide[i] = orig[i]-2;
else if (n == 3)
hide[i] = orig[i]+3;
else if (n == 4)
hide[i] = orig[i]-4;


It doesn't make much sense to me and hurts my head. Consider making a function to rotate characters like I did.

jwstohr
Jan 27, 2009, 08:44 AM
Awesome DF, Thank you!

Username
Feb 02, 2009, 12:07 PM
I'd like to say ty also! This is one of the hawtest threads i've read! ;p