I'm doing the first problem in pset2 and having issues with segmentation faults. Whenever I run the program and enter a string, the error occurs. Based on the lecture I'm assuming it has something to do with one of the arrays. Can anyone tell me why I'm getting the error (and also how to avoid it in the future)?

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

int main(void) {

    string name;
    string initials = "";
    char character;
    name = GetString();

    initials[0] = name[0];

    for(int i = 0; i <= strlen(name); i++) {

        int x = 1;
        character = name[i];
        if(character == ' ') {
            name[i] = initials[x];

    printf("%s", initials); 

You initialized the string initials with a string literal. String literals are constant — once they are created, you cannot change their values. To fix that, you should use a char array instead.

You must log in to answer this question.

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