Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // vigenere encryption
- //
- #include<stdio.h>
- //#include<cs50.h>
- #include<ctype.h>
- #include<stdlib.h>
- #include<string.h>
- int main(int argc,char *argv[]){
- char *ptext; // for plaintext
- int n;
- int j=0;
- int moves=0;
- // if key was not passed
- if(argc!=2){
- printf("improper no of arguments passed\n");
- exit(1);
- }
- // for wrong format error of key
- n=strlen(argv[1]);
- for(int i=0;i<n;i++){
- if(!isalpha(argv[1][i])){
- printf("wrong format of argument\n");
- exit(1);
- }
- }
- // converting Plain-text to cypher
- char *key;
- key=argv[1];
- //getting the plaintext
- //ptext=GetString();
- scanf("%[^\n]s",ptext);
- int plen=strlen(ptext);
- //printf("key = %s %d = plen",key,plen);
- char ctext[plen];
- for (int i = 0; i < plen; ++i)
- {
- if(j==n)
- j=0;
- // key evaluation
- // for only encrypting alphabetical characters
- if(isalpha(ptext[i])){
- //modifing key value
- if(islower(key[j]))
- moves = ( key[j] - 'a' );
- else
- moves = ( key[j] - 'A' );
- //for lower case alhabets
- if (islower(ptext[i]))
- {
- //overflow
- if(ptext[i]+moves > 'z'){
- moves=(ptext[i]+moves)-'z';
- ctext[i] = ('a'+ moves-1);
- j++;
- }
- else{
- ctext[i] = (ptext[i]+ moves);
- j++;
- }
- }
- //for upper case alphabets
- else if (isupper(ptext[i]))
- {
- if(ptext[i]+moves > 'Z'){
- moves=(ptext[i]+moves)-'Z';
- ctext[i]=('A'+moves-1);
- j++;
- }
- else{
- ctext[i] = (ptext[i]+ moves);
- j++;
- }
- }
- }
- else{
- ctext[i]=ptext[i];
- }
- }
- // printing cypher
- printf("%s",ctext);
- // ctext printing extra text on 'a' as key and 'a' as plaintext
- return 0;
- }
Add Comment
Please, Sign In to add comment