[ back to toc ]

help

Date: 2002/03/16 12:31

Q:
hi ,
i have a problem with my code ,well it doesnt seem like just one
problem!!!
it is my first time to work with 2-D arrays so i dont know how to work it
out!
i well tell you what it should do ,and plz tell me why it is not doing it!
///////////////////////////////////////////////
the question it:

suppose that you want to send a password(of max 16 char) via an unsecured
connection over the internet, a good idia is to encrypt the password and
let the recipient decrypt it,
write a function that will take a string (terminated by'\0' and at most 16
characters) and return its encrypted image via the same parameter:

void secure(char password)

the encrypted image is formed using this method:
1- store the characters in a 4*4 arra,row by row .tf there are empty
locations at the end of the array,fill them with'$'.
2-collect the characters from the 4*4 array ,column by column ..
(so when you call that function twice you will get the right password)
//////////////////////////////////////////////////
i wrote the code this way:
#include<stdio.h>

void secure (char password[])
{
int B[16][16];
int i,j;
for(i=0;i<16;i++)
for(j=0;j<16;j++)
B[i][j]=password[i];
for(i=0;i<16;i++)
for(j=0;j<16;j++)
password[i]=B[j][i];

}

int main()
{
int i,j,save,$;
char password[17];
printf("Enter the password,at most 16 charecters");
for(i=0;i<17;i++)
if(password[i]=='\0')
save=i;
for(j=i;j<17;j++)
password[i]='$';
scanf("%s",password);
secure(password);
printf("the encrypted image is %s",password);
secure(password);
printf("the original password is %s",password);
}

////////////////////////////////////////////////
plz help it is urgent..thanks so much,,ajee

A:
#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <stdlib.h>

char passw[17];

void secure(char *p){
char carr[4][4];
int i,j;

for( i=0 ; i < 4 ; i ++ )
for( j=0 ; j < 4 ; j++ )
carr[i][j] = p[i*4+j];

for( i=0 ; i < 4 ; i ++ )
for( j=0 ; j < 4 ; j++ )
p[i+j*4] = carr[i][j];
}

#define PASSW "birkamirka"
main(){
memset(passw,'$',17);
memcpy(passw,PASSW,strlen(PASSW));
printf("%s\n",passw);
secure(passw);
printf("%s\n",passw);
secure(passw);
printf("%s\n",passw);
exit(0);
}

[ back to toc ]