0
:) vigenere.c exists
:) vigenere.c compiles
:) encrypts "a" as "a" using "a" as keyword
:) encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword
:) encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword
:) encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword
:) handles lack of argv[1]
:) handles argc > 2
:( rejects "Hax0r2" as keyword
    \ expected output, not a prompt for input

So please anybody suggest me how achieve last condition.

for(int i = 0, len = strlen(argv[1]) ; i <= len ; i++) 
{ 
     if(!isalpha(argv[i])) 
     { 
          printf("only alpha characters allowed!!\n"); 
          return 1; 
     } 
}

If I use this code I'm getting segmentation fault.

2
  • I think I may have had the same problem at one point. Try taking out the printf statement "only alpha characters allowed!!" and then recompiling. Oct 26 '14 at 13:21
  • Ok so I think I see the problem here. You have a very minor error with the identity in the "for command" which is causing the program to go one iteration too far. Remember that i starts at 0 so that if the strlen = 4, i can be 0, 1, 2, or 3. Oct 26 '14 at 15:09
3

As far as I can see, the problem lies in if(!isalpha(argv[i])). argv[] is a string of strings. It seems that you wish to access individual elements of second element in argv. If that's the case, then you should probably replace that with if(!isalpha(argv[1][i])).

Two more points to be mentioned :

  1. isalpha() takes a single character as a parameter and not a string. Unfortunately, argv[i] stands for a string.
  2. You receive segmentation fault because you access argv[i] for those values of i for which argv[] is already NULL(or better to say not even allocated memory). Remember how many command-line arguments you are passing, and then match them with the maximum value of i as an index with argv[]. Do those values exist in argv? I think that much is enough to point why the thing behaved in the wrong way.

Good Luck.

2
  • Yeah... such a big silly mistake:( Thank You so much...
    – Latha S S
    Oct 26 '14 at 15:34
  • My pleasure. Expect you to do the same, help others if you can solve their problems.
    – sinister
    Oct 26 '14 at 15:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .