0

For crack assignment I made a function, capsPattern, that takes 2 char[] arguments: a word, and a binary-numeric pattern that represents which letters are to be capitalized. It works nicely when I'm passing it an index from my words array but it does not work when I pass it a literal. So, capsPattern(wordsArray[5], "1011") works, whereas capsPattern("test", "1011") throws segmentation error. I'm guessing this might be because string literal "test" isn't automatically null-terminated? If so, how would I fix that, assuming I don't know in advance the length of the word argument?

Here's my code:

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

string capsPattern (char word[], char pattern[]);

int main(int argc, string argv[])
{
    //initialize char wordsArray[][]
    string unhashed = capsPattern(wordsArray[i], "1011"); //this works
    string unhashed2 = capsPattern("test", "1011"); //this doesn't
}

string capsPattern (char word[], char pattern[])
{
    string _word = word;
    for (int i = 0; (pattern[i] != '\0') && (_word[i] != '\0'); i++)
    {
        if (pattern[i] == '0') 
        {
            if ((_word[i] >= 'A') && (_word[i] <= 'Z'))
            {
                _word[i] += 32;
            }
        }
        else if (pattern[i] == '1') 
        {
            if ((_word[i] >= 'a') && (_word[i] <= 'z'))
            {
                _word[i] -= 32;
            }
        }
    }
    return _word;
}
  • I fear I cannot see the root cause, but what I do see right away is that string _word = word; won't work. You'll hear more about this in week4, but it comes down to that they point to the same place in memory. AKA You need to use strcpy() or a for loop, to not have it affect the original word ;) Hope this helps a bit, and sorry about not being able to find the root cause – HowToCompute Feb 21 '17 at 8:45
  • I see, thanks! I just re-watched Array short from Week 2 and realized what I was trying to do here isn't legal. Oh well. – ArtemPetrov Feb 21 '17 at 17:29
2

There's a couple of things that I can see won't work:

string unhashed = capsPattern(wordsArray[i], "1011");

I can't see how this will work as you're expecting, as wordsArray[i] is just character i from wordsArray i.e. you're only sending a single character to the function capsPattern() when it is expecting a char array. Just changing this to wordsArray should be fine though.

string _word = word;

Again, this will not work as you are expecting. It does not create a copy, it creates what's called a pointer to the original string. In short, every time you change _word, you are also changing word. In order to copy a string properly, have a look at the function strcpy().

string unhashed2 = capsPattern("test", "1011");

This is linked to the last point. The string "test" is actually a char * (or string as it is referred to in the cs50 library) which is stored in read-only memory, so when you try to edit _word in your function, you are actually trying to edit word which has been passed a read-only char *. Hence this is not allowed and will throw a segmentation fault.

| improve this answer | |
  • Thanks you, I'm going to try this! wordsArray[i] passes the full word rather than character; wordsArray is a 2D array, like that: char wordsArray [][5] = {"uno", "dos", "tres", "etc"}; – ArtemPetrov Feb 21 '17 at 14:55
  • Ok, that makes sense then. Sometimes it's hard to see without context! – Steve Bunting Feb 21 '17 at 15:00
  • Ok so, I now understand the error in trying to do string _word = word, since arrays can't be copied that way in C. The way I fixed my function was by creating a static char[] in the function, using strcpy to copy the values of "word" into it, and then returning it after capitalizing appropriate letters. There's probably a better way but I haven't gotten to more advanced stuff like malloc and calloc etc, so this will do for now. Thanks for the tips. – ArtemPetrov Feb 21 '17 at 18:32
  • Sounds good to me, glad you got it sorted. And can't see any reason to malloc any memory myself! – Steve Bunting Feb 21 '17 at 19:33

You must log in to answer this question.

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