#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int k,j,i,len,cnt,flag=0;
char message[200],key[20];
printf("Enter the Secret Key: ");
fflush(stdin);
gets(key);
//check for valid key
for(j=0;key[j]!=0;j++)
{
if((key[j]>=97 && key[j]<=122))
key[j]=key[j]-32;
else if((key[j]>=65 && key[j]<=90))
{}
else
{
printf("Keyword must only contain letters A-Z and a-z");
return 1;
}
}
len=strlen(key);
printf("Enter Secret Message Here: ");
fflush(stdin);
gets(message);
cnt = 0;
for(i = 0;i < strlen(message);i++)
{
flag=0;
k=key[cnt]-65;
if( (message[i]>=65 && message[i]<=90) )
{
for(j=1;j<=k;j++)
{
if(message[i]>=65 && message[i]<=90)
{
if(message[i]!=65)
message[i]-=1;
else
message[i]='Z';
}
else
message[i]='Z'-1;
}
flag=1;
}
else if(message[i]>=97 && message[i]<=122)
{
for(j=1;j<=k;j++)
{
if( (message[i]>=97 && message[i]<=122) )
{
if(message[i]!=97)
message[i]-=1;
else
message[i]='z';
}
else
message[i]='z'-1;
}
flag=1;
}
if(flag==1)
{
if(cnt==len-1)
cnt=0;
else
cnt++;
}
}
printf("Original Message: %s\n",message);
return 0;
}
#include<stdlib.h>
#include<string.h>
int main()
{
int k,j,i,len,cnt,flag=0;
char message[200],key[20];
printf("Enter the Secret Key: ");
fflush(stdin);
gets(key);
//check for valid key
for(j=0;key[j]!=0;j++)
{
if((key[j]>=97 && key[j]<=122))
key[j]=key[j]-32;
else if((key[j]>=65 && key[j]<=90))
{}
else
{
printf("Keyword must only contain letters A-Z and a-z");
return 1;
}
}
len=strlen(key);
printf("Enter Secret Message Here: ");
fflush(stdin);
gets(message);
cnt = 0;
for(i = 0;i < strlen(message);i++)
{
flag=0;
k=key[cnt]-65;
if( (message[i]>=65 && message[i]<=90) )
{
for(j=1;j<=k;j++)
{
if(message[i]>=65 && message[i]<=90)
{
if(message[i]!=65)
message[i]-=1;
else
message[i]='Z';
}
else
message[i]='Z'-1;
}
flag=1;
}
else if(message[i]>=97 && message[i]<=122)
{
for(j=1;j<=k;j++)
{
if( (message[i]>=97 && message[i]<=122) )
{
if(message[i]!=97)
message[i]-=1;
else
message[i]='z';
}
else
message[i]='z'-1;
}
flag=1;
}
if(flag==1)
{
if(cnt==len-1)
cnt=0;
else
cnt++;
}
}
printf("Original Message: %s\n",message);
return 0;
}
No comments:
Post a Comment