Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

An question about cesar's encryption

Name: Anonymous 2013-10-24 16:40

I'm doing some tests about encryption and I'm trying this one.
It's a small code and I'm having a little issue using the ASCII table.
I want to don't appear this strange chars.
http://puu.sh/4Yx30.png
What should i change?
Here's the code.

########################################################
#include <stdio.h>
#include <string.h>
#define TM 127
int main ()
{
char user [TM], userc[TM];
char pass [TM], passc[TM];
int key,len,tmax,ini,i;
tmax=125;
ini=40;

printf("Type a key: ");
scanf("%d", &key);

printf ("Type the user: \n");
scanf ("%s", &user);
len=strlen(user);

if (len<100){
    for (i=0;i<len;i++){
        if (user[i]+key>tmax){
        userc[i]=user[i]+key-ini;
        }
        else{
        userc[i]=user[i]+key;
        }
    }
//encrypted
printf("user: %s\n",userc);
}
system ("PAUSE");
return 0;
}

######################################

Name: Anonymous 2013-10-24 22:34


if (len<100){
    for (i=0;i<len;i++){
        if (user[i]+key>tmax){
        userc[i]=user[i]+key-ini;
        }
        else{
        userc[i]=user[i]+key;
        }
    }
//encrypted
printf("user: %s\n",userc);
}

Why "<100"?
The problem is twofold:
1. You do not terminate the "userc" string (hint0: "asd" is the string {'a', 's', 'd', '\0'}) (hint1: '\0' terminates a string, note the single quotes) (hint2: i refers to the index just after the end of the string when the loop is complete)
2. You have that shitty pointless if-condition


Also:
system ("PAUSE");
Please don't do that, it's ugly to spawn a cmd.exe subprocess just to wait for a single keypress.

Name: Anonymous 2013-10-24 22:35

And please remember to correctly indent your code. Nothing turns off another programmer like poor indentation.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List