1

when i give an input of ./vigenere abc it doesn't accept and its printing key must be alphabetic.where have i gone wrong.would anyone explain in brief? and how do i correct it? Please help me out.!

 int main(int argc,string argv[])
 {
  if(argc!=2)
   {
    printf("Usage: ./vigenere k\n");
    return 1;
   }
 for(int i=0,l=strlen(argv[1]);i<l;i++)
 {
  string k=argv[i];
  if(!isalpha(k[i]))
   {
    printf("key must me alphabetical\n");
    return 1;
   }}

[EDIT: unrelated code removed.]

1

Look at where and how k is declared. During each pass through the for loop, k is declared as starting at argv[i]. That means that on the first pass, k=argv[0], the name of the program. This sneaks past, as does the next pass. On the third pass, string k=argv[2], which doesn't exist, so it fails.

Remember, argv[x] is a string in the argv array. argv[1][x] are letters in the second string in the array, i.e., if the string is "abc" then argv[1][1] == 'b'

k should be declared once, before the loop starts, and then the loop can step through the string.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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