45hook

Untitled

Jul 26th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. // vigenere encryption
  2. //
  3. #include<stdio.h>
  4. //#include<cs50.h>
  5. #include<ctype.h>
  6. #include<stdlib.h>
  7. #include<string.h>
  8.  
  9. int main(int argc,char *argv[]){
  10. char *ptext; // for plaintext
  11. int n;
  12. int j=0;
  13. int moves=0;
  14.  
  15. // if key was not passed
  16. if(argc!=2){
  17. printf("improper no of arguments passed\n");
  18. exit(1);
  19. }
  20. // for wrong format error of key
  21. n=strlen(argv[1]);
  22. for(int i=0;i<n;i++){
  23. if(!isalpha(argv[1][i])){
  24. printf("wrong format of argument\n");
  25. exit(1);
  26. }
  27. }
  28.  
  29.  
  30. // converting Plain-text to cypher
  31. char *key;
  32. key=argv[1];
  33.  
  34. //getting the plaintext
  35. //ptext=GetString();
  36. scanf("%[^\n]s",ptext);
  37. int plen=strlen(ptext);
  38. //printf("key = %s %d = plen",key,plen);
  39. char ctext[plen];
  40.  
  41.  
  42. for (int i = 0; i < plen; ++i)
  43. {
  44.  
  45. if(j==n)
  46. j=0;
  47.  
  48. // key evaluation
  49.  
  50. // for only encrypting alphabetical characters
  51. if(isalpha(ptext[i])){
  52. //modifing key value
  53. if(islower(key[j]))
  54. moves = ( key[j] - 'a' );
  55. else
  56. moves = ( key[j] - 'A' );
  57.  
  58. //for lower case alhabets
  59. if (islower(ptext[i]))
  60. {
  61. //overflow
  62. if(ptext[i]+moves > 'z'){
  63. moves=(ptext[i]+moves)-'z';
  64. ctext[i] = ('a'+ moves-1);
  65. j++;
  66. }
  67. else{
  68. ctext[i] = (ptext[i]+ moves);
  69. j++;
  70. }
  71. }
  72. //for upper case alphabets
  73. else if (isupper(ptext[i]))
  74. {
  75. if(ptext[i]+moves > 'Z'){
  76. moves=(ptext[i]+moves)-'Z';
  77. ctext[i]=('A'+moves-1);
  78. j++;
  79. }
  80. else{
  81. ctext[i] = (ptext[i]+ moves);
  82. j++;
  83. }
  84. }
  85. }
  86. else{
  87. ctext[i]=ptext[i];
  88. }
  89. }
  90.  
  91. // printing cypher
  92. printf("%s",ctext);
  93. // ctext printing extra text on 'a' as key and 'a' as plaintext
  94.  
  95. return 0;
  96. }
Add Comment
Please, Sign In to add comment