Hey guys,
I'm working with strchr
and, for some reason, am getting a segfault. My program takes a string, iterates through the characters of that string, and adds a score to the "game" based on what letter the current character is. That sounds complicated, but my code will explain it:
// initialize alphabet and corresponding scores as arrays
char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int scores[] = {1, 4, 4, 2, 1, 4, 3, 3, 1, 10, 5, 2, 4, 2, 1, 4, 10, 1, 1, 1, 2, 5, 4, 8, 3, 10};
char word[] = "hello";
// iterate through word
for (int i = 0; i < strlen(word); i++)
{
// get current character in alphabet, update
// score based on corresponding letter score
char* c = strchr(alphabet, word[i]);
while (c != NULL)
{
int index = c - word;
score += scores[index];
}
}
This gives a segfault. What am I doing wrong? Thanks in advance!