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



 int main(int argc,string argv[])
 {  
     int i,n,q,r;


    if  (argc==2)

    {
       string p = argv[1];
       int k = atoi(p);
       printf("plaintext:  ");
       string s = get_string ();
              n = strlen(s);
       printf("%i,%i,%s\n",k,n,s);
         char c[n];
         for (i=0; i<n; i++)

         {  if (isalpha('s[i]'))
              { if (islower('s[i]'))
                {  q = s[i];
                   r = (((q + k-97)%26)+97);
                   c[i]=r;
                  printf("%i,%c,%c\n",r,s[i],c[i]);
                }  

              else if (isupper('s[i]'))
              {    q = s[i];
                   r = (((q + k-65)%26)+65);
                   c[i] = r;
                  printf("%i,%c,%c\n",r,s[i],c[i]);
              } }   



            else
            {   c[i] = s[i];
               printf("%c\n",c[i]);    
            } 

         }       
   }

    else
    {   printf ("Usage: ./caeser k\n");

    }


   return 0;g50


}

Program compiles. On execution with ./caeser 2, I am getting this error: plaintext: Hello! 2,6,Hello! Segmentation fault

0

This is a syntax issue. Look at the following line:

if (isalpha('s[i]'))

It looks like you wanted to test the chars in the s[] array. Unfortunately, that's not what's going on. Single quotes are used to identify a single char. (Double quotes are used to identify a literal string.) So, instead of passing the char array s[] to isalpha, it is probably passing the literal string in single quotes. (Not entirely positive what's being passed because of single quotes, not double quotes.)

If the single quotes are removed, it will actually pass the char located at s[i] and not seg fault.

This problem repeats several times throughout the code, so be prepared to fix them all. At least, it's consistent! ;-)

That fixes the seg fault issue, but there may or may not be more. If there are other issues, please open a new question.

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

1
  • You are a Genius! Thanks so much.
    – gvr
    Feb 14 '18 at 6:11

You must log in to answer this question.

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