0

I am having an issue with the following code, no matter what I do the second if statement throws a segmentation error. The two are very similar, just flipped, and I can't figure it out. I even isolated the code and hard-coded values to make it run and make sure I know exactly what code is causing the error, but I still can't figure out why.

Edit #2: It may have something to do with the use of CS50 library's "string" type, and if so I would need a workable alternative. But if that's the case I wonder why it works for the first example (the first for loop) and not the second.

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

int main(void)
{

    int i;
    string key = "B";
    string alphabet = "a";

    for (i = 0; i < strlen(key); i++)
    {
        if (islower(key[i]) && isupper(alphabet[i]))
        {
            alphabet[i] = tolower(alphabet[i]);

        }
    }

    for (i = 0; i < strlen(key); i++)
    {
        if (isupper(key[i]) && islower(alphabet[i]))
        {
            alphabet[i] = toupper(alphabet[i]);
        }
                printf("yeet");
    }
}

Edit #1: UPDATED WITH MRE. However, I'm concerned about someone else's ability to reproduce due to the fact that this includes Harvard's CS50 library, and I don't know what the accessibility of this library is outside of the CS50 course.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
UntoldDawn
  • 25
  • 4
  • May you add the declarations of `alphabet` and `key`? – Kfir Ventura Sep 14 '21 at 10:34
  • 1
    @KfirVentura Please do not ask for specific details, Instead ask for a [mre] ( `[mre]` for convenience.) – Yunnosch Sep 14 '21 at 10:35
  • 1
    One major problem with this code is the loop test `i < strlen(key)`. Even though `strlen(key)` is loop-invariant, you are evaluating it *every single time through the loop*. That means your loops run in O(n*n) time rather than O(n) as they should. Tip: If you were running this by hand, you wouldn't count the length of `key` every time through the loop, so your code shouldn't either. – Tom Karzes Sep 14 '21 at 10:37
  • 1) make sure `key` and `alphabet` have the same length 2) make sure `alphabet` is writable (no `char *alphabet = "The Quick Brown Fox";` ... use `char alphabet[] = "The Quick Brown Fox";` instead) – pmg Sep 14 '21 at 10:38
  • 1
    Regarding your bug, are you certain that `alphabet` is the same length (or longer) than `key`? If not, then you're indexing past the end and corrupting memory. – Tom Karzes Sep 14 '21 at 10:38
  • The shown code could be correct, in appropriate context (as hinted in comments). Since you see segfaults it probably is not, but the mistake is in the code you did not show. Please provide a [mre]. – Yunnosch Sep 14 '21 at 10:42
  • According to https://github.com/cs50/libcs50/blob/main/src/cs50.h#L51 `string` is a `typedef` for a `char*` - which means you are trying to modify a string literal, which isn't allowed – UnholySheep Sep 14 '21 at 15:03
  • Provided a minimum reproducible example. Based on the comments it seems likely that the problem lies with the declaration of type "string" which is provided by cs50 library. Does anybody have an alternative solution to using type "string"? – UntoldDawn Sep 14 '21 at 15:09

1 Answers1

0

first we need you to show us the entire code it's important because it might be the problem.

The problem might be because you're trying to modify a char* with a constant string in it have you declared you arrays like that ? :

char *key = "content A";
char *alphabet = "content B";

if yes your arrays are constant which means they cannot be modified you need to create a copy of them like so

char *key = "content A";
char *key_cpy = malloc(strlen(key) * sizeof(char));
strcpy(key_cpy, key);

and then you can modify key_cpy content because the content isn't a constant which means it can be modified

tell me if it worked !

Tsirsuna
  • 130
  • 2
  • 16
  • An easier way to create modifiable null-terminated strings would be simply `char key[] = "content A";` – UnholySheep Sep 14 '21 at 15:04
  • 1
    Also your `key_cpy` is missing space for the null-terminator, it's off by one – UnholySheep Sep 14 '21 at 15:05
  • Please see my updated post for more information and a minimum reproducible example. – UntoldDawn Sep 14 '21 at 15:10
  • 1
    @Sir Tristanus, You solved it! Thank you so much. I had been working my entire program around this issue, so I had to essentially rewrite the program, but all of my headaches were coming from this one problem which you solved for me. Now that I'm more familiar with these types and how they work, I shouldn't run into this kind of problem taking up so much of my time again. Thank you. – UntoldDawn Sep 14 '21 at 16:34