I was doing this Highest Scoring Word kata on codewars which is to find the highest scoring word out of a string that is seperated by spaces and I kept getting the warning in the title even though I passed all the tests. Why does this happen?
I wrote in C and this is my code
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
** @param str: a C-string containing only lowercase letters and spaces (' ')
** @return: a C-string allocated on the heap containing the highest scoring word of str
*/
int calculateScore(char* word) {
int score = 0;
int length = strlen(word);
for (int i = 0; i < length; i++) {
char ch = *(word + i);
score += ch;
}
return score;
}
char *highestScoringWord(const char *str)
{
char *word = malloc(sizeof(str)/sizeof(char));
char *token = malloc(sizeof(str)/sizeof(char));
int score = 0;
char *rest = malloc(sizeof(str)/sizeof(char));
strcpy(rest, str);
token = strtok_r(rest, " ", &rest);
while (token) {
int newscore = calculateScore (token);
if (score < newscore) {
score = newscore;
word = token;
}
token = strtok_r(rest, " ", &rest);
}
return word;
}