Using C11, implement the function to_upper (void *to_upper(const char *str), Returns a…
- User
Using C11, implement the function to_upper (void *to_upper(const char *str), Returns a copy of the string (str) converted to uppercase. In case of an error, returns NULL). It is forbidden to use string.h and ctype.h
Assistant
Here is the implementation of to_upper() in C11 without using string.h and ctype.h:
cvoid *to_upper(const char *str) { if (!str) return NULL; size_t len = 0; while (str[len]) len++; char *upper = malloc(len + 1); if (!upper) return NULL; for (size_t i = 0; i < len; i++) { if (str[i] >= 'a' && str[i] <= 'z') { upper[i] = str[i] - 32; } else { upper[i] = str[i]; } } upper[len] = '\0'; return upper; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.