0
#include<stdio.h>
#include<cs50.h>
#include<string.h>
#include<ctype.h>

char vigenere(char p, int b, string c);
int  main(int argc, string argv[])
{
   int k = 0;
   do
   {
      if(argc == 2)
      {      
         string s = GetString();
         for (int i = 0; i < strlen(s); i++)
         {
            char p = s[i];
            if (!isalpha(p))
            {
               printf("%c", p);
            }
            else
            {
                string q = argv[1];
                char g = vigenere(p,k,q);
                printf("%c", g);

                if (k < strlen(argv[1]))
                {
                    k++;
                }
                else
                  k = 0;
           }
       }
   }
   else
   {
      printf("enter a valid key! \n");
      return 1;    
   }
}while(!isalpha(argv[1]));

printf("\n");
}


char vigenere (char p, int b, string c)
{
        int a;
        char l = c[b];
        if (islower(l))
         {
               int  j = (int) l;
               if (islower(p))
               {
                  a = (int) p;
                  a = a - 97;
                  j = j - 97;
                  a = (a + j) % 26;
                  a = a + 97;
               }
               else 
               {
                a = (int) p;
                a = a - 65;
                j = j - 97;
                a = (a + j) % 26;
                a = a + 65;
               }
         }
        else
         {
                  int j = (int) l;
                  if (islower(p))
               {
                  a = (int) p;
                  a = a - 97;
                  j = j - 65;
                  a = (a + j) % 26;
                  a = a + 97;
               }
               else 
               {
                    a = (int) p;
                    a = a - 65;
                    j = j - 65;
                    a = (a + j) % 26;
                    a = a + 65;
                }

         }
    char t = (char) a;
    return(t);          
}

This code throws segmentation fault!, which I couldn't sort out. Help me in this issue! Thank you!

0

1 Answer 1

1

isalpha() takes an argument of character type not on array of characters. What you are doing is passing a string, which is an Array of characters. isalpha() will return false because it gets the Memory address of the first element of the argv[] which is an integer value always.

For example:

argv[1] = "bacon"

You can Access each element of above string by

argv[1][0] has 'b'
argv[1][1] has 'a'
argv[1][2] has 'c'
.
.
. 

You must log in to answer this question.

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