0

I've been trying crack problem in pset2 and most of the times it doesn't print something or gives segmentation fault

I've tried to debug it using debug50 but that takes a great deal of time and even after 30 seconds or 1 minute does not show anything, it's definitely because of my algorithm but I am very much confused with the segmentation fault. Please anyone help me.

    #define _XOPEN_SOURCE
    #include<cs50.h>
    #include<stdio.h>
    #include<string.h>
    #include<unistd.h>

    int main(int argc, string argv[])
{   int i=0; int c=0; char a[52];string d="",f="",g="",s="";
  if(argc==2)
  {
    for(i=0;i<26;i++)
    {
        a[i]=(char)(i+65);
    }
    for(i=26;i<52;i++)
    {
        a[i]=(char)(i+97-26);
    }

    for(i=0;i<52;i++)
    {
        s=s+a[i];
        if(argv[1]==(crypt(s,"50")))
        {
            c++;
            break;
        }

        else
        {
            for(int j=0;j<52;j++)
            {
                d=a[j]+s;
                if(argv[1]==(crypt(d,"50")))
                {
                    c++;
                    break;
                }
                else
                {
                    for(int k=0;k<52;k++)
                    {
                        f=a[k]+d;
                        if(argv[1]==(crypt(f,"50")))
                        {
                            c++;
                            break;
                        }
                        else
                        {
                            for(int l=0;l<52;l++)
                            {
                                g=a[l]+f;
                                if(argv[1]==(crypt(g,"50")))
                                {
                                    c++;
                                    break;
                                }
                                g="";
                            }
                        }
                        f="";
                    }
                }
                d="";
            }
        }
        s="";

    }
    if(c==1)
    printf("%s\n",s);
    return 0;
}
else
{
    printf("Usage: ./crypt hashed key");
    retutn 1;

}

}
0

string d="",f="",g="",s="";

This declares 4 immutable strings, so you will segfault if you try to change them. If you want those to be changeable, you need to either use malloc to get the memory for them, or declare them as char arrays that are large enough to hold the chars you need.

2
  • If I declare character arrays then I will have to use another loops to check each element of the array. Is there any way to check the whole array with the input. Sep 29 '17 at 9:39
  • Remember that a string is simply a char array that ends in the NUL char. so "abc" is 'a' 'b' 'c' '\0'
    – curiouskiwi
    Sep 29 '17 at 23:57

You must log in to answer this question.

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