Advertisement
Guest User

Vigenere

a guest
Oct 25th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<cs50.h>
  3. #include<stdlib.h>
  4. #include<ctype.h>
  5. #include<string.h>
  6.  
  7. int main(int argc, char* argv[])
  8. {
  9.  
  10. int i=0,j=0,n=0,k=0;
  11.  
  12. if(argc!=2)
  13.  
  14. {
  15. printf("Bad I/p\n"); // checking for valid argv[1]
  16. return 1;
  17. exit(0);
  18. }
  19.  
  20. char* key=argv[1];
  21.  
  22. for(i=0,n=strlen(key);i<n;i++) // To check wether each character of key is alphabet
  23.  
  24. {
  25.  
  26. if (isalpha(key[i]))
  27. {
  28.  
  29. printf("AFTER VALID\n");
  30.  
  31.  
  32. if (isupper(key[i]))
  33.  
  34. {
  35. key[i]=key[i]-65;
  36. }
  37.  
  38.  
  39. if (islower(key[i]))
  40.  
  41. {
  42. key[i]=key[i]-97;
  43. }
  44.  
  45. }
  46. else
  47.  
  48. printf("Enter valid key\n");
  49. exit(0);
  50. return 1;
  51. }
  52.  
  53.  
  54.  
  55. printf("B4 input\n"); // why isn't this statement getting executed ?
  56.  
  57. string msg=GetString(); // plaintext input from user
  58.  
  59. if (isalpha(msg[j]))
  60.  
  61. {
  62. for (int j=0,m=strlen(msg);j<m;j++)
  63.  
  64. {
  65.  
  66.  
  67. if (isupper(msg[j]))
  68.  
  69. {
  70. msg[j]=(((msg[j]-65)+key[k%n])%26)+65; // k%n = k% strlen(keyword) cipher formula for upper case
  71.  
  72. }
  73.  
  74. if (islower(msg[j]))
  75.  
  76. {
  77. msg[j]=(((msg[j]-97)+key[k%n])%26)+97; // 97 and 65 are added to convert back to ascii form
  78.  
  79. }
  80. else
  81.  
  82. printf("%c",msg[j]);
  83.  
  84. k++;
  85.  
  86. printf("%c*",msg[j]);
  87.  
  88. } // j-loop 2
  89.  
  90.  
  91. } //isalpha du
  92. else
  93. {
  94. printf("Enter valid key\n");
  95. exit(0);
  96. return 1;
  97.  
  98. }
  99.  
  100. } //main du bracket
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement