Every failing sample below is the real generated C, with the compiler or test error that rejected it. Trial 0 runs at temperature 0, trials 1 and 2 at 0.7 — so a task passing 1/3 is a sampling-luck pass, not a reliable one. Models are ordered best first. Other technologies: C · Python · Bash · research · how to reproduce.
Setup and model serving are on the models index — including a script-free version using nothing but curl and a compiler; with a server running, this is the exact command that produced the samples below:
# C suite (16 easy + 3 hard tasks, 3 trials each; trial 0 at temp 0, then 0.7)
python scripts/eval_code.py --target <model> --set all --trials 3 \
--dump-failures results/failures
# brutal set: 6 tasks where the textbook answer is wrong
python scripts/eval_code.py --target <model> --set brutal --trials 3 \
--dump-failures results/failures --json > results/<model>-brutal-c.json
# grading is deterministic: cc -std=c11 -O2 -Wall must compile it, then hidden
# asserts run. A referee re-graded every failure by hand.
Served via llama.cpp CUDA · — tok/s · — GB RAM · coding total 117/126
No failures.
No failures.
Served via llama.cpp CUDA · — tok/s · — GB RAM · coding total 111/126
Implement in C11: `uint32_t bswap32(uint32_t x)`. Returns x with its four bytes reversed (endian swap). Include <stdint.h> yourself. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdint.h>
static inline uint32_t bswap32(uint32_t x)
{
return ((x & 0x000000FFu) << 24) |
((x & 0x0000FF00u) << 8) |
((x & 0x00FF0000u) >> 8) |
((x & 0xFF000000u) >> 24);
}#include <stdint.h>
static inline uint32_t bswap32(uint32_t x)
{
return ((x & 0x000000FFu) << 24) |
((x & 0x0000FF00u) << 8) |
((x & 0x00FF0000u) >> 8) |
((x & 0xFF000000u) >> 24);
}#include <stdint.h>
static inline uint32_t bswap32(uint32_t x)
{
return ((x & 0x000000FFu) << 24) |
((x & 0x0000FF00u) << 8) |
((x & 0x00FF0000u) >> 8) |
((x & 0xFF000000u) >> 24);
}Implement in C11: `int glob_match(const char *pat, const char *str)`. Returns 1 if str matches the shell-style pattern pat, else 0. '*' matches any sequence (including empty), '?' matches exactly one character. All other characters match literally. The whole string must match. Reply with only a C code block. No main function, no tests, no explanation.
/* glob_match.c - shell‑style pattern matching
*
* Returns 1 if the null‑terminated string `str` matches the pattern `pat`,
* otherwise returns 0. The pattern may contain:
* * – matches any sequence of characters (including the empty sequence)
* ? – matches exactly one character
* All other characters match themselves literally.
*
* The whole string must be matched; partial matches are not accepted.
*
* This implementation follows the classic two‑pointer algorithm with back‑tracking
* for '*', running in O(|pat|·|str|) worst‑case time and O(1) extra space.
*/
int glob_match(const char *pat, const char *str)
{
const char *p = pat; /* current position in pattern */
const char *s = str; /* current position in string */
const char *star = NULL; /* last position of '*' in pattern */
const char *ss = NULL; /* position in string when '*' was seen */
while (*s) {
if (*p == '*') {
/* remember this '*', advance pattern, and remember string position */
star = p++;
ss = s;
} else if (*p == '?' || *p == *s) {
/* single‑character match */
p++;
s++;
} else if (star) {
/* mismatch after a previous '*': backtrack */
p = star + 1; /* pattern after '*' */
s = ++ss; /* try to let '*' consume one more character */
} else {
/* no '*' to backtrack to and characters differ */
return 0;
}
}
/* Consume any trailing '*' in the pattern */
while (*p == '*')
++p;
return *p == '\0';
}Served via llama.cpp CUDA · — tok/s · — GB RAM · coding total 108/126
No failures.
Implement in C11: `double rpn_eval(const char *expr)`. Evaluates a space-separated Reverse Polish Notation expression of non-negative integers and operators + - * /. Returns the result as a double. Input is always valid and non-empty. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_STACK 1000
double rpn_eval(const char *expr) {
double stack[MAX_STACK];
int top = -1;
char *token = strtok((char *)expr, " ");
while (token != NULL) {
if (isdigit(token[0])) {
stack[++top] = atof(token);
} else {
double b = stack[top--];
double a = stack[top--];
switch (token[0]) {
case '+': stack[++top] = a + b; break;
case '-': stack[++top] = a - b; break;
case '*': stack[++top] = a * b; break;
case '/': stack[++top] = a / b; break;
}
}
token = strtok(NULL, " ");
}
return stack[top];
}#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_STACK 1000
double rpn_eval(const char *expr) {
double stack[MAX_STACK];
int top = -1;
char *token = strtok((char *)expr, " ");
while (token != NULL) {
if (isdigit(token[0])) {
stack[++top] = atof(token);
} else {
double b = stack[top--];
double a = stack[top--];
switch (token[0]) {
case '+': stack[++top] = a + b; break;
case '-': stack[++top] = a - b; break;
case '*': stack[++top] = a * b; break;
case '/': stack[++top] = a / b; break;
}
}
token = strtok(NULL, " ");
}
return stack[top];
}#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_STACK 1000
double rpn_eval(const char *expr) {
double stack[MAX_STACK];
int top = -1;
char *token = strtok((char *)expr, " ");
while (token != NULL) {
if (isdigit(token[0])) {
stack[++top] = atof(token);
} else {
double b = stack[top--];
double a = stack[top--];
switch (token[0]) {
case '+': stack[++top] = a + b; break;
case '-': stack[++top] = a - b; break;
case '*': stack[++top] = a * b; break;
case '/': stack[++top] = a / b; break;
}
}
token = strtok(NULL, " ");
}
return stack[top];
}Served via llama.cpp CUDA · — tok/s · — GB RAM · coding total 105/126
Implement in C11: `int count_words(const char *s)`. Counts words in s. Words are separated by one or more spaces; leading/trailing spaces possible. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdbool.h>
#include <ctype.h>
int count_words(const char *s) {
if (s == NULL) {
return 0;
}
int count = 0;
bool in_word = false;
while (*s != '\0') {
if (isspace((unsigned char)*s)) {
in_word = false;
} else {
if (!in_word) {
count++;
in_word = true;
}
}
s++;
}
return count;
}#include <ctype.h>
#include <stdbool.h>
int count_words(const char *s) {
if (s == NULL) {
return 0;
}
int count = 0;
bool in_word = false;
while (*s != '\0') {
if (isspace((unsigned char)*s)) {
in_word = false;
} else if (!in_word) {
in_word = true;
count++;
}
s++;
}
return count;
}#include <ctype.h>
#include <stdbool.h>
int count_words(const char *s) {
if (s == NULL) {
return 0;
}
int count = 0;
bool in_word = false;
while (*s != '\0') {
if (isspace((unsigned char)*s)) {
in_word = false;
} else if (!in_word) {
in_word = true;
count++;
}
s++;
}
return count;
}Implement in C11: `int cmp_desc(const void *a, const void *b)`. qsort comparator ordering int values descending. Must be safe against overflow. Reply with only a C code block. No main function, no tests, no explanation.
int cmp_desc(const void *a, const void *b) {
int val_a = *(const int *)a;
int val_b = *(const int *)b;
if (val_a > val_b) {
return 1;
} else if (val_a < val_b) {
return -1;
} else {
return 0;
}
}int cmp_desc(const void *a, const void *b) {
int val_a = *(const int *)a;
int val_b = *(const int *)b;
if (val_a > val_b) {
return 1;
} else if (val_a < val_b) {
return -1;
} else {
return 0;
}
}int cmp_desc(const void *a, const void *b) {
int val_a = *(const int *)a;
int val_b = *(const int *)b;
if (val_a > val_b) {
return 1;
} else if (val_a < val_b) {
return -1;
} else {
return 0;
}
}No failures.
Served via llama.cpp CUDA · — tok/s · — GB RAM · coding total 105/126
Implement in C11: `int cmp_desc(const void *a, const void *b)`. qsort comparator ordering int values descending. Must be safe against overflow. Reply with only a C code block. No main function, no tests, no explanation.
#include <limits.h>
int cmp_desc(const void *a, const void *b) {
int ia = *(const int *)a;
int ib = *(const int *)b;
if (ia > ib) {
if (ia > 0 && ib < 0) return 1;
if (ia < 0 && ib > 0) return -1;
if (ia - ib > 0) return 1;
return 0;
} else if (ia < ib) {
if (ia < 0 && ib > 0) return -1;
if (ia > 0 && ib < 0) return 1;
if (ia - ib < 0) return -1;
return 0;
}
return 0;
}#include <limits.h>
int cmp_desc(const void *a, const void *b) {
int ia = *(const int *)a;
int ib = *(const int *)b;
if (ia > ib) {
if (ia > 0 && ib < 0) return 1;
if (ia < 0 && ib > 0) return -1;
if (ia - ib > 0) return 1;
return 0;
} else if (ia < ib) {
if (ia < 0 && ib > 0) return -1;
if (ia > 0 && ib < 0) return 1;
if (ia - ib < 0) return -1;
return 0;
}
return 0;
}#include <limits.h>
int cmp_desc(const void *a, const void *b) {
int ia = *(const int *)a;
int ib = *(const int *)b;
if (ia > ib) {
if (ia > 0 && ib < 0) return 1;
if (ia < 0 && ib > 0) return -1;
if (ia - ib > 0) return 1;
return 0;
} else if (ia < ib) {
if (ia < 0 && ib > 0) return -1;
if (ia > 0 && ib < 0) return 1;
if (ia - ib < 0) return -1;
return 0;
}
return 0;
}Implement in C11: `int atoi_strict(const char *s, int *out)`. Parses an optionally negative decimal integer from s into out. Returns 0 on success, -1 if s is not exactly one valid integer (empty, junk, trailing chars). No overflow tests. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
if (*p == '\0') {
return -1;
}
int sign = 1;
if (*p == '-') {
sign = -1;
p++;
if (*p == '\0') {
return -1;
}
} else if (*p == '+') {
p++;
if (*p == '\0') {
return -1;
}
}
if (!isdigit((unsigned char)*p)) {
return -1;
}
while (isdigit((unsigned char)*p)) {
p++;
}
if (*p != '\0') {
return -1;
}
long long val = 0;
p = s;
if (*p == '-' || *p == '+') {
p++;
}
while (isdigit((unsigned char)*p)) {
val = val * 10 + (*p - '0');
p++;
}
*out = (int)(sign * val);
return 0;
}#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
if (*p == '\0') {
return -1;
}
int sign = 1;
if (*p == '-') {
sign = -1;
p++;
if (*p == '\0') {
return -1;
}
} else if (*p == '+') {
p++;
if (*p == '\0') {
return -1;
}
}
if (!isdigit((unsigned char)*p)) {
return -1;
}
while (isdigit((unsigned char)*p)) {
p++;
}
if (*p != '\0') {
return -1;
}
long long val = 0;
p = s;
if (*p == '-' || *p == '+') {
p++;
}
while (isdigit((unsigned char)*p)) {
val = val * 10 + (*p - '0');
p++;
}
*out = (int)(sign * val);
return 0;
}#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
if (*p == '\0') {
return -1;
}
int sign = 1;
if (*p == '-') {
sign = -1;
p++;
if (*p == '\0') {
return -1;
}
} else if (*p == '+') {
p++;
if (*p == '\0') {
return -1;
}
}
if (!isdigit((unsigned char)*p)) {
return -1;
}
while (isdigit((unsigned char)*p)) {
p++;
}
if (*p != '\0') {
return -1;
}
long long val = 0;
p = s;
if (*p == '-' || *p == '+') {
p++;
}
while (isdigit((unsigned char)*p)) {
val = val * 10 + (*p - '0');
p++;
}
*out = (int)(sign * val);
return 0;
}Implement in C11: `int glob_match(const char *pat, const char *str)`. Returns 1 if str matches the shell-style pattern pat, else 0. '*' matches any sequence (including empty), '?' matches exactly one character. All other characters match literally. The whole string must match. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdbool.h>
int glob_match(const char *pat, const char *str) {
const char *p = pat;
const char *s = str;
const char *star = NULL;
const char *match = NULL;
while (*s) {
if (*p == '*') {
star = p;
match = s;
p++;
} else if (*p == '?' || *p == *s) {
p++;
s++;
} else if (star) {
p = star + 1;
match++;
s = match;
} else {
return 0;
}
}
while (*p == '*') {
p++;
}
return *p == '\0';
}#include <stdbool.h>
int glob_match(const char *pat, const char *str) {
const char *p = pat;
const char *s = str;
const char *star = NULL;
const char *match = NULL;
while (*s) {
if (*p == '*') {
star = p;
match = s;
p++;
} else if (*p == '?' || *p == *s) {
p++;
s++;
} else if (star) {
p = star + 1;
match++;
s = match;
} else {
return 0;
}
}
while (*p == '*') {
p++;
}
return *p == '\0';
}#include <stdbool.h>
int glob_match(const char *pat, const char *str) {
const char *p = pat;
const char *s = str;
const char *star = NULL;
const char *match = NULL;
while (*s) {
if (*p == '*') {
star = p;
match = s;
p++;
} else if (*p == '?' || *p == *s) {
p++;
s++;
} else if (star) {
p = star + 1;
match++;
s = match;
} else {
return 0;
}
}
while (*p == '*') {
p++;
}
return *p == '\0';
}Served via llama.cpp CUDA · — tok/s · — GB RAM · coding total 105/126
Implement in C11: `int is_prime(int n)`. Returns 1 if n is prime, 0 otherwise. n <= 1 is not prime. Reply with only a C code block. No main function, no tests, no explanation.
#include <math.h>
int is_prime(int n) {
if (n <= 1) return 0;
if (n == 2) return 1;
if (n % 2 == 0) return 0;
int limit = (int)sqrt(n);
for (int i = 3; i <= limit; i += 2) {
if (n % i == 0) return 0;
}
return 1;
}#include <math.h>
int is_prime(int n) {
if (n <= 1) return 0;
if (n == 2) return 1;
if (n % 2 == 0) return 0;
int limit = (int)sqrt(n);
for (int i = 3; i <= limit; i += 2) {
if (n % i == 0) return 0;
}
return 1;
}#include <math.h>
int is_prime(int n) {
if (n <= 1) return 0;
if (n == 2) return 1;
if (n % 2 == 0) return 0;
int limit = (int)sqrt(n);
for (int i = 3; i <= limit; i += 2) {
if (n % i == 0) return 0;
}
return 1;
}Implement in C11: `void itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
#include <limits.h>
void itoa(int value, char *buf) {
char temp[12];
int i = 0;
unsigned int uvalue;
int negative = 0;
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
if (value < 0) {
negative = 1;
uvalue = (unsigned int)(-(value + 1)) + 1;
} else {
uvalue = (unsigned int)value;
}
while (uvalue > 0) {
temp[i++] = '0' + (uvalue % 10);
uvalue /= 10;
}
if (negative) {
buf[i++] = '-';
}
for (int j = 0; j < i; j++) {
buf[j] = temp[i - 1 - j];
}
buf[i] = '\0';
}Implement in C11: `uint32_t bswap32(uint32_t x)`. Returns x with its four bytes reversed (endian swap). Include <stdint.h> yourself. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdint.h>
static inline uint32_t bswap32(uint32_t x) {
return ((x & 0x000000FFU) << 24) |
((x & 0x0000FF00U) << 8) |
((x & 0x00FF0000U) >> 8) |
((x & 0xFF000000U) >> 24);
}#include <stdint.h>
static inline uint32_t bswap32(uint32_t x) {
return ((x & 0x000000FFU) << 24) |
((x & 0x0000FF00U) << 8) |
((x & 0x00FF0000U) >> 8) |
((x & 0xFF000000U) >> 24);
}#include <stdint.h>
static inline uint32_t bswap32(uint32_t x) {
return ((x & 0x000000FFU) << 24) |
((x & 0x0000FF00U) << 8) |
((x & 0x00FF0000U) >> 8) |
((x & 0xFF000000U) >> 24);
}Implement in C11: `int cmp_desc(const void *a, const void *b)`. qsort comparator ordering int values descending. Must be safe against overflow. Reply with only a C code block. No main function, no tests, no explanation.
int cmp_desc(const void *a, const void *b) {
int ia = *(const int *)a;
int ib = *(const int *)b;
return (ia > ib) - (ia < ib);
}int cmp_desc(const void *a, const void *b) {
int ia = *(const int *)a;
int ib = *(const int *)b;
return (ia > ib) - (ia < ib);
}int cmp_desc(const void *a, const void *b) {
int ia = *(const int *)a;
int ib = *(const int *)b;
return (ia > ib) - (ia < ib);
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (idx < 0 || !line || !out || cap == 0) return -1;
const char *p = line;
int current_idx = 0;
size_t out_pos = 0;
while (current_idx <= idx && *p) {
if (current_idx == idx) {
if (*p == '"') {
p++;
while (*p) {
if (*p == '"') {
if (*(p + 1) == '"') {
if (out_pos < cap - 1) out[out_pos++] = '"';
p += 2;
} else if (*(p + 1) == ',' || *(p + 1) == '\0' || *(p + 1) == '\n') {
p++;
break;
} else {
if (out_pos < cap - 1) out[out_pos++] = '"';
p++;
}
} else {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
}
if (*p == '"') p++;
if (out_pos < cap) out[out_pos] = '\0';
return (int)out_pos;
} else {
while (*p && *p != ',' && *p != '\n') {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
if (out_pos < cap) out[out_pos] = '\0';
return (int)out_pos;
}
}
if (*p == '"') {
p++;
while (*p && *p != '"') {
if (*p == '\\' && *(p + 1) == '"') p += 2;
else if (*p == '"') p++;
else p++;
}
if (*p == '"') p++;
} else {
while (*p && *p != ',' && *p != '\n') p++;
}
if (*p == ',') p++;
current_idx++;
}
return -1;
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (idx < 0 || !line || !out || cap == 0) return -1;
const char *p = line;
int current_idx = 0;
char *field_start = NULL;
size_t out_len = 0;
while (*p) {
if (current_idx == idx) {
field_start = (char *)p;
out_len = 0;
}
if (*p == '"') {
p++;
while (*p) {
if (*p == '"') {
if (p[1] == '"') {
if (out_len < cap - 1) out[out_len] = '"';
out_len++;
p += 2;
} else {
p++;
break;
}
} else {
if (out_len < cap - 1) out[out_len] = *p;
out_len++;
p++;
}
}
if (current_idx == idx) {
if (*p) p++;
}
} else if (*p == ',') {
if (current_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
current_idx++;
p++;
} else {
if (current_idx == idx) {
if (out_len < cap - 1) out[out_len] = *p;
out_len++;
}
if (*p == '\r' || *p == '\n') break;
p++;
}
}
if (current_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
return -1;
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (idx < 0 || !line || !out || cap == 0) return -1;
const char *p = line;
int current_idx = 0;
char *field_start = NULL;
size_t out_len = 0;
while (*p) {
if (current_idx == idx) {
field_start = (char *)p;
out_len = 0;
}
if (*p == '"') {
p++;
while (*p) {
if (*p == '"') {
if (p[1] == '"') {
if (out_len < cap - 1) out[out_len] = '"';
out_len++;
p += 2;
} else {
p++;
break;
}
} else {
if (out_len < cap - 1) out[out_len] = *p;
out_len++;
p++;
}
}
if (current_idx == idx) {
if (*p) p++;
}
} else if (*p == ',') {
if (current_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
current_idx++;
p++;
} else {
if (current_idx == idx) {
if (out_len < cap - 1) out[out_len] = *p;
out_len++;
}
if (*p == '\r' || *p == '\n') break;
p++;
}
}
if (current_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
return -1;
}Served via llama.cpp CUDA · — tok/s · — GB RAM · coding total 105/126
Implement in C11: `int atoi_strict(const char *s, int *out)`. Parses an optionally negative decimal integer from s into out. Returns 0 on success, -1 if s is not exactly one valid integer (empty, junk, trailing chars). No overflow tests. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (!s || !out) return -1;
int sign = 1;
long val = 0;
int started = 0;
while (*s && isspace((unsigned char)*s)) s++;
if (*s == '-') {
sign = -1;
s++;
} else if (*s == '+') {
s++;
}
while (*s) {
if (!isdigit((unsigned char)*s)) return -1;
started = 1;
val = val * 10 + (*s - '0');
s++;
}
if (!started) return -1;
*out = (int)(sign * val);
return 0;
}#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (!s || !out) return -1;
int sign = 1;
long val = 0;
int started = 0;
while (*s && isspace((unsigned char)*s)) s++;
if (*s == '-') {
sign = -1;
s++;
} else if (*s == '+') {
s++;
}
while (*s) {
if (!isdigit((unsigned char)*s)) return -1;
started = 1;
val = val * 10 + (*s - '0');
s++;
}
if (!started) return -1;
*out = (int)(sign * val);
return 0;
}#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (!s || !out) return -1;
int sign = 1;
long val = 0;
int started = 0;
while (*s && isspace((unsigned char)*s)) s++;
if (*s == '-') {
sign = -1;
s++;
} else if (*s == '+') {
s++;
}
while (*s) {
if (!isdigit((unsigned char)*s)) return -1;
started = 1;
val = val * 10 + (*s - '0');
s++;
}
if (!started) return -1;
*out = (int)(sign * val);
return 0;
}Implement in C11: `int glob_match(const char *pat, const char *str)`. Returns 1 if str matches the shell-style pattern pat, else 0. '*' matches any sequence (including empty), '?' matches exactly one character. All other characters match literally. The whole string must match. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdbool.h>
int glob_match(const char *pat, const char *str) {
const char *star = NULL;
const char *match = str;
while (*str) {
if (*pat == '?' || *pat == *str) {
pat++;
str++;
} else if (*pat == '*') {
star = pat++;
match = str;
} else if (star) {
pat = star + 1;
str = ++match;
} else {
return 0;
}
}
while (*pat == '*') {
pat++;
}
return *pat == '\0';
}#include <stdbool.h>
int glob_match(const char *pat, const char *str) {
const char *star = NULL;
const char *match = str;
while (*str) {
if (*pat == '?' || *pat == *str) {
pat++;
str++;
} else if (*pat == '*') {
star = pat++;
match = str;
} else if (star) {
pat = star + 1;
str = ++match;
} else {
return 0;
}
}
while (*pat == '*') {
pat++;
}
return *pat == '\0';
}#include <stdbool.h>
int glob_match(const char *pat, const char *str) {
const char *star = NULL;
const char *match = str;
while (*str) {
if (*pat == '?' || *pat == *str) {
pat++;
str++;
} else if (*pat == '*') {
star = pat++;
match = str;
} else if (star) {
pat = star + 1;
str = ++match;
} else {
return 0;
}
}
while (*pat == '*') {
pat++;
}
return *pat == '\0';
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (!line || idx < 0) return -1;
const char *p = line;
int field_idx = 0;
size_t out_len = 0;
while (*p) {
if (field_idx == idx) {
if (*p == '"') {
p++;
while (*p && (out_len < cap - 1)) {
if (*p == '"') {
p++;
if (*p == '"') {
if (out_len < cap - 1) out[out_len++] = '"';
p++;
} else {
break;
}
} else {
if (out_len < cap - 1) out[out_len++] = *p;
p++;
}
}
} else {
while (*p && *p != ',' && (out_len < cap - 1)) {
out[out_len++] = *p;
p++;
}
}
out[out_len] = '\0';
return (int)out_len;
}
if (*p == '"') {
p++;
while (*p && (*p != '"' || *(p+1) == '"')) {
if (*p == '"' && *(p+1) == '"') p += 2;
else p++;
}
if (*p == '"') p++;
} else {
while (*p && *p != ',') p++;
}
if (*p == ',') p++;
field_idx++;
}
return -1;
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (!line || idx < 0) return -1;
const char *p = line;
int field_idx = 0;
size_t out_len = 0;
while (*p) {
if (field_idx == idx) {
if (*p == '"') {
p++;
while (*p && (out_len < cap - 1)) {
if (*p == '"') {
p++;
if (*p == '"') {
if (out_len < cap - 1) out[out_len++] = '"';
p++;
} else {
break;
}
} else {
if (out_len < cap - 1) out[out_len++] = *p;
p++;
}
}
} else {
while (*p && *p != ',' && (out_len < cap - 1)) {
out[out_len++] = *p;
p++;
}
}
out[out_len] = '\0';
return (int)out_len;
}
if (*p == '"') {
p++;
while (*p && (*p != '"' || *(p+1) == '"')) {
if (*p == '"' && *(p+1) == '"') p += 2;
else p++;
}
if (*p == '"') p++;
} else {
while (*p && *p != ',') p++;
}
if (*p == ',') p++;
field_idx++;
}
return -1;
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (!line || idx < 0) return -1;
const char *p = line;
int field_idx = 0;
size_t out_len = 0;
while (*p) {
if (field_idx == idx) {
if (*p == '"') {
p++;
while (*p && (out_len < cap - 1)) {
if (*p == '"') {
p++;
if (*p == '"') {
if (out_len < cap - 1) out[out_len++] = '"';
p++;
} else {
break;
}
} else {
if (out_len < cap - 1) out[out_len++] = *p;
p++;
}
}
} else {
while (*p && *p != ',' && (out_len < cap - 1)) {
out[out_len++] = *p;
p++;
}
}
out[out_len] = '\0';
return (int)out_len;
}
if (*p == '"') {
p++;
while (*p && (*p != '"' || *(p+1) == '"')) {
if (*p == '"' && *(p+1) == '"') p += 2;
else p++;
}
if (*p == '"') p++;
} else {
while (*p && *p != ',') p++;
}
if (*p == ',') p++;
field_idx++;
}
return -1;
}Served via llama.cpp CUDA · — tok/s · — GB RAM · coding total 102/126
Implement in C11: `int binary_search(const int *a, int n, int key)`. a is sorted ascending, n elements. Returns the index of key, or -1 if absent. Reply with only a C code block. No main function, no tests, no explanation.
int binary_search(const int *a, int n, int key)
{
if (n <= 0 || a == NULL)
return -1;
int low = 0;
int high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (a[mid] == key)
return mid;
else if (a[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}int binary_search(const int *a, int n, int key)
{
if (n <= 0 || a == NULL)
return -1;
int low = 0;
int high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (a[mid] == key)
return mid;
else if (a[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}Implement in C11: `int cmp_desc(const void *a, const void *b)`. qsort comparator ordering int values descending. Must be safe against overflow. Reply with only a C code block. No main function, no tests, no explanation.
(no code extracted)(no code extracted)Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
(no code extracted)(no code extracted)(no code extracted)Served via llama.cpp CUDA · — tok/s · — GB RAM · coding total 102/126
Implement in C11: `void itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
#include <limits.h>
void itoa(int value, char *buf) {
char *p = buf;
int is_negative = 0;
unsigned int uvalue;
if (value < 0) {
is_negative = 1;
/* Handle INT_MIN safely by converting to unsigned */
uvalue = (unsigned int)(-(value + 1)) + 1u;
} else {
uvalue = (unsigned int)value;
}
if (uvalue == 0) {
*p++ = '0';
} else {
char tmp[10];
int i = 0;
while (uvalue > 0) {
tmp[i++] = (char)('0' + (uvalue % 10));
uvalue /= 10;
}
while (i > 0) {
*p++ = tmp[--i];
}
}
if (is_negative) {
*p++ = '-';
}
*p = '\0';
}#include <limits.h>
void itoa(int value, char *buf) {
char *p = buf;
int is_negative = 0;
unsigned int uvalue;
if (value < 0) {
is_negative = 1;
/* Handle INT_MIN safely by converting to unsigned */
uvalue = (unsigned int)(-(value + 1)) + 1u;
} else {
uvalue = (unsigned int)value;
}
if (uvalue == 0) {
*p++ = '0';
} else {
char tmp[10];
int i = 0;
while (uvalue > 0) {
tmp[i++] = (char)('0' + (uvalue % 10));
uvalue /= 10;
}
while (i > 0) {
*p++ = tmp[--i];
}
}
if (is_negative) {
*p++ = '-';
}
*p = '\0';
}#include <limits.h>
void itoa(int value, char *buf) {
char *p = buf;
int is_negative = 0;
unsigned int uvalue;
if (value < 0) {
is_negative = 1;
/* Handle INT_MIN safely by converting to unsigned */
uvalue = (unsigned int)(-(value + 1)) + 1u;
} else {
uvalue = (unsigned int)value;
}
if (uvalue == 0) {
*p++ = '0';
} else {
char tmp[10];
int i = 0;
while (uvalue > 0) {
tmp[i++] = (char)('0' + (uvalue % 10));
uvalue /= 10;
}
while (i > 0) {
*p++ = tmp[--i];
}
}
if (is_negative) {
*p++ = '-';
}
*p = '\0';
}Implement in C11: `void trim(char *s)`. Strips leading and trailing whitespace (space, tab, newline) from s in place. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
void trim(char *s) {
if (s == NULL) return;
char *start = s;
while (*start && isspace((unsigned char)*start)) {
start++;
}
char *end = start + __builtin_strlen(start) - 1;
while (end > start && isspace((unsigned char)*end)) {
end--;
}
if (start != s) {
size_t len = (size_t)(end - start) + 1;
for (size_t i = 0; i < len; i++) {
s[i] = start[i];
}
s[len] = '\0';
} else {
end[1] = '\0';
}
}#include <ctype.h>
void trim(char *s) {
if (s == NULL) return;
char *start = s;
while (*start && isspace((unsigned char)*start)) {
start++;
}
char *end = start + __builtin_strlen(start) - 1;
while (end > start && isspace((unsigned char)*end)) {
end--;
}
if (start != s) {
size_t len = (size_t)(end - start) + 1;
for (size_t i = 0; i < len; i++) {
s[i] = start[i];
}
s[len] = '\0';
} else {
end[1] = '\0';
}
}#include <ctype.h>
void trim(char *s) {
if (s == NULL) return;
char *start = s;
while (*start && isspace((unsigned char)*start)) {
start++;
}
char *end = start + __builtin_strlen(start) - 1;
while (end > start && isspace((unsigned char)*end)) {
end--;
}
if (start != s) {
size_t len = (size_t)(end - start) + 1;
for (size_t i = 0; i < len; i++) {
s[i] = start[i];
}
s[len] = '\0';
} else {
end[1] = '\0';
}
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int field_idx = 0;
size_t out_len = 0;
while (1) {
/* Skip leading whitespace? RFC 4180 doesn't specify, but typically no.
We just check if we're at a quoted field. */
if (*p == '"') {
/* Quoted field */
p++; /* skip opening quote */
while (1) {
if (*p == '\0') {
/* Unterminated quote; treat rest as field content?
RFC says this is invalid, but we'll just stop. */
break;
}
if (*p == '"') {
if (*(p + 1) == '"') {
/* Doubled quote -> single quote */
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
} else {
/* Closing quote */
p++;
break;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
}
/* After closing quote, expect comma or end of line */
if (*p == ',') {
p++;
}
/* If not comma and not end, it's malformed; we'll just continue */
} else {
/* Unquoted field */
while (*p != '\0' && *p != ',') {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
if (*p == ',') {
p++;
}
}
/* We've parsed one field */
if (field_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
/* Check if we've reached end of line */
if (*p == '\0') {
return -1; /* idx out of range */
}
field_idx++;
}
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int field_idx = 0;
size_t out_len = 0;
while (1) {
/* Skip leading whitespace? RFC 4180 doesn't specify, but typically no.
We just check if we're at a quoted field. */
if (*p == '"') {
/* Quoted field */
p++; /* skip opening quote */
while (1) {
if (*p == '\0') {
/* Unterminated quote; treat rest as field content?
RFC says this is invalid, but we'll just stop. */
break;
}
if (*p == '"') {
if (*(p + 1) == '"') {
/* Doubled quote -> single quote */
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
} else {
/* Closing quote */
p++;
break;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
}
/* After closing quote, expect comma or end of line */
if (*p == ',') {
p++;
}
/* If not comma and not end, it's malformed; we'll just continue */
} else {
/* Unquoted field */
while (*p != '\0' && *p != ',') {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
if (*p == ',') {
p++;
}
}
/* We've parsed one field */
if (field_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
/* Check if we've reached end of line */
if (*p == '\0') {
return -1; /* idx out of range */
}
field_idx++;
}
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int field_idx = 0;
size_t out_len = 0;
while (1) {
/* Skip leading whitespace? RFC 4180 doesn't specify, but typically no.
We just check if we're at a quoted field. */
if (*p == '"') {
/* Quoted field */
p++; /* skip opening quote */
while (1) {
if (*p == '\0') {
/* Unterminated quote; treat rest as field content?
RFC says this is invalid, but we'll just stop. */
break;
}
if (*p == '"') {
if (*(p + 1) == '"') {
/* Doubled quote -> single quote */
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
} else {
/* Closing quote */
p++;
break;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
}
/* After closing quote, expect comma or end of line */
if (*p == ',') {
p++;
}
/* If not comma and not end, it's malformed; we'll just continue */
} else {
/* Unquoted field */
while (*p != '\0' && *p != ',') {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
if (*p == ',') {
p++;
}
}
/* We've parsed one field */
if (field_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
/* Check if we've reached end of line */
if (*p == '\0') {
return -1; /* idx out of range */
}
field_idx++;
}
}Served via llama.cpp CUDA · — tok/s · — GB RAM · coding total 99/126
Implement in C11: `void itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
#include <limits.h>
#include <string.h>
void itoa(int value, char *buf) {
int i = 0;
int negative = 0;
if (value < 0) {
negative = 1;
// Handle INT_MIN safely by using long long for negation
long long val = (long long)value;
val = -val;
value = (int)val;
}
// Handle zero case
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
// Convert digits in reverse order
while (value > 0) {
buf[i++] = (char)('0' + (value % 10));
value /= 10;
}
// Add negative sign if needed
if (negative) {
buf[i++] = '-';
}
// Null-terminate and reverse the string
buf[i] = '\0';
// Reverse the buffer in place
int start = 0;
int end = i - 1;
while (start < end) {
char temp = buf[start];
buf[start] = buf[end];
buf[end] = temp;
start++;
end--;
}
}#include <limits.h>
#include <string.h>
void itoa(int value, char *buf) {
int i = 0;
int negative = 0;
if (value < 0) {
negative = 1;
// Handle INT_MIN safely by using long long for negation
long long val = (long long)value;
val = -val;
value = (int)val;
}
// Handle zero case
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
// Convert digits in reverse order
while (value > 0) {
buf[i++] = (char)('0' + (value % 10));
value /= 10;
}
// Add negative sign if needed
if (negative) {
buf[i++] = '-';
}
// Null-terminate and reverse the string
buf[i] = '\0';
// Reverse the buffer in place
int start = 0;
int end = i - 1;
while (start < end) {
char temp = buf[start];
buf[start] = buf[end];
buf[end] = temp;
start++;
end--;
}
}#include <limits.h>
#include <string.h>
void itoa(int value, char *buf) {
int i = 0;
int negative = 0;
if (value < 0) {
negative = 1;
// Handle INT_MIN safely by using long long for negation
long long val = (long long)value;
val = -val;
value = (int)val;
}
// Handle zero case
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
// Convert digits in reverse order
while (value > 0) {
buf[i++] = (char)('0' + (value % 10));
value /= 10;
}
// Add negative sign if needed
if (negative) {
buf[i++] = '-';
}
// Null-terminate and reverse the string
buf[i] = '\0';
// Reverse the buffer in place
int start = 0;
int end = i - 1;
while (start < end) {
char temp = buf[start];
buf[start] = buf[end];
buf[end] = temp;
start++;
end--;
}
}Implement in C11: `void trim(char *s)`. Strips leading and trailing whitespace (space, tab, newline) from s in place. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
void trim(char *s) {
if (s == NULL || *s == '\0') return;
// Skip leading whitespace
char *start = s;
while (*start && isspace((unsigned char)*start)) {
start++;
}
// If string is all whitespace
if (*start == '\0') {
*s = '\0';
return;
}
// Find end of non-whitespace content
char *end = start;
while (*end && !isspace((unsigned char)*end)) {
end++;
}
// Move trimmed content to beginning
while (start <= end) {
*s++ = *start++;
}
*s = '\0';
}#include <ctype.h>
void trim(char *s) {
if (s == NULL || *s == '\0') return;
// Skip leading whitespace
char *start = s;
while (*start && isspace((unsigned char)*start)) {
start++;
}
// If string is all whitespace
if (*start == '\0') {
*s = '\0';
return;
}
// Find end of non-whitespace content
char *end = start;
while (*end && !isspace((unsigned char)*end)) {
end++;
}
// Move trimmed content to beginning
while (start <= end) {
*s++ = *start++;
}
*s = '\0';
}#include <ctype.h>
void trim(char *s) {
if (s == NULL || *s == '\0') return;
// Skip leading whitespace
char *start = s;
while (*start && isspace((unsigned char)*start)) {
start++;
}
// If string is all whitespace
if (*start == '\0') {
*s = '\0';
return;
}
// Find end of non-whitespace content
char *end = start;
while (*end && !isspace((unsigned char)*end)) {
end++;
}
// Move trimmed content to beginning
while (start <= end) {
*s++ = *start++;
}
*s = '\0';
}Implement in C11: `int atoi_strict(const char *s, int *out)`. Parses an optionally negative decimal integer from s into out. Returns 0 on success, -1 if s is not exactly one valid integer (empty, junk, trailing chars). No overflow tests. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (!s || !out) return -1;
const char *p = s;
int sign = 1;
// Skip leading whitespace
while (isspace((unsigned char)*p)) p++;
// Check for sign
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
// Must have at least one digit
if (!isdigit((unsigned char)*p)) return -1;
int result = 0;
while (isdigit((unsigned char)*p)) {
result = result * 10 + (*p - '0');
p++;
}
// Skip trailing whitespace
while (isspace((unsigned char)*p)) p++;
// Must have consumed entire string
if (*p != '\0') return -1;
*out = result * sign;
return 0;
}#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (!s || !out) return -1;
const char *p = s;
int sign = 1;
// Skip leading whitespace
while (isspace((unsigned char)*p)) p++;
// Check for sign
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
// Must have at least one digit
if (!isdigit((unsigned char)*p)) return -1;
int result = 0;
while (isdigit((unsigned char)*p)) {
result = result * 10 + (*p - '0');
p++;
}
// Skip trailing whitespace
while (isspace((unsigned char)*p)) p++;
// Must have consumed entire string
if (*p != '\0') return -1;
*out = result * sign;
return 0;
}#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (!s || !out) return -1;
const char *p = s;
int sign = 1;
// Skip leading whitespace
while (isspace((unsigned char)*p)) p++;
// Check for sign
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
// Must have at least one digit
if (!isdigit((unsigned char)*p)) return -1;
int result = 0;
while (isdigit((unsigned char)*p)) {
result = result * 10 + (*p - '0');
p++;
}
// Skip trailing whitespace
while (isspace((unsigned char)*p)) p++;
// Must have consumed entire string
if (*p != '\0') return -1;
*out = result * sign;
return 0;
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
#include <stddef.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int field = 0;
int in_quotes = 0;
size_t out_pos = 0;
while (*p != '\0') {
if (field == idx) {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
if (*p == '"') {
if (p[1] == '"') {
p++;
if (out_pos < cap - 1) {
out[out_pos++] = '"';
}
} else {
in_quotes = !in_quotes;
}
}
} else {
if (*p == '"') {
if (p[1] == '"') {
p++;
} else {
in_quotes = !in_quotes;
}
}
}
if (!in_quotes && *p == ',') {
if (field == idx) {
break;
}
field++;
p++;
continue;
}
p++;
}
if (field == idx) {
out[out_pos] = '\0';
return (int)out_pos;
} else {
return -1;
}
}#include <stddef.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int field = 0;
int in_quotes = 0;
size_t out_pos = 0;
while (*p != '\0') {
if (field == idx) {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
if (*p == '"') {
if (p[1] == '"') {
p++;
if (out_pos < cap - 1) {
out[out_pos++] = '"';
}
} else {
in_quotes = !in_quotes;
}
}
} else {
if (*p == '"') {
if (p[1] == '"') {
p++;
} else {
in_quotes = !in_quotes;
}
}
}
if (!in_quotes && *p == ',') {
if (field == idx) {
break;
}
field++;
p++;
continue;
}
p++;
}
if (field == idx) {
out[out_pos] = '\0';
return (int)out_pos;
} else {
return -1;
}
}#include <stddef.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int field = 0;
int in_quotes = 0;
size_t out_pos = 0;
while (*p != '\0') {
if (field == idx) {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
if (*p == '"') {
if (p[1] == '"') {
p++;
if (out_pos < cap - 1) {
out[out_pos++] = '"';
}
} else {
in_quotes = !in_quotes;
}
}
} else {
if (*p == '"') {
if (p[1] == '"') {
p++;
} else {
in_quotes = !in_quotes;
}
}
}
if (!in_quotes && *p == ',') {
if (field == idx) {
break;
}
field++;
p++;
continue;
}
p++;
}
if (field == idx) {
out[out_pos] = '\0';
return (int)out_pos;
} else {
return -1;
}
}Served via llama.cpp CUDA · — tok/s · — GB RAM · coding total 99/126
Implement in C11: `void reverse_string(char *s)`. Reverses s in place. Reply with only a C code block. No main function, no tests, no explanation.
void reverse_string(char *s) {
if (s == NULL) return;
size_t len = 0;
while (s[len] != '\0') {
len++;
}
for (size_t i = 0; i < len / 2; i++) {
char temp = s[i];
s[i] = s[len - 1 - i];
s[len - 1 - i] = temp;
}
}void reverse_string(char *s) {
if (s == NULL) return;
size_t len = 0;
while (s[len] != '\0') {
len++;
}
for (size_t i = 0; i < len / 2; i++) {
char temp = s[i];
s[i] = s[len - 1 - i];
s[len - 1 - i] = temp;
}
}void reverse_string(char *s) {
if (s == NULL) return;
size_t len = 0;
while (s[len] != '\0') {
len++;
}
for (size_t i = 0; i < len / 2; i++) {
char temp = s[i];
s[i] = s[len - 1 - i];
s[len - 1 - i] = temp;
}
}Implement in C11: `int count_words(const char *s)`. Counts words in s. Words are separated by one or more spaces; leading/trailing spaces possible. Reply with only a C code block. No main function, no tests, no explanation.
int count_words(const char *s) {
int count = 0;
int in_word = 0;
if (s == NULL) {
return 0;
}
while (*s) {
if (*s == ' ') {
in_word = 0;
} else {
if (!in_word) {
count++;
in_word = 1;
}
}
s++;
}
return count;
}int count_words(const char *s) {
int count = 0;
int in_word = 0;
if (s == NULL) {
return 0;
}
while (*s) {
if (*s == ' ') {
in_word = 0;
} else {
if (!in_word) {
count++;
in_word = 1;
}
}
s++;
}
return count;
}int count_words(const char *s) {
int count = 0;
int in_word = 0;
if (s == NULL) {
return 0;
}
while (*s) {
if (*s == ' ') {
in_word = 0;
} else {
if (!in_word) {
count++;
in_word = 1;
}
}
s++;
}
return count;
}Implement in C11: `void trim(char *s)`. Strips leading and trailing whitespace (space, tab, newline) from s in place. Reply with only a C code block. No main function, no tests, no explanation.
void trim(char *s) {
if (s == NULL) return;
// Find the first non-whitespace character
char *start = s;
while (*start == ' ' || *start == '\t' || *start == '\n') {
start++;
}
// If the string is all whitespace or empty, just null-terminate
if (*start == '\0') {
*s = '\0';
return;
}
// Find the last non-whitespace character
char *end = s + strlen(s) - 1;
while (end > start && (*end == ' ' || *end == '\t' || *end == '\n')) {
end--;
}
// Move the trimmed substring to the beginning of s
size_t len = (size_t)(end - start + 1);
memmove(s, start, len);
s[len] = '\0';
}void trim(char *s) {
if (s == NULL) return;
// Find the first non-whitespace character
char *start = s;
while (*start == ' ' || *start == '\t' || *start == '\n') {
start++;
}
// If the string is all whitespace or empty, just null-terminate
if (*start == '\0') {
*s = '\0';
return;
}
// Find the last non-whitespace character
char *end = s + strlen(s) - 1;
while (end > start && (*end == ' ' || *end == '\t' || *end == '\n')) {
end--;
}
// Move the trimmed substring to the beginning of s
size_t len = (size_t)(end - start + 1);
memmove(s, start, len);
s[len] = '\0';
}void trim(char *s) {
if (s == NULL) return;
// Find the first non-whitespace character
char *start = s;
while (*start == ' ' || *start == '\t' || *start == '\n') {
start++;
}
// If the string is all whitespace or empty, just null-terminate
if (*start == '\0') {
*s = '\0';
return;
}
// Find the last non-whitespace character
char *end = s + strlen(s) - 1;
while (end > start && (*end == ' ' || *end == '\t' || *end == '\n')) {
end--;
}
// Move the trimmed substring to the beginning of s
size_t len = (size_t)(end - start + 1);
memmove(s, start, len);
s[len] = '\0';
}Implement in C11: `int atoi_strict(const char *s, int *out)`. Parses an optionally negative decimal integer from s into out. Returns 0 on success, -1 if s is not exactly one valid integer (empty, junk, trailing chars). No overflow tests. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
#include <stddef.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
int sign = 1;
long long val = 0;
// Skip leading whitespace
while (isspace((unsigned char)*p)) {
p++;
}
// Check for empty string after whitespace
if (*p == '\0') {
return -1;
}
// Handle optional sign
if (*p == '+') {
p++;
} else if (*p == '-') {
sign = -1;
p++;
}
// Check if there are digits
if (!isdigit((unsigned char)*p)) {
return -1;
}
// Parse digits
while (isdigit((unsigned char)*p)) {
val = val * 10 + (*p - '0');
p++;
}
// Check for trailing characters (must be end of string)
if (*p != '\0') {
return -1;
}
// Apply sign
val *= sign;
// Store result
*out = (int)val;
return 0;
}#include <ctype.h>
#include <stddef.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
int sign = 1;
long long val = 0;
// Skip leading whitespace
while (isspace((unsigned char)*p)) {
p++;
}
// Check for empty string after whitespace
if (*p == '\0') {
return -1;
}
// Handle optional sign
if (*p == '+') {
p++;
} else if (*p == '-') {
sign = -1;
p++;
}
// Check if there are digits
if (!isdigit((unsigned char)*p)) {
return -1;
}
// Parse digits
while (isdigit((unsigned char)*p)) {
val = val * 10 + (*p - '0');
p++;
}
// Check for trailing characters (must be end of string)
if (*p != '\0') {
return -1;
}
// Apply sign
val *= sign;
// Store result
*out = (int)val;
return 0;
}#include <ctype.h>
#include <stddef.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
int sign = 1;
long long val = 0;
// Skip leading whitespace
while (isspace((unsigned char)*p)) {
p++;
}
// Check for empty string after whitespace
if (*p == '\0') {
return -1;
}
// Handle optional sign
if (*p == '+') {
p++;
} else if (*p == '-') {
sign = -1;
p++;
}
// Check if there are digits
if (!isdigit((unsigned char)*p)) {
return -1;
}
// Parse digits
while (isdigit((unsigned char)*p)) {
val = val * 10 + (*p - '0');
p++;
}
// Check for trailing characters (must be end of string)
if (*p != '\0') {
return -1;
}
// Apply sign
val *= sign;
// Store result
*out = (int)val;
return 0;
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int current_idx = 0;
while (*p != '\0') {
if (current_idx == idx) {
// Start extracting the field
size_t out_pos = 0;
if (*p == '"') {
// Quoted field
p++; // skip opening quote
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
// Escaped quote
if (out_pos < cap - 1) {
out[out_pos++] = '"';
}
p += 2;
} else {
// End of quoted field
p++; // skip closing quote
break;
}
} else {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
p++;
}
}
// After closing quote, expect comma or end of string
if (*p == ',') {
p++;
}
out[out_pos] = '\0';
return (int)out_pos;
} else {
// Unquoted field
while (*p != '\0' && *p != ',') {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
p++;
}
if (*p == ',') {
p++;
}
out[out_pos] = '\0';
return (int)out_pos;
}
}
// Skip to next field
if (*p == '"') {
p++; // skip opening quote
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
p += 2;
} else {
p++; // skip closing quote
break;
}
} else {
p++;
}
}
// After closing quote, expect comma or end of string
if (*p == ',') {
p++;
}
} else {
while (*p != '\0' && *p != ',') {
p++;
}
if (*p == ',') {
p++;
}
}
current_idx++;
}
// If we've processed all fields and idx is still not reached, it's out of range
// But we need to check if the last field was the one we wanted
// The loop above increments current_idx after processing each field.
// If the line ends with a field (no trailing comma), current_idx will be one more than the last field index.
// So if idx == current_idx - 1 and we haven't returned yet, we missed it?
// Actually, the logic above handles extraction when current_idx == idx at the start of the field.
// If we exit the loop, it means we ran out of characters.
// Let's reconsider: if idx is beyond the number of fields, return -1.
// Count fields to determine if idx is valid
// Actually, the above loop structure: when we start processing a field, current_idx is the index of that field.
// If we finish the line and never hit current_idx == idx, then idx is out of range.
return -1;
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int current_idx = 0;
while (*p != '\0') {
if (current_idx == idx) {
// Start extracting the field
size_t out_pos = 0;
if (*p == '"') {
// Quoted field
p++; // skip opening quote
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
// Escaped quote
if (out_pos < cap - 1) {
out[out_pos++] = '"';
}
p += 2;
} else {
// End of quoted field
p++; // skip closing quote
break;
}
} else {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
p++;
}
}
// After closing quote, expect comma or end of string
if (*p == ',') {
p++;
}
out[out_pos] = '\0';
return (int)out_pos;
} else {
// Unquoted field
while (*p != '\0' && *p != ',') {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
p++;
}
if (*p == ',') {
p++;
}
out[out_pos] = '\0';
return (int)out_pos;
}
}
// Skip to next field
if (*p == '"') {
p++; // skip opening quote
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
p += 2;
} else {
p++; // skip closing quote
break;
}
} else {
p++;
}
}
// After closing quote, expect comma or end of string
if (*p == ',') {
p++;
}
} else {
while (*p != '\0' && *p != ',') {
p++;
}
if (*p == ',') {
p++;
}
}
current_idx++;
}
// If we've processed all fields and idx is still not reached, it's out of range
// But we need to check if the last field was the one we wanted
// The loop above increments current_idx after processing each field.
// If the line ends with a field (no trailing comma), current_idx will be one more than the last field index.
// So if idx == current_idx - 1 and we haven't returned yet, we missed it?
// Actually, the logic above handles extraction when current_idx == idx at the start of the field.
// If we exit the loop, it means we ran out of characters.
// Let's reconsider: if idx is beyond the number of fields, return -1.
// Count fields to determine if idx is valid
// Actually, the above loop structure: when we start processing a field, current_idx is the index of that field.
// If we finish the line and never hit current_idx == idx, then idx is out of range.
return -1;
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int current_idx = 0;
while (*p != '\0') {
if (current_idx == idx) {
// Start extracting the field
size_t out_pos = 0;
if (*p == '"') {
// Quoted field
p++; // skip opening quote
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
// Escaped quote
if (out_pos < cap - 1) {
out[out_pos++] = '"';
}
p += 2;
} else {
// End of quoted field
p++; // skip closing quote
break;
}
} else {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
p++;
}
}
// After closing quote, expect comma or end of string
if (*p == ',') {
p++;
}
out[out_pos] = '\0';
return (int)out_pos;
} else {
// Unquoted field
while (*p != '\0' && *p != ',') {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
p++;
}
if (*p == ',') {
p++;
}
out[out_pos] = '\0';
return (int)out_pos;
}
}
// Skip to next field
if (*p == '"') {
p++; // skip opening quote
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
p += 2;
} else {
p++; // skip closing quote
break;
}
} else {
p++;
}
}
// After closing quote, expect comma or end of string
if (*p == ',') {
p++;
}
} else {
while (*p != '\0' && *p != ',') {
p++;
}
if (*p == ',') {
p++;
}
}
current_idx++;
}
// If we've processed all fields and idx is still not reached, it's out of range
// But we need to check if the last field was the one we wanted
// The loop above increments current_idx after processing each field.
// If the line ends with a field (no trailing comma), current_idx will be one more than the last field index.
// So if idx == current_idx - 1 and we haven't returned yet, we missed it?
// Actually, the logic above handles extraction when current_idx == idx at the start of the field.
// If we exit the loop, it means we ran out of characters.
// Let's reconsider: if idx is beyond the number of fields, return -1.
// Count fields to determine if idx is valid
// Actually, the above loop structure: when we start processing a field, current_idx is the index of that field.
// If we finish the line and never hit current_idx == idx, then idx is out of range.
return -1;
}Served via llama.cpp CUDA · — tok/s · — GB RAM · coding total 96/126
Implement in C11: `void reverse_string(char *s)`. Reverses s in place. Reply with only a C code block. No main function, no tests, no explanation.
void reverse_string(char *s) {
if (s == NULL) return;
char *end = s + strlen(s) - 1;
while (s < end) {
char tmp = *s;
*s++ = *end;
*end-- = tmp;
}
}void reverse_string(char *s) {
if (s == NULL) return;
char *end = s + strlen(s) - 1;
while (s < end) {
char tmp = *s;
*s++ = *end;
*end-- = tmp;
}
}void reverse_string(char *s) {
if (s == NULL) return;
char *end = s + strlen(s) - 1;
while (s < end) {
char tmp = *s;
*s++ = *end;
*end-- = tmp;
}
}Implement in C11: `void itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
#include <limits.h>
#include <stdbool.h>
void itoa(int value, char *buf) {
bool is_negative = false;
if (value < 0) {
is_negative = true;
if (value == INT_MIN) {
// Handle INT_MIN separately to avoid overflow
*buf++ = '-';
value = -(value + 1);
*buf++ = '2';
value = 1431655765; // (INT_MAX + 1) / 10
*buf++ = '1';
value = 463648264; // (INT_MAX + 1) % 10
*buf++ = '4';
value = 746348264; // (INT_MAX + 1) / 10
*buf++ = '6';
value = 348264; // (INT_MAX + 1) % 10
*buf++ = '3';
value = 48264; // (INT_MAX + 1) / 10
*buf++ = '4';
value = 8264; // (INT_MAX + 1) % 10
*buf++ = '8';
value = 264; // (INT_MAX + 1) / 10
*buf++ = '2';
value = 64; // (INT_MAX + 1) % 10
*buf++ = '6';
value = 6; // (INT_MAX + 1) / 10
*buf++ = '4';
*buf++ = '7';
*buf = '\0';
return;
}
value = -value;
}
char temp[12];
int i = 0;
do {
temp[i++] = '0' + (value % 10);
value /= 10;
} while (value > 0);
if (is_negative) {
*buf++ = '-';
}
while (i > 0) {
*buf++ = temp[--i];
}
*buf = '\0';
}#include <limits.h>
#include <stdbool.h>
void itoa(int value, char *buf) {
bool is_negative = false;
if (value < 0) {
is_negative = true;
if (value == INT_MIN) {
// Handle INT_MIN separately to avoid overflow
*buf++ = '-';
value = -(value + 1);
*buf++ = '2';
value = 1431655766; // INT_MIN + 1 = 2147483647, so 2147483647 / 10 = 214748364
*buf++ = '1';
value = 43165576;
*buf++ = '4';
value = 7483647;
*buf++ = '7';
value = 483647;
*buf++ = '4';
value = 83647;
*buf++ = '8';
value = 3647;
*buf++ = '3';
value = 647;
*buf++ = '6';
value = 47;
*buf++ = '4';
value = 7;
*buf++ = '7';
*buf = '\0';
return;
}
value = -value;
}
char temp[12]; // Enough for INT_MAX (10 digits) + NUL
int i = 0;
do {
temp[i++] = '0' + (value % 10);
value /= 10;
} while (value > 0);
if (is_negative) {
*buf++ = '-';
}
while (i > 0) {
*buf++ = temp[--i];
}
*buf = '\0';
}#include <limits.h>
#include <stdbool.h>
void itoa(int value, char *buf) {
bool is_negative = false;
if (value < 0) {
is_negative = true;
if (value == INT_MIN) {
// Handle INT_MIN separately to avoid overflow
*buf++ = '-';
value = -(value + 1);
*buf++ = '2';
value = 1431655766; // INT_MIN + 1 = 2147483647, so 2147483647 / 10 = 214748364
*buf++ = '1';
value = 43165576;
*buf++ = '4';
value = 7483647;
*buf++ = '7';
value = 483647;
*buf++ = '4';
value = 83647;
*buf++ = '8';
value = 3647;
*buf++ = '3';
value = 647;
*buf++ = '6';
value = 47;
*buf++ = '4';
value = 7;
*buf++ = '7';
*buf = '\0';
return;
}
value = -value;
}
char temp[12]; // Enough for INT_MAX (10 digits) + NUL
int i = 0;
do {
temp[i++] = '0' + (value % 10);
value /= 10;
} while (value > 0);
if (is_negative) {
*buf++ = '-';
}
while (i > 0) {
*buf++ = temp[--i];
}
*buf = '\0';
}Implement in C11: `void trim(char *s)`. Strips leading and trailing whitespace (space, tab, newline) from s in place. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
void trim(char *s) {
if (!s) return;
char *end = s + strlen(s) - 1;
while (end >= s && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
char *start = s;
while (*start && isspace((unsigned char)*start)) {
start++;
}
memmove(s, start, end - start + 2);
}#include <ctype.h>
void trim(char *s) {
if (!s) return;
char *end = s + strlen(s) - 1;
while (end >= s && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
char *start = s;
while (*start && isspace((unsigned char)*start)) {
start++;
}
memmove(s, start, end - start + 2);
}#include <ctype.h>
void trim(char *s) {
if (!s) return;
char *end = s + strlen(s) - 1;
while (end >= s && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
char *start = s;
while (*start && isspace((unsigned char)*start)) {
start++;
}
memmove(s, start, end - start + 2);
}Implement in C11: `int atoi_strict(const char *s, int *out)`. Parses an optionally negative decimal integer from s into out. Returns 0 on success, -1 if s is not exactly one valid integer (empty, junk, trailing chars). No overflow tests. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
#include <stddef.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) return -1;
// Skip leading whitespace
while (isspace((unsigned char)*s)) s++;
// Check for empty string
if (*s == '\0') return -1;
// Handle sign
int sign = 1;
if (*s == '-') {
sign = -1;
s++;
} else if (*s == '+') {
s++;
}
// Check if there's at least one digit
if (!isdigit((unsigned char)*s)) return -1;
// Parse digits
long long value = 0;
while (isdigit((unsigned char)*s)) {
value = value * 10 + (*s - '0');
s++;
}
// Check for trailing non-digit characters
while (isspace((unsigned char)*s)) s++;
if (*s != '\0') return -1;
*out = sign * value;
return 0;
}#include <ctype.h>
#include <stddef.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) return -1;
// Skip leading whitespace
while (isspace((unsigned char)*s)) s++;
// Check for empty string
if (*s == '\0') return -1;
// Handle sign
int sign = 1;
if (*s == '-') {
sign = -1;
s++;
} else if (*s == '+') {
s++;
}
// Check if there's at least one digit
if (!isdigit((unsigned char)*s)) return -1;
// Parse digits
long long value = 0;
while (isdigit((unsigned char)*s)) {
value = value * 10 + (*s - '0');
s++;
}
// Check for trailing non-digit characters
while (isspace((unsigned char)*s)) s++;
if (*s != '\0') return -1;
*out = sign * value;
return 0;
}#include <ctype.h>
#include <stddef.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) return -1;
// Skip leading whitespace
while (isspace((unsigned char)*s)) s++;
// Check for empty string
if (*s == '\0') return -1;
// Handle sign
int sign = 1;
if (*s == '-') {
sign = -1;
s++;
} else if (*s == '+') {
s++;
}
// Check if there's at least one digit
if (!isdigit((unsigned char)*s)) return -1;
// Parse digits
long long value = 0;
while (isdigit((unsigned char)*s)) {
value = value * 10 + (*s - '0');
s++;
}
// Check for trailing non-digit characters
while (isspace((unsigned char)*s)) s++;
if (*s != '\0') return -1;
*out = sign * value;
return 0;
}Implement in C11: `double rpn_eval(const char *expr)`. Evaluates a space-separated Reverse Polish Notation expression of non-negative integers and operators + - * /. Returns the result as a double. Input is always valid and non-empty. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
double rpn_eval(const char *expr) {
char *stack = malloc(strlen(expr) * sizeof(double));
if (!stack) return NAN;
double *top = stack;
const char *p = expr;
while (*p) {
if (isspace(*p)) { p++; continue; }
if (isdigit(*p)) {
double num = 0;
while (isdigit(*p)) {
num = num * 10 + (*p - '0');
p++;
}
*top++ = num;
} else {
double b = *(--top);
double a = *(--top);
switch (*p++) {
case '+': *top++ = a + b; break;
case '-': *top++ = a - b; break;
case '*': *top++ = a * b; break;
case '/': *top++ = a / b; break;
}
}
}
double result = *(--top);
free(stack);
return result;
}#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
double rpn_eval(const char *expr) {
char *stack = malloc(strlen(expr) * sizeof(double));
if (!stack) return NAN;
double *top = stack;
const char *p = expr;
while (*p) {
if (isspace(*p)) { p++; continue; }
if (isdigit(*p)) {
double num = 0;
while (isdigit(*p)) {
num = num * 10 + (*p - '0');
p++;
}
*top++ = num;
} else {
double b = *(--top);
double a = *(--top);
switch (*p++) {
case '+': *top++ = a + b; break;
case '-': *top++ = a - b; break;
case '*': *top++ = a * b; break;
case '/': *top++ = a / b; break;
}
}
}
double result = *(--top);
free(stack);
return result;
}#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
double rpn_eval(const char *expr) {
char *stack = malloc(strlen(expr) * sizeof(double));
if (!stack) return NAN;
double *top = stack;
const char *p = expr;
while (*p) {
if (isspace(*p)) { p++; continue; }
if (isdigit(*p)) {
double num = 0;
while (isdigit(*p)) {
num = num * 10 + (*p - '0');
p++;
}
*top++ = num;
} else {
double b = *(--top);
double a = *(--top);
switch (*p++) {
case '+': *top++ = a + b; break;
case '-': *top++ = a - b; break;
case '*': *top++ = a * b; break;
case '/': *top++ = a / b; break;
}
}
}
double result = *(--top);
free(stack);
return result;
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
#include <string.h>
#include <ctype.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (cap == 0) return -1;
const char *p = line;
int field = 0;
int in_quotes = 0;
size_t out_pos = 0;
while (*p && field <= idx) {
if (in_quotes) {
if (*p == '"') {
const char *next = p + 1;
if (*next == '"') {
if (out_pos < cap - 1) out[out_pos++] = '"';
p = next + 1;
} else {
in_quotes = 0;
p++;
}
} else {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
} else {
if (*p == '"') {
in_quotes = 1;
p++;
} else if (*p == ',') {
field++;
p++;
if (field > idx) break;
} else {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
}
}
if (field != idx) return -1;
if (out_pos < cap) out[out_pos] = '\0';
return (int)out_pos;
}#include <string.h>
#include <ctype.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (cap == 0) return -1;
const char *p = line;
int field = 0;
int in_quotes = 0;
size_t out_pos = 0;
while (*p && field <= idx) {
if (in_quotes) {
if (*p == '"') {
const char *next = p + 1;
if (*next == '"') {
if (out_pos < cap - 1) out[out_pos++] = '"';
p = next + 1;
} else {
in_quotes = 0;
p++;
}
} else {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
} else {
if (*p == '"') {
in_quotes = 1;
p++;
} else if (*p == ',') {
field++;
p++;
if (field > idx) break;
} else {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
}
}
if (field != idx) return -1;
if (out_pos < cap) out[out_pos] = '\0';
return (int)out_pos;
}#include <string.h>
#include <ctype.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (cap == 0) return -1;
const char *p = line;
int field = 0;
int in_quotes = 0;
size_t out_pos = 0;
while (*p && field <= idx) {
if (in_quotes) {
if (*p == '"') {
const char *next = p + 1;
if (*next == '"') {
if (out_pos < cap - 1) out[out_pos++] = '"';
p = next + 1;
} else {
in_quotes = 0;
p++;
}
} else {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
} else {
if (*p == '"') {
in_quotes = 1;
p++;
} else if (*p == ',') {
field++;
p++;
if (field > idx) break;
} else {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
}
}
if (field != idx) return -1;
if (out_pos < cap) out[out_pos] = '\0';
return (int)out_pos;
}Served via llama.cpp CUDA · — tok/s · — GB RAM · coding total 96/126
Implement in C11: `void itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
#include <limits.h>
#include <stddef.h>
void itoa(int value, char *buf) {
char temp[12];
int i = 0;
int is_negative = 0;
if (value == INT_MIN) {
/* Handle INT_MIN separately to avoid overflow when negating */
const char *s = "-2147483648";
while (*s) {
buf[i++] = *s++;
}
buf[i] = '\0';
return;
}
if (value < 0) {
is_negative = 1;
value = -value;
}
if (value == 0) {
buf[i++] = '0';
} else {
while (value > 0) {
temp[i++] = (value % 10) + '0';
value /= 10;
}
}
if (is_negative) {
buf[i++] = '-';
}
/* Reverse the string */
int start = 0;
int end = i - 1;
while (start < end) {
char tmp = buf[start];
buf[start] = buf[end];
buf[end] = tmp;
start++;
end--;
}
buf[i] = '\0';
}#include <limits.h>
#include <stddef.h>
void itoa(int value, char *buf) {
char temp[12];
int i = 0;
int is_negative = 0;
if (value == INT_MIN) {
/* Handle INT_MIN separately to avoid overflow when negating */
const char *s = "-2147483648";
while (*s) {
buf[i++] = *s++;
}
buf[i] = '\0';
return;
}
if (value < 0) {
is_negative = 1;
value = -value;
}
if (value == 0) {
buf[i++] = '0';
} else {
while (value > 0) {
temp[i++] = (value % 10) + '0';
value /= 10;
}
}
if (is_negative) {
buf[i++] = '-';
}
/* Reverse the string */
int start = 0;
int end = i - 1;
while (start < end) {
char tmp = buf[start];
buf[start] = buf[end];
buf[end] = tmp;
start++;
end--;
}
buf[i] = '\0';
}#include <limits.h>
#include <stddef.h>
void itoa(int value, char *buf) {
char temp[12];
int i = 0;
int is_negative = 0;
if (value == INT_MIN) {
/* Handle INT_MIN separately to avoid overflow when negating */
const char *s = "-2147483648";
while (*s) {
buf[i++] = *s++;
}
buf[i] = '\0';
return;
}
if (value < 0) {
is_negative = 1;
value = -value;
}
if (value == 0) {
buf[i++] = '0';
} else {
while (value > 0) {
temp[i++] = (value % 10) + '0';
value /= 10;
}
}
if (is_negative) {
buf[i++] = '-';
}
/* Reverse the string */
int start = 0;
int end = i - 1;
while (start < end) {
char tmp = buf[start];
buf[start] = buf[end];
buf[end] = tmp;
start++;
end--;
}
buf[i] = '\0';
}Implement in C11: `void trim(char *s)`. Strips leading and trailing whitespace (space, tab, newline) from s in place. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
void trim(char *s) {
if (!s) return;
// Strip leading whitespace
while (isspace((unsigned char)*s)) {
s++;
}
// Strip trailing whitespace
char *end = s;
while (*end != '\0') {
end++;
}
end--;
while (end > s && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
// Move the trimmed string to the beginning if leading whitespace was removed
if (s != end + 1) {
char *dst = s;
char *src = s;
while (*src) {
*dst++ = *src++;
}
*dst = '\0';
}
}#include <ctype.h>
void trim(char *s) {
if (!s) return;
// Strip leading whitespace
while (isspace((unsigned char)*s)) {
s++;
}
// Strip trailing whitespace
char *end = s;
while (*end != '\0') {
end++;
}
end--;
while (end > s && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
// Move the trimmed string to the beginning if leading whitespace was removed
if (s != end + 1) {
char *dst = s;
char *src = s;
while (*src) {
*dst++ = *src++;
}
*dst = '\0';
}
}#include <ctype.h>
void trim(char *s) {
if (!s) return;
// Strip leading whitespace
while (isspace((unsigned char)*s)) {
s++;
}
// Strip trailing whitespace
char *end = s;
while (*end != '\0') {
end++;
}
end--;
while (end > s && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
// Move the trimmed string to the beginning if leading whitespace was removed
if (s != end + 1) {
char *dst = s;
char *src = s;
while (*src) {
*dst++ = *src++;
}
*dst = '\0';
}
}Implement in C11: `int atoi_strict(const char *s, int *out)`. Parses an optionally negative decimal integer from s into out. Returns 0 on success, -1 if s is not exactly one valid integer (empty, junk, trailing chars). No overflow tests. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
int sign = 1;
// Skip leading whitespace
while (isspace((unsigned char)*p)) {
p++;
}
// Handle optional sign
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
// Check if there is at least one digit
if (!isdigit((unsigned char)*p)) {
return -1;
}
// Parse digits
int result = 0;
while (isdigit((unsigned char)*p)) {
int digit = *p - '0';
result = result * 10 + digit;
p++;
}
// Check for trailing non-whitespace characters
if (*p != '\0') {
// Allow trailing whitespace only if it was part of the initial skip?
// The spec says "exactly one valid integer", so trailing chars (including whitespace) make it invalid.
// However, typically atoi ignores trailing whitespace. But the spec says "trailing chars" is invalid.
// Let's re-read: "s is not exactly one valid integer (empty, junk, trailing chars)".
// This implies that if there are any characters after the integer, it's invalid.
// But what about leading whitespace? The standard atoi skips leading whitespace.
// The prompt says "Parses an optionally negative decimal integer from s".
// Usually, strict parsing means no leading/trailing whitespace unless specified.
// But the example of "trailing chars" being invalid suggests we should check for anything after the number.
// Let's assume leading whitespace is allowed (like standard atoi) but trailing is not.
// Actually, let's look at the return condition: "-1 if s is not exactly one valid integer".
// If we allow leading whitespace, then " 123 " would have trailing space, so invalid.
// " 123" would be valid.
// Let's stick to: skip leading whitespace, parse integer, ensure nothing left (or only null terminator).
// Wait, if we skipped leading whitespace, we are at the first digit or sign.
// After parsing digits, if *p is not '\0', it's invalid.
return -1;
}
*out = sign * result;
return 0;
}#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
int sign = 1;
// Skip leading whitespace
while (isspace((unsigned char)*p)) {
p++;
}
// Handle optional sign
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
// Check if there is at least one digit
if (!isdigit((unsigned char)*p)) {
return -1;
}
// Parse digits
int result = 0;
while (isdigit((unsigned char)*p)) {
int digit = *p - '0';
result = result * 10 + digit;
p++;
}
// Check for trailing non-whitespace characters
if (*p != '\0') {
// Allow trailing whitespace only if it was part of the initial skip?
// The spec says "exactly one valid integer", so trailing chars (including whitespace) make it invalid.
// However, typically atoi ignores trailing whitespace. But the spec says "trailing chars" is invalid.
// Let's re-read: "s is not exactly one valid integer (empty, junk, trailing chars)".
// This implies that if there are any characters after the integer, it's invalid.
// But what about leading whitespace? The standard atoi skips leading whitespace.
// The prompt says "Parses an optionally negative decimal integer from s".
// Usually, strict parsing means no leading/trailing whitespace unless specified.
// But the example of "trailing chars" being invalid suggests we should check for anything after the number.
// Let's assume leading whitespace is allowed (like standard atoi) but trailing is not.
// Actually, let's look at the return condition: "-1 if s is not exactly one valid integer".
// If we allow leading whitespace, then " 123 " would have trailing space, so invalid.
// " 123" would be valid.
// Let's stick to: skip leading whitespace, parse integer, ensure nothing left (or only null terminator).
// Wait, if we skipped leading whitespace, we are at the first digit or sign.
// After parsing digits, if *p is not '\0', it's invalid.
return -1;
}
*out = sign * result;
return 0;
}#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
int sign = 1;
// Skip leading whitespace
while (isspace((unsigned char)*p)) {
p++;
}
// Handle optional sign
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
// Check if there is at least one digit
if (!isdigit((unsigned char)*p)) {
return -1;
}
// Parse digits
int result = 0;
while (isdigit((unsigned char)*p)) {
int digit = *p - '0';
result = result * 10 + digit;
p++;
}
// Check for trailing non-whitespace characters
if (*p != '\0') {
// Allow trailing whitespace only if it was part of the initial skip?
// The spec says "exactly one valid integer", so trailing chars (including whitespace) make it invalid.
// However, typically atoi ignores trailing whitespace. But the spec says "trailing chars" is invalid.
// Let's re-read: "s is not exactly one valid integer (empty, junk, trailing chars)".
// This implies that if there are any characters after the integer, it's invalid.
// But what about leading whitespace? The standard atoi skips leading whitespace.
// The prompt says "Parses an optionally negative decimal integer from s".
// Usually, strict parsing means no leading/trailing whitespace unless specified.
// But the example of "trailing chars" being invalid suggests we should check for anything after the number.
// Let's assume leading whitespace is allowed (like standard atoi) but trailing is not.
// Actually, let's look at the return condition: "-1 if s is not exactly one valid integer".
// If we allow leading whitespace, then " 123 " would have trailing space, so invalid.
// " 123" would be valid.
// Let's stick to: skip leading whitespace, parse integer, ensure nothing left (or only null terminator).
// Wait, if we skipped leading whitespace, we are at the first digit or sign.
// After parsing digits, if *p is not '\0', it's invalid.
return -1;
}
*out = sign * result;
return 0;
}Implement in C11: `int glob_match(const char *pat, const char *str)`. Returns 1 if str matches the shell-style pattern pat, else 0. '*' matches any sequence (including empty), '?' matches exactly one character. All other characters match literally. The whole string must match. Reply with only a C code block. No main function, no tests, no explanation.
int glob_match(const char *pat, const char *str) {
const char *star = NULL;
const char *str_match = NULL;
while (*str) {
switch (*pat) {
case '?':
str++;
pat++;
break;
case '*':
star = pat++;
str_match = str;
break;
default:
if (*pat != *str) {
if (star) {
pat = star + 1;
str_match++;
str = str_match;
} else {
return 0;
}
} else {
str++;
pat++;
}
break;
}
}
while (*pat == '*') {
pat++;
}
return *pat == '\0';
}int glob_match(const char *pat, const char *str) {
const char *star = NULL;
const char *str_match = NULL;
while (*str) {
switch (*pat) {
case '?':
str++;
pat++;
break;
case '*':
star = pat++;
str_match = str;
break;
default:
if (*pat != *str) {
if (star) {
pat = star + 1;
str_match++;
str = str_match;
} else {
return 0;
}
} else {
str++;
pat++;
}
break;
}
}
while (*pat == '*') {
pat++;
}
return *pat == '\0';
}int glob_match(const char *pat, const char *str) {
const char *star = NULL;
const char *str_match = NULL;
while (*str) {
switch (*pat) {
case '?':
str++;
pat++;
break;
case '*':
star = pat++;
str_match = str;
break;
default:
if (*pat != *str) {
if (star) {
pat = star + 1;
str_match++;
str = str_match;
} else {
return 0;
}
} else {
str++;
pat++;
}
break;
}
}
while (*pat == '*') {
pat++;
}
return *pat == '\0';
}Served via llama.cpp CUDA · — tok/s · — GB RAM · coding total 94/126
Implement in C11: `int atoi_strict(const char *s, int *out)`. Parses an optionally negative decimal integer from s into out. Returns 0 on success, -1 if s is not exactly one valid integer (empty, junk, trailing chars). No overflow tests. Reply with only a C code block. No main function, no tests, no explanation.
#include <limits.h>
#include <stdbool.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
bool negative = false;
// Skip leading whitespace
while (*p == ' ') {
p++;
}
// Handle optional sign
if (*p == '-') {
negative = true;
p++;
} else if (*p == '+') {
p++;
}
// Check for empty string after sign
if (*p == '\0') {
return -1;
}
// Parse digits
long result = 0;
bool has_digits = false;
while (*p >= '0' && *p <= '9') {
has_digits = true;
result = result * 10 + (*p - '0');
p++;
}
// Must have at least one digit
if (!has_digits) {
return -1;
}
// Check for trailing characters
while (*p != '\0') {
if (*p != ' ') {
return -1;
}
p++;
}
// Apply sign and check bounds
if (negative) {
if (result > (unsigned long)INT_MAX + 1) {
return -1;
}
*out = -(int)result;
} else {
if (result > INT_MAX) {
return -1;
}
*out = (int)result;
}
return 0;
}#include <limits.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
int sign = 1;
long result = 0;
int valid = 0;
// Skip leading whitespace
while (*p == ' ') {
p++;
}
// Handle optional sign
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
// Parse digits
while (*p >= '0' && *p <= '9') {
result = result * 10 + (*p - '0');
valid = 1;
p++;
}
// If no digits were found, it's invalid
if (!valid) {
return -1;
}
// Apply sign and check for overflow
if (sign == 1 && result > INT_MAX) {
return -1;
}
if (sign == -1 && -result < INT_MIN) {
return -1;
}
*out = (int)(sign * result);
return 0;
}#include <limits.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
int sign = 1;
long result = 0;
int valid = 0;
// Skip leading whitespace
while (*p == ' ') {
p++;
}
// Handle optional sign
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
// Parse digits
while (*p >= '0' && *p <= '9') {
result = result * 10 + (*p - '0');
valid = 1;
p++;
}
// If no digits were found, it's invalid
if (!valid) {
return -1;
}
// Apply sign and check for overflow
if (sign == 1 && result > INT_MAX) {
return -1;
}
if (sign == -1 && -result < INT_MIN) {
return -1;
}
*out = (int)(sign * result);
return 0;
}Implement in C11: `int glob_match(const char *pat, const char *str)`. Returns 1 if str matches the shell-style pattern pat, else 0. '*' matches any sequence (including empty), '?' matches exactly one character. All other characters match literally. The whole string must match. Reply with only a C code block. No main function, no tests, no explanation.
#include <stddef.h>
static int match_star(const char *s, const char *p, const char **ss, const char **sp);
int glob_match(const char *pat, const char *str) {
const char *s = str;
const char *p = pat;
while (*s) {
if (*p == '*') {
if (match_star(s, p, &s, &p)) {
return 1;
}
return 0;
} else if (*p == '?' || *p == *s) {
s++;
p++;
} else {
return 0;
}
}
while (*p == '*') {
p++;
}
return *p == '\0';
}
static int match_star(const char *s, const char *p, const char **ss, const char **sp) {
const char *star = *sp;
const char *match = *ss;
const char *tmp;
while (*match) {
if (*p == '?' || *p == *match) {
p++;
match++;
} else {
if (*p != '*') {
return 0;
}
p++;
tmp = match;
if (match_star(match, p, &match, &p)) {
return 1;
}
match = tmp + 1;
}
}
while (*p == '*') {
p++;
}
if (*p == '\0') {
*ss = match;
*sp = p;
return 1;
}
return 0;
}#include <stddef.h>
#include <stdbool.h>
static bool match_star(const char *s, const char *p, const char **next_s, const char **next_p);
int glob_match(const char *pat, const char *str) {
if (*pat == '*') {
return match_star(str, pat, &str, &pat);
}
for (; *str && *pat; str++, pat++) {
if (*pat == '*') {
return match_star(str, pat, &str, &pat);
}
if (*pat == '?' || *pat == *str) {
continue;
}
return 0;
}
return (*pat == '\0' && *str == '\0');
}
static bool match_star(const char *s, const char *p, const char **next_s, const char **next_p) {
const char *star = *p;
const char *match = *next_s;
const char *tmp_s = *next_s;
const char *tmp_p = *next_p;
for (;;) {
if (*tmp_p == '\0') {
*next_s = tmp_s;
*next_p = tmp_p;
return true;
}
if (*tmp_p == '*') {
tmp_p++;
continue;
}
if (*tmp_p == '?' || *tmp_p == *tmp_s) {
tmp_p++;
tmp_s++;
continue;
}
if (match != *next_s) {
tmp_s = ++match;
continue;
}
return false;
}
}#include <stddef.h>
#include <stdbool.h>
static bool match_star(const char *s, const char *p, const char **next_s, const char **next_p);
int glob_match(const char *pat, const char *str) {
if (*pat == '*') {
return match_star(str, pat, &str, &pat);
}
for (; *str && *pat; str++, pat++) {
if (*pat == '*') {
return match_star(str, pat, &str, &pat);
}
if (*pat == '?' || *pat == *str) {
continue;
}
return 0;
}
return (*pat == '\0' && *str == '\0');
}
static bool match_star(const char *s, const char *p, const char **next_s, const char **next_p) {
const char *star = *p;
const char *match = *next_s;
const char *tmp_s = *next_s;
const char *tmp_p = *next_p;
for (;;) {
if (*tmp_p == '\0') {
*next_s = tmp_s;
*next_p = tmp_p;
return true;
}
if (*tmp_p == '*') {
tmp_p++;
continue;
}
if (*tmp_p == '?' || *tmp_p == *tmp_s) {
tmp_p++;
tmp_s++;
continue;
}
if (match != *next_s) {
tmp_s = ++match;
continue;
}
return false;
}
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
#include <stddef.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
const char *p = line;
const char *start = NULL;
const char *end = NULL;
int current = 0;
int in_quotes = 0;
size_t out_len = 0;
if (idx < 0) {
return -1;
}
while (*p != '\0') {
if (current == idx) {
if (in_quotes) {
start = p;
} else {
start = p;
while (*p != ',' && *p != '\0') {
p++;
}
end = p;
break;
}
}
if (*p == '"') {
if (in_quotes) {
if (*(p + 1) == '"') {
p++;
} else {
in_quotes = 0;
}
} else {
in_quotes = 1;
}
} else if (*p == ',' && !in_quotes) {
current++;
}
p++;
}
if (current != idx) {
return -1;
}
if (in_quotes) {
return -1;
}
if (start == NULL) {
return -1;
}
if (end == NULL) {
end = p;
}
size_t copy_len = end - start;
if (copy_len > cap - 1) {
copy_len = cap - 1;
}
memcpy(out, start, copy_len);
out[copy_len] = '\0';
return (int)copy_len;
}#include <stddef.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
const char *p = line;
const char *start = NULL;
const char *end = NULL;
int current = 0;
int in_quotes = 0;
size_t out_len = 0;
if (idx < 0) return -1;
while (*p != '\0') {
if (current == idx) {
start = p;
while (*p != '\0' && *p != '\n' && *p != '\r') {
if (*p == '"') {
if (in_quotes && *(p + 1) == '"') {
p++;
} else {
in_quotes = !in_quotes;
}
}
p++;
}
end = p;
break;
}
if (*p == '\n' || *p == '\r') {
current++;
p++;
while ((*p == '\n' || *p == '\r') && *p != '\0') {
p++;
}
} else {
p++;
}
}
if (current != idx) {
return -1;
}
if (start == NULL) {
return -1;
}
p = start;
while (p < end) {
if (*p == '"') {
if (in_quotes) {
if (*(p + 1) == '"') {
p++;
} else {
in_quotes = 0;
}
} else {
in_quotes = 1;
}
}
if (in_quotes) {
if (out_len + 1 < cap) {
out[out_len++] = *p;
}
} else {
if (*p != ',') {
if (out_len + 1 < cap) {
out[out_len++] = *p;
}
} else {
break;
}
}
p++;
}
if (out_len >= cap) {
out[cap - 1] = '\0';
} else {
out[out_len] = '\0';
}
return (int)out_len;
}#include <stddef.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
const char *p = line;
const char *start = NULL;
const char *end = NULL;
int current = 0;
int in_quotes = 0;
size_t out_len = 0;
if (idx < 0) return -1;
while (*p != '\0') {
if (current == idx) {
start = p;
while (*p != '\0' && *p != '\n' && *p != '\r') {
if (*p == '"') {
if (in_quotes && *(p + 1) == '"') {
p++;
} else {
in_quotes = !in_quotes;
}
}
p++;
}
end = p;
break;
}
if (*p == '\n' || *p == '\r') {
current++;
p++;
while ((*p == '\n' || *p == '\r') && *p != '\0') {
p++;
}
} else {
p++;
}
}
if (current != idx) {
return -1;
}
if (start == NULL) {
return -1;
}
p = start;
while (p < end) {
if (*p == '"') {
if (in_quotes) {
if (*(p + 1) == '"') {
p++;
} else {
in_quotes = 0;
}
} else {
in_quotes = 1;
}
}
if (in_quotes) {
if (out_len + 1 < cap) {
out[out_len++] = *p;
}
} else {
if (*p != ',') {
if (out_len + 1 < cap) {
out[out_len++] = *p;
}
} else {
break;
}
}
p++;
}
if (out_len >= cap) {
out[cap - 1] = '\0';
} else {
out[out_len] = '\0';
}
return (int)out_len;
}Served via llama.cpp CUDA · — tok/s · — GB RAM · coding total 93/126
Implement in C11: `void reverse_string(char *s)`. Reverses s in place. Reply with only a C code block. No main function, no tests, no explanation.
void reverse_string(char *s) {
if (s == NULL) return;
char *start = s;
char *end = s;
// Find the end of the string
while (*end != '\0') {
end++;
}
end--; // Move back to the last character
// Reverse the string in place
while (start < end) {
char temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}void reverse_string(char *s) {
if (s == NULL) return;
char *start = s;
char *end = s;
// Find the end of the string
while (*end != '\0') {
end++;
}
end--; // Move back to the last character
// Reverse the string in place
while (start < end) {
char temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}void reverse_string(char *s) {
if (s == NULL) return;
char *start = s;
char *end = s;
// Find the end of the string
while (*end != '\0') {
end++;
}
end--; // Move back to the last character
// Reverse the string in place
while (start < end) {
char temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}Implement in C11: `const char *fizzbuzz(int n)`. Returns "FizzBuzz" if n divisible by 15, "Fizz" by 3, "Buzz" by 5, else the decimal string of n. A static buffer is fine. Reply with only a C code block. No main function, no tests, no explanation.
const char *fizzbuzz(int n) {
static char buffer[16];
if (n % 15 == 0) {
return "FizzBuzz";
} else if (n % 3 == 0) {
return "Fizz";
} else if (n % 5 == 0) {
return "Buzz";
} else {
snprintf(buffer, sizeof(buffer), "%d", n);
return buffer;
}
}const char *fizzbuzz(int n) {
static char buffer[16];
if (n % 15 == 0) {
return "FizzBuzz";
} else if (n % 3 == 0) {
return "Fizz";
} else if (n % 5 == 0) {
return "Buzz";
} else {
snprintf(buffer, sizeof(buffer), "%d", n);
return buffer;
}
}const char *fizzbuzz(int n) {
static char buffer[16];
if (n % 15 == 0) {
return "FizzBuzz";
} else if (n % 3 == 0) {
return "Fizz";
} else if (n % 5 == 0) {
return "Buzz";
} else {
snprintf(buffer, sizeof(buffer), "%d", n);
return buffer;
}
}Implement in C11: `int count_words(const char *s)`. Counts words in s. Words are separated by one or more spaces; leading/trailing spaces possible. Reply with only a C code block. No main function, no tests, no explanation.
int count_words(const char *s) {
if (s == NULL) return 0;
int count = 0;
int in_word = 0;
for (int i = 0; s[i] != '\0'; i++) {
if (s[i] == ' ') {
if (in_word) {
count++;
in_word = 0;
}
} else {
in_word = 1;
}
}
if (in_word) {
count++;
}
return count;
}int count_words(const char *s) {
if (s == NULL) return 0;
int count = 0;
int in_word = 0;
for (int i = 0; s[i] != '\0'; i++) {
if (s[i] == ' ') {
if (in_word) {
count++;
in_word = 0;
}
} else {
in_word = 1;
}
}
if (in_word) {
count++;
}
return count;
}int count_words(const char *s) {
if (s == NULL) return 0;
int count = 0;
int in_word = 0;
for (int i = 0; s[i] != '\0'; i++) {
if (s[i] == ' ') {
if (in_word) {
count++;
in_word = 0;
}
} else {
in_word = 1;
}
}
if (in_word) {
count++;
}
return count;
}Implement in C11: `void itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdint.h>
#include <stdbool.h>
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
bool negative = value < 0;
if (negative && value == INT_MIN) {
// Special case for INT_MIN to avoid overflow when negating
int32_t temp = value;
int i = 0;
while (temp != 0) {
buf[i++] = '0' - (temp % 10);
temp /= 10;
}
buf[i] = '\0';
// Reverse the string
for (int j = 0; j < i/2; j++) {
char c = buf[j];
buf[j] = buf[i-1-j];
buf[i-1-j] = c;
}
buf[i] = '\0';
return;
}
int32_t abs_value = negative ? -value : value;
int i = 0;
while (abs_value != 0) {
buf[i++] = '0' + (abs_value % 10);
abs_value /= 10;
}
if (negative) {
buf[i++] = '-';
}
// Reverse the string
for (int j = 0; j < i/2; j++) {
char c = buf[j];
buf[j] = buf[i-1-j];
buf[i-1-j] = c;
}
buf[i] = '\0';
}#include <stdint.h>
#include <stdbool.h>
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
bool negative = value < 0;
if (negative && value == INT_MIN) {
// Special case for INT_MIN to avoid overflow when negating
int32_t temp = value;
int i = 0;
while (temp != 0) {
buf[i++] = '0' - (temp % 10);
temp /= 10;
}
buf[i] = '\0';
// Reverse the string
for (int j = 0; j < i/2; j++) {
char c = buf[j];
buf[j] = buf[i-1-j];
buf[i-1-j] = c;
}
buf[i] = '\0';
return;
}
int32_t abs_value = negative ? -value : value;
int i = 0;
while (abs_value != 0) {
buf[i++] = '0' + (abs_value % 10);
abs_value /= 10;
}
if (negative) {
buf[i++] = '-';
}
// Reverse the string
for (int j = 0; j < i/2; j++) {
char c = buf[j];
buf[j] = buf[i-1-j];
buf[i-1-j] = c;
}
buf[i] = '\0';
}#include <stdint.h>
#include <stdbool.h>
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
bool negative = value < 0;
if (negative && value == INT_MIN) {
// Special case for INT_MIN to avoid overflow when negating
int32_t temp = value;
int i = 0;
while (temp != 0) {
buf[i++] = '0' - (temp % 10);
temp /= 10;
}
buf[i] = '\0';
// Reverse the string
for (int j = 0; j < i/2; j++) {
char c = buf[j];
buf[j] = buf[i-1-j];
buf[i-1-j] = c;
}
buf[i] = '\0';
return;
}
int32_t abs_value = negative ? -value : value;
int i = 0;
while (abs_value != 0) {
buf[i++] = '0' + (abs_value % 10);
abs_value /= 10;
}
if (negative) {
buf[i++] = '-';
}
// Reverse the string
for (int j = 0; j < i/2; j++) {
char c = buf[j];
buf[j] = buf[i-1-j];
buf[i-1-j] = c;
}
buf[i] = '\0';
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdio.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (!line || idx < 0 || !out || cap == 0) return -1;
const char *p = line;
int current_idx = 0;
size_t out_pos = 0;
while (*p) {
// Skip leading whitespace
while (*p == ' ' || *p == '\t') p++;
// Check if we're at the right field
if (current_idx == idx) {
// Handle quoted field
if (*p == '"') {
p++; // Skip opening quote
while (*p) {
if (*p == '"') {
// Check for doubled quote
if (*(p + 1) == '"') {
if (out_pos < cap - 1) {
out[out_pos++] = '"';
}
p += 2; // Skip both quotes
} else {
// End of quoted field
p++; // Skip closing quote
break;
}
} else {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
p++;
}
}
} else {
// Unquoted field
while (*p && *p != ',') {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
p++;
}
}
out[out_pos] = '\0';
return (int)out_pos;
}
// Skip to next field
if (*p == '"') {
p++; // Skip opening quote
while (*p && *p != '"') p++;
if (*p == '"') p++; // Skip closing quote
} else {
while (*p && *p != ',') p++;
}
// Skip comma
if (*p == ',') p++;
current_idx++;
}
return -1; // Field index out of range
}#include <stdio.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (!line || idx < 0 || !out || cap == 0) return -1;
const char *p = line;
int current_idx = 0;
size_t out_pos = 0;
while (*p) {
// Skip leading whitespace
while (*p == ' ' || *p == '\t') p++;
// Check if we're at the right field
if (current_idx != idx) {
// Skip to next field
int in_quotes = 0;
while (*p) {
if (*p == '"') {
if (*(p + 1) == '"') {
p += 2; // Skip escaped quote
} else {
in_quotes = !in_quotes;
p++;
}
} else if (*p == ',' && !in_quotes) {
break;
} else {
p++;
}
}
if (*p == ',') p++;
current_idx++;
continue;
}
// Found the target field
int in_quotes = 0;
int field_start = 0;
// Check if field starts with quote
while (*p == ' ' || *p == '\t') p++;
if (*p == '"') {
in_quotes = 1;
p++;
field_start = 1;
}
// Extract field content
while (*p) {
if (*p == '"') {
if (*(p + 1) == '"') {
// Escaped quote
if (out_pos < cap - 1) {
out[out_pos++] = '"';
}
p += 2;
} else {
// End of quoted field
if (in_quotes) {
in_quotes = 0;
p++;
break;
} else {
// Unexpected quote
if (out_pos < cap - 1) {
out[out_pos++] = '"';
}
p++;
}
}
} else if (*p == ',' && !in_quotes) {
// End of field
break;
} else {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
p++;
}
}
// Add null terminator
out[out_pos] = '\0';
return (int)out_pos;
}
// Field not found
return -1;
}#include <stdio.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (!line || idx < 0 || !out || cap == 0) return -1;
const char *p = line;
int current_idx = 0;
size_t out_pos = 0;
while (*p) {
// Skip leading whitespace
while (*p == ' ' || *p == '\t') p++;
// Check if we're at the right field
if (current_idx != idx) {
// Skip to next field
int in_quotes = 0;
while (*p) {
if (*p == '"') {
if (*(p + 1) == '"') {
p += 2; // Skip escaped quote
} else {
in_quotes = !in_quotes;
p++;
}
} else if (*p == ',' && !in_quotes) {
break;
} else {
p++;
}
}
if (*p == ',') p++;
current_idx++;
continue;
}
// Found the target field
int in_quotes = 0;
int field_start = 0;
// Check if field starts with quote
while (*p == ' ' || *p == '\t') p++;
if (*p == '"') {
in_quotes = 1;
p++;
field_start = 1;
}
// Extract field content
while (*p) {
if (*p == '"') {
if (*(p + 1) == '"') {
// Escaped quote
if (out_pos < cap - 1) {
out[out_pos++] = '"';
}
p += 2;
} else {
// End of quoted field
if (in_quotes) {
in_quotes = 0;
p++;
break;
} else {
// Unexpected quote
if (out_pos < cap - 1) {
out[out_pos++] = '"';
}
p++;
}
}
} else if (*p == ',' && !in_quotes) {
// End of field
break;
} else {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
p++;
}
}
// Add null terminator
out[out_pos] = '\0';
return (int)out_pos;
}
// Field not found
return -1;
}Served via llama.cpp CUDA · — tok/s · — GB RAM · coding total 93/126
Implement in C11: `int count_words(const char *s)`. Counts words in s. Words are separated by one or more spaces; leading/trailing spaces possible. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
int count_words(const char *s) {
int count = 0;
int in_word = 0;
if (s == NULL) {
return 0;
}
while (*s != '\0') {
if (isspace((unsigned char)*s)) {
in_word = 0;
} else {
if (!in_word) {
count++;
in_word = 1;
}
}
s++;
}
return count;
}#include <ctype.h>
int count_words(const char *s) {
int count = 0;
int in_word = 0;
if (s == NULL) {
return 0;
}
while (*s != '\0') {
if (isspace((unsigned char)*s)) {
in_word = 0;
} else {
if (!in_word) {
count++;
in_word = 1;
}
}
s++;
}
return count;
}#include <ctype.h>
int count_words(const char *s) {
int count = 0;
int in_word = 0;
if (s == NULL) {
return 0;
}
while (*s != '\0') {
if (isspace((unsigned char)*s)) {
in_word = 0;
} else {
if (!in_word) {
count++;
in_word = 1;
}
}
s++;
}
return count;
}Implement in C11: `void rot13(char *s)`. Applies ROT13 to s in place: letters rotate by 13, all other characters unchanged. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
void rot13(char *s) {
if (s == NULL) return;
for (; *s != '\0'; ++s) {
if (isalpha((unsigned char)*s)) {
char base = isupper((unsigned char)*s) ? 'A' : 'a';
*s = (char)(((*s - base + 13) % 26) + base);
}
}
}#include <ctype.h>
void rot13(char *s) {
if (s == NULL) return;
for (; *s != '\0'; ++s) {
if (isalpha((unsigned char)*s)) {
char base = isupper((unsigned char)*s) ? 'A' : 'a';
*s = (char)(((*s - base + 13) % 26) + base);
}
}
}#include <ctype.h>
void rot13(char *s) {
if (s == NULL) return;
for (; *s != '\0'; ++s) {
if (isalpha((unsigned char)*s)) {
char base = isupper((unsigned char)*s) ? 'A' : 'a';
*s = (char)(((*s - base + 13) % 26) + base);
}
}
}Implement in C11: `void trim(char *s)`. Strips leading and trailing whitespace (space, tab, newline) from s in place. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
void trim(char *s) {
if (s == NULL) return;
char *start = s;
char *end;
while (*start && isspace((unsigned char)*start)) {
start++;
}
if (*start == '\0') {
*s = '\0';
return;
}
end = start + strlen(start) - 1;
while (end > start && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
if (start != s) {
char *dst = s;
while (start <= end) {
*dst++ = *start++;
}
*dst = '\0';
}
}#include <ctype.h>
void trim(char *s) {
if (s == NULL) return;
char *start = s;
char *end;
while (*start && isspace((unsigned char)*start)) {
start++;
}
if (*start == '\0') {
*s = '\0';
return;
}
end = start + strlen(start) - 1;
while (end > start && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
if (start != s) {
char *dst = s;
while (start <= end) {
*dst++ = *start++;
}
*dst = '\0';
}
}#include <ctype.h>
void trim(char *s) {
if (s == NULL) return;
char *start = s;
char *end;
while (*start && isspace((unsigned char)*start)) {
start++;
}
if (*start == '\0') {
*s = '\0';
return;
}
end = start + strlen(start) - 1;
while (end > start && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
if (start != s) {
char *dst = s;
while (start <= end) {
*dst++ = *start++;
}
*dst = '\0';
}
}Implement in C11: `int atoi_strict(const char *s, int *out)`. Parses an optionally negative decimal integer from s into out. Returns 0 on success, -1 if s is not exactly one valid integer (empty, junk, trailing chars). No overflow tests. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
// Skip leading whitespace is NOT allowed for "strict" parsing of "exactly one integer"
// unless the integer itself starts with whitespace? No, standard atoi skips whitespace.
// However, "strict" usually implies no leading whitespace unless specified.
// Let's assume standard integer format: optional sign, then digits.
// If there is leading whitespace, it's "junk" before the number -> fail.
if (*p == '\0') {
return -1; // Empty string
}
int sign = 1;
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
// Must have at least one digit
if (!isdigit((unsigned char)*p)) {
return -1;
}
long long val = 0;
while (isdigit((unsigned char)*p)) {
val = val * 10 + (*p - '0');
p++;
}
// Check for trailing characters
if (*p != '\0') {
return -1;
}
// Apply sign
if (sign == -1) {
val = -val;
}
*out = (int)val;
return 0;
}#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
// Skip leading whitespace is NOT allowed for "strict" parsing of "exactly one integer"
// unless the integer itself starts with whitespace? No, standard atoi skips whitespace.
// However, "strict" usually implies no leading whitespace unless specified.
// Let's assume standard integer format: optional sign, then digits.
// If there is leading whitespace, it's "junk" before the number -> fail.
if (*p == '\0') {
return -1; // Empty string
}
int sign = 1;
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
// Must have at least one digit
if (!isdigit((unsigned char)*p)) {
return -1;
}
long long val = 0;
while (isdigit((unsigned char)*p)) {
val = val * 10 + (*p - '0');
p++;
}
// Check for trailing characters
if (*p != '\0') {
return -1;
}
// Apply sign
if (sign == -1) {
val = -val;
}
*out = (int)val;
return 0;
}#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
// Skip leading whitespace is NOT allowed for "strict" parsing of "exactly one integer"
// unless the integer itself starts with whitespace? No, standard atoi skips whitespace.
// However, "strict" usually implies no leading whitespace unless specified.
// Let's assume standard integer format: optional sign, then digits.
// If there is leading whitespace, it's "junk" before the number -> fail.
if (*p == '\0') {
return -1; // Empty string
}
int sign = 1;
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
// Must have at least one digit
if (!isdigit((unsigned char)*p)) {
return -1;
}
long long val = 0;
while (isdigit((unsigned char)*p)) {
val = val * 10 + (*p - '0');
p++;
}
// Check for trailing characters
if (*p != '\0') {
return -1;
}
// Apply sign
if (sign == -1) {
val = -val;
}
*out = (int)val;
return 0;
}Implement in C11: `int glob_match(const char *pat, const char *str)`. Returns 1 if str matches the shell-style pattern pat, else 0. '*' matches any sequence (including empty), '?' matches exactly one character. All other characters match literally. The whole string must match. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdbool.h>
int glob_match(const char *pat, const char *str) {
const char *star_pat = NULL;
const char *star_str = NULL;
while (*str != '\0') {
if (*pat == '*') {
star_pat = pat;
star_str = str;
pat++;
} else if (*pat == '?' || *pat == *str) {
pat++;
str++;
} else if (star_pat != NULL) {
pat = star_pat + 1;
str = star_str + 1;
star_str = str;
} else {
return 0;
}
}
while (*pat == '*') {
pat++;
}
return (*pat == '\0');
}#include <stdbool.h>
int glob_match(const char *pat, const char *str) {
const char *star_pat = NULL;
const char *star_str = NULL;
while (*str != '\0') {
if (*pat == '*') {
star_pat = pat;
star_str = str;
pat++;
} else if (*pat == '?' || *pat == *str) {
pat++;
str++;
} else if (star_pat != NULL) {
pat = star_pat + 1;
str = star_str + 1;
star_str = str;
} else {
return 0;
}
}
while (*pat == '*') {
pat++;
}
return (*pat == '\0');
}#include <stdbool.h>
int glob_match(const char *pat, const char *str) {
const char *star_pat = NULL;
const char *star_str = NULL;
while (*str != '\0') {
if (*pat == '*') {
star_pat = pat;
star_str = str;
pat++;
} else if (*pat == '?' || *pat == *str) {
pat++;
str++;
} else if (star_pat != NULL) {
pat = star_pat + 1;
str = star_str + 1;
star_str = str;
} else {
return 0;
}
}
while (*pat == '*') {
pat++;
}
return (*pat == '\0');
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
```c
#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (cap == 0) return -1;
int current_idx = 0;
const char *p = line;
size_t out_len = 0;
int in_quotes = 0;
while (*p != '\0') {
if (current_idx == idx) {
// We are inside the target field
if (*p == '"') {
if (!in_quotes) {
// Start of quoted field
in_quotes = 1;
p++;
continue;
} else {
// Inside quoted field
if (*(p + 1) == '"') {
// Escaped quote
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = 0;
p++;
continue;
}
}
} else if (*p == ',' && !in_quotes) {
// End of field
break;
} else {
// Regular character
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
} else {
// Skipping previous fields
if (*p == '"') {
if (!in_quotes) {
in_quotes = 1;
} else {
if (*(p + 1) == '"') {
p += 2;
continue;
} else {
in_quotes = 0;
}
}
} else if (*p == ',' && !in_quotes) {
current_idx++;
}
p++;
}
}
// Check if we found the field
if (current_idx != idx) {
// If we reached end of string and current_idx matches, it's the last field
if (current_idx == idx && out_len > 0) {
// Already processed
} else if (current_idx == idx && out_len == 0 && *p == '\0') {
// Empty last field
} else {
return -1;
}
}
// Re-scan to get exact length if we were in the target field
// Actually, the logic above processes the target field.
// But we need to handle the case where the field is empty or we just finished it.
// Let's refine the logic to be more robust.
// Reset and do a cleaner pass
p = line;
current_idx = 0;
out_len = 0;
in_quotes = 0;
int found = 0;
while (*p != '\0') {
if (current_idx == idx) {
found = 1;
if (*p == '"') {
if (!in_quotes) {
in_quotes = 1;
p++;
continue;
} else {
if (*(p + 1) == '"') {
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
in_quotes = 0;
p++;
continue;
}
}
} else if (*p == ',' && !in_quotes) {
break;
} else {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
} else {
if (*p == '"') {
if (!in_quotes) {
in_quotes = 1;
} else {
if (*(p + 1) == '"') {
p += 2;
continue;
} else {
in_quotes = 0;
}
}
} else if (*p == ',' && !in_quotes) {
current_idx++;
}
p++;
}
}
if (!found) {
// Check if it's the last field (empty or not)
if (current_idx == idx) {
found = 1;
}
}
if (!found) {
return -1```c
#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (cap == 0) return -1;
int current_idx = 0;
const char *p = line;
size_t out_len = 0;
int in_quotes = 0;
while (*p != '\0') {
if (current_idx == idx) {
// We are inside the target field
if (*p == '"') {
if (!in_quotes) {
// Start of quoted field
in_quotes = 1;
p++;
continue;
} else {
// Inside quoted field
if (*(p + 1) == '"') {
// Escaped quote
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = 0;
p++;
continue;
}
}
} else if (*p == ',' && !in_quotes) {
// End of field
break;
} else {
// Regular character
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
} else {
// Skipping previous fields
if (*p == '"') {
if (!in_quotes) {
in_quotes = 1;
} else {
if (*(p + 1) == '"') {
p += 2;
continue;
} else {
in_quotes = 0;
}
}
} else if (*p == ',' && !in_quotes) {
current_idx++;
}
p++;
}
}
// Check if we found the field
if (current_idx != idx) {
// If we reached end of string and current_idx matches, it's the last field
if (current_idx == idx && out_len > 0) {
// Already processed
} else if (current_idx == idx && out_len == 0 && *p == '\0') {
// Empty last field
} else {
return -1;
}
}
// Re-scan to get exact length if we were in the target field
// Actually, the logic above processes the target field.
// But we need to handle the case where the field is empty or we just finished it.
// Let's refine the logic to be more robust.
// Reset and do a cleaner pass
p = line;
current_idx = 0;
out_len = 0;
in_quotes = 0;
int found = 0;
while (*p != '\0') {
if (current_idx == idx) {
found = 1;
if (*p == '"') {
if (!in_quotes) {
in_quotes = 1;
p++;
continue;
} else {
if (*(p + 1) == '"') {
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
in_quotes = 0;
p++;
continue;
}
}
} else if (*p == ',' && !in_quotes) {
break;
} else {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
} else {
if (*p == '"') {
if (!in_quotes) {
in_quotes = 1;
} else {
if (*(p + 1) == '"') {
p += 2;
continue;
} else {
in_quotes = 0;
}
}
} else if (*p == ',' && !in_quotes) {
current_idx++;
}
p++;
}
}
if (!found) {
// Check if it's the last field (empty or not)
if (current_idx == idx) {
found = 1;
}
}
if (!found) {
return -1```c
#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (cap == 0) return -1;
int current_idx = 0;
const char *p = line;
size_t out_len = 0;
int in_quotes = 0;
while (*p != '\0') {
if (current_idx == idx) {
// We are inside the target field
if (*p == '"') {
if (!in_quotes) {
// Start of quoted field
in_quotes = 1;
p++;
continue;
} else {
// Inside quoted field
if (*(p + 1) == '"') {
// Escaped quote
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = 0;
p++;
continue;
}
}
} else if (*p == ',' && !in_quotes) {
// End of field
break;
} else {
// Regular character
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
} else {
// Skipping previous fields
if (*p == '"') {
if (!in_quotes) {
in_quotes = 1;
} else {
if (*(p + 1) == '"') {
p += 2;
continue;
} else {
in_quotes = 0;
}
}
} else if (*p == ',' && !in_quotes) {
current_idx++;
}
p++;
}
}
// Check if we found the field
if (current_idx != idx) {
// If we reached end of string and current_idx matches, it's the last field
if (current_idx == idx && out_len > 0) {
// Already processed
} else if (current_idx == idx && out_len == 0 && *p == '\0') {
// Empty last field
} else {
return -1;
}
}
// Re-scan to get exact length if we were in the target field
// Actually, the logic above processes the target field.
// But we need to handle the case where the field is empty or we just finished it.
// Let's refine the logic to be more robust.
// Reset and do a cleaner pass
p = line;
current_idx = 0;
out_len = 0;
in_quotes = 0;
int found = 0;
while (*p != '\0') {
if (current_idx == idx) {
found = 1;
if (*p == '"') {
if (!in_quotes) {
in_quotes = 1;
p++;
continue;
} else {
if (*(p + 1) == '"') {
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
in_quotes = 0;
p++;
continue;
}
}
} else if (*p == ',' && !in_quotes) {
break;
} else {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
} else {
if (*p == '"') {
if (!in_quotes) {
in_quotes = 1;
} else {
if (*(p + 1) == '"') {
p += 2;
continue;
} else {
in_quotes = 0;
}
}
} else if (*p == ',' && !in_quotes) {
current_idx++;
}
p++;
}
}
if (!found) {
// Check if it's the last field (empty or not)
if (current_idx == idx) {
found = 1;
}
}
if (!found) {
return -1Served via llama.cpp CUDA · — tok/s · — GB RAM · coding total 93/126
Implement in C11: `void reverse_string(char *s)`. Reverses s in place. Reply with only a C code block. No main function, no tests, no explanation.
#include <stddef.h>
void reverse_string(char *s) {
if (s == NULL) {
return;
}
size_t len = 0;
while (s[len] != '\0') {
len++;
}
size_t left = 0;
size_t right = len - 1;
while (left < right) {
char temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--;
}
}#include <stddef.h>
void reverse_string(char *s) {
if (s == NULL) {
return;
}
size_t len = 0;
while (s[len] != '\0') {
len++;
}
size_t left = 0;
size_t right = len - 1;
while (left < right) {
char temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--;
}
}#include <stddef.h>
void reverse_string(char *s) {
if (s == NULL) {
return;
}
size_t len = 0;
while (s[len] != '\0') {
len++;
}
size_t left = 0;
size_t right = len - 1;
while (left < right) {
char temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--;
}
}Implement in C11: `int count_words(const char *s)`. Counts words in s. Words are separated by one or more spaces; leading/trailing spaces possible. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdbool.h>
int count_words(const char *s) {
int count = 0;
bool in_word = false;
if (s == NULL) {
return 0;
}
while (*s != '\0') {
if (*s != ' ') {
if (!in_word) {
count++;
in_word = true;
}
} else {
in_word = false;
}
s++;
}
return count;
}#include <stdbool.h>
int count_words(const char *s) {
int count = 0;
bool in_word = false;
if (s == NULL) {
return 0;
}
while (*s != '\0') {
if (*s != ' ') {
if (!in_word) {
count++;
in_word = true;
}
} else {
in_word = false;
}
s++;
}
return count;
}#include <stdbool.h>
int count_words(const char *s) {
int count = 0;
bool in_word = false;
if (s == NULL) {
return 0;
}
while (*s != '\0') {
if (*s != ' ') {
if (!in_word) {
count++;
in_word = true;
}
} else {
in_word = false;
}
s++;
}
return count;
}Implement in C11: `void itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
```c
#include <limits.h>
#include <stdio.h>
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
char *p = buf;
int is_negative = 0;
if (value < 0) {
is_negative = 1;
if (value == INT_MIN) {
// Handle INT_MIN specifically to avoid overflow on negation
*p++ = '-';
value = -INT_MAX - 1; // This is still negative, but we process digits directly
// We will process the absolute value logic manually for INT_MIN
// INT_MIN is -2147483648. Digits: 2,1,4,7,4,8,3,6,4,8
// Since we can't negate it safely, we treat it as a special case or use unsigned math.
// A cleaner approach for INT_MIN:
// Convert to unsigned, negate, then handle sign.
// But standard int negation of INT_MIN is undefined behavior.
// Let's restart the logic for INT_MIN to be safe.
} else {
value = -value;
}
}
// Re-evaluate for INT_MIN safety
if (is_negative && value == INT_MIN) {
// This path is unreachable if we negated above, so we need a different structure.
// Let's rewrite the logic to handle INT_MIN without negating it first.
}
}
// Corrected implementation below
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
char *p = buf;
int is_negative = 0;
if (value < 0) {
is_negative = 1;
if (value == INT_MIN) {
// Special handling for INT_MIN to avoid undefined behavior on negation
// INT_MIN is -2147483648
// We can work with the absolute value by using unsigned arithmetic
// or by processing the number as a string of digits directly.
// A common trick: value = -value is UB.
// Instead, we can do: unsigned int u = (unsigned int)(-value) is UB.
// Correct way: unsigned int u = (unsigned int)(value); // This is 2147483648
// But wait, (unsigned int)(INT_MIN) is 2147483648.
// So we can just use the unsigned representation.
// However, the sign is already handled.
// Let's just use the unsigned cast for the magnitude.
// But wait, if value is INT_MIN, -value is UB.
// So we must not do value = -value.
// We will use a temporary unsigned variable.
unsigned int u = (unsigned int)value; // This is 2147483648
// Now we convert u to string, but we need to be careful.
// Actually, (unsigned int)INT_MIN is 2147483648.
// So we can just convert u.
// But wait, the logic below expects a positive int.
// Let's just use the unsigned variable for the loop.
// We need to set value to a positive representation for the loop.
// But we can't assign u to value because value is int.
// Let's change the loop to work on unsigned.
// Actually, simpler:
// If value is INT_MIN, we know the string is "-2147483648".
// But let's do it generically.
// We can use a temporary unsigned int.
unsigned int temp = (unsigned int)value; // 2147483648
// Now we convert temp to string.
// But wait, the loop below uses 'value'.
// Let's refactor to use a local variable for the magnitude.
// Let's restart the function with a cleaner approach.
}
}
}
// Final Correct Implementation
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';```c
#include <limits.h>
#include <stdio.h>
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
char *p = buf;
int is_negative = 0;
if (value < 0) {
is_negative = 1;
if (value == INT_MIN) {
// Handle INT_MIN specifically to avoid overflow on negation
*p++ = '-';
value = -INT_MAX - 1; // This is still negative, but we process digits directly
// We will process the absolute value logic manually for INT_MIN
// INT_MIN is -2147483648. Digits: 2,1,4,7,4,8,3,6,4,8
// Since we can't negate it safely, we treat it as a special case or use unsigned math.
// A cleaner approach for INT_MIN:
// Convert to unsigned, negate, then handle sign.
// But standard int negation of INT_MIN is undefined behavior.
// Let's restart the logic for INT_MIN to be safe.
} else {
value = -value;
}
}
// Re-evaluate for INT_MIN safety
if (is_negative && value == INT_MIN) {
// This path is unreachable if we negated above, so we need a different structure.
// Let's rewrite the logic to handle INT_MIN without negating it first.
}
}
// Corrected implementation below
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
char *p = buf;
int is_negative = 0;
if (value < 0) {
is_negative = 1;
if (value == INT_MIN) {
// Special handling for INT_MIN to avoid undefined behavior on negation
// INT_MIN is -2147483648
// We can work with the absolute value by using unsigned arithmetic
// or by processing the number as a string of digits directly.
// A common trick: value = -value is UB.
// Instead, we can do: unsigned int u = (unsigned int)(-value) is UB.
// Correct way: unsigned int u = (unsigned int)(value); // This is 2147483648
// But wait, (unsigned int)(INT_MIN) is 2147483648.
// So we can just use the unsigned representation.
// However, the sign is already handled.
// Let's just use the unsigned cast for the magnitude.
// But wait, if value is INT_MIN, -value is UB.
// So we must not do value = -value.
// We will use a temporary unsigned variable.
unsigned int u = (unsigned int)value; // This is 2147483648
// Now we convert u to string, but we need to be careful.
// Actually, (unsigned int)INT_MIN is 2147483648.
// So we can just convert u.
// But wait, the logic below expects a positive int.
// Let's just use the unsigned variable for the loop.
// We need to set value to a positive representation for the loop.
// But we can't assign u to value because value is int.
// Let's change the loop to work on unsigned.
// Actually, simpler:
// If value is INT_MIN, we know the string is "-2147483648".
// But let's do it generically.
// We can use a temporary unsigned int.
unsigned int temp = (unsigned int)value; // 2147483648
// Now we convert temp to string.
// But wait, the loop below uses 'value'.
// Let's refactor to use a local variable for the magnitude.
// Let's restart the function with a cleaner approach.
}
}
}
// Final Correct Implementation
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';```c
#include <limits.h>
#include <stdio.h>
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
char *p = buf;
int is_negative = 0;
if (value < 0) {
is_negative = 1;
if (value == INT_MIN) {
// Handle INT_MIN specifically to avoid overflow on negation
*p++ = '-';
value = -INT_MAX - 1; // This is still negative, but we process digits directly
// We will process the absolute value logic manually for INT_MIN
// INT_MIN is -2147483648. Digits: 2,1,4,7,4,8,3,6,4,8
// Since we can't negate it safely, we treat it as a special case or use unsigned math.
// A cleaner approach for INT_MIN:
// Convert to unsigned, negate, then handle sign.
// But standard int negation of INT_MIN is undefined behavior.
// Let's restart the logic for INT_MIN to be safe.
} else {
value = -value;
}
}
// Re-evaluate for INT_MIN safety
if (is_negative && value == INT_MIN) {
// This path is unreachable if we negated above, so we need a different structure.
// Let's rewrite the logic to handle INT_MIN without negating it first.
}
}
// Corrected implementation below
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
char *p = buf;
int is_negative = 0;
if (value < 0) {
is_negative = 1;
if (value == INT_MIN) {
// Special handling for INT_MIN to avoid undefined behavior on negation
// INT_MIN is -2147483648
// We can work with the absolute value by using unsigned arithmetic
// or by processing the number as a string of digits directly.
// A common trick: value = -value is UB.
// Instead, we can do: unsigned int u = (unsigned int)(-value) is UB.
// Correct way: unsigned int u = (unsigned int)(value); // This is 2147483648
// But wait, (unsigned int)(INT_MIN) is 2147483648.
// So we can just use the unsigned representation.
// However, the sign is already handled.
// Let's just use the unsigned cast for the magnitude.
// But wait, if value is INT_MIN, -value is UB.
// So we must not do value = -value.
// We will use a temporary unsigned variable.
unsigned int u = (unsigned int)value; // This is 2147483648
// Now we convert u to string, but we need to be careful.
// Actually, (unsigned int)INT_MIN is 2147483648.
// So we can just convert u.
// But wait, the logic below expects a positive int.
// Let's just use the unsigned variable for the loop.
// We need to set value to a positive representation for the loop.
// But we can't assign u to value because value is int.
// Let's change the loop to work on unsigned.
// Actually, simpler:
// If value is INT_MIN, we know the string is "-2147483648".
// But let's do it generically.
// We can use a temporary unsigned int.
unsigned int temp = (unsigned int)value; // 2147483648
// Now we convert temp to string.
// But wait, the loop below uses 'value'.
// Let's refactor to use a local variable for the magnitude.
// Let's restart the function with a cleaner approach.
}
}
}
// Final Correct Implementation
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';Implement in C11: `void trim(char *s)`. Strips leading and trailing whitespace (space, tab, newline) from s in place. Reply with only a C code block. No main function, no tests, no explanation.
#include <stddef.h>
#include <ctype.h>
void trim(char *s) {
if (s == NULL) return;
char *start = s;
char *end;
while (isspace((unsigned char)*start)) {
start++;
}
if (*start == '\0') {
*s = '\0';
return;
}
end = s + strlen(s) - 1;
while (end > start && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
if (start != s) {
memmove(s, start, (size_t)(end - start + 1));
}
}#include <stddef.h>
#include <ctype.h>
void trim(char *s) {
if (s == NULL) return;
char *start = s;
char *end;
while (isspace((unsigned char)*start)) {
start++;
}
if (*start == '\0') {
*s = '\0';
return;
}
end = s + strlen(s) - 1;
while (end > start && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
if (start != s) {
memmove(s, start, (size_t)(end - start + 1));
}
}#include <stddef.h>
#include <ctype.h>
void trim(char *s) {
if (s == NULL) return;
char *start = s;
char *end;
while (isspace((unsigned char)*start)) {
start++;
}
if (*start == '\0') {
*s = '\0';
return;
}
end = s + strlen(s) - 1;
while (end > start && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
if (start != s) {
memmove(s, start, (size_t)(end - start + 1));
}
}Implement in C11: `int cmp_desc(const void *a, const void *b)`. qsort comparator ordering int values descending. Must be safe against overflow. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdlib.h>
int cmp_desc(const void *a, const void *b) {
const int *ia = (const int *)a;
const int *ib = (const int *)b;
int va = *ia;
int vb = *ib;
if (va > vb) {
return (va < 0 && vb > 0) ? 1 : (va - vb > 0 ? 1 : -1);
} else if (va < vb) {
return (va > 0 && vb < 0) ? -1 : (va - vb < 0 ? -1 : 1);
}
return 0;
}#include <stdlib.h>
int cmp_desc(const void *a, const void *b) {
const int *ia = (const int *)a;
const int *ib = (const int *)b;
int va = *ia;
int vb = *ib;
if (va > vb) {
return (va < 0 && vb > 0) ? 1 : (va - vb > 0 ? 1 : -1);
} else if (va < vb) {
return (va > 0 && vb < 0) ? -1 : (va - vb < 0 ? -1 : 1);
}
return 0;
}#include <stdlib.h>
int cmp_desc(const void *a, const void *b) {
const int *ia = (const int *)a;
const int *ib = (const int *)b;
int va = *ia;
int vb = *ib;
if (va > vb) {
return (va < 0 && vb > 0) ? 1 : (va - vb > 0 ? 1 : -1);
} else if (va < vb) {
return (va > 0 && vb < 0) ? -1 : (va - vb < 0 ? -1 : 1);
}
return 0;
}Implement in C11: `int atoi_strict(const char *s, int *out)`. Parses an optionally negative decimal integer from s into out. Returns 0 on success, -1 if s is not exactly one valid integer (empty, junk, trailing chars). No overflow tests. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
#include <stddef.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
int sign = 1;
int val = 0;
/* Skip leading whitespace */
while (isspace((unsigned char)*p)) {
p++;
}
/* Check for empty string after whitespace */
if (*p == '\0') {
return -1;
}
/* Handle optional sign */
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
/* Check for empty string after sign */
if (*p == '\0') {
return -1;
}
/* Parse digits */
while (isdigit((unsigned char)*p)) {
val = val * 10 + (*p - '0');
p++;
}
/* Ensure no trailing characters */
if (*p != '\0') {
return -1;
}
*out = sign * val;
return 0;
}#include <ctype.h>
#include <stddef.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
int sign = 1;
int val = 0;
/* Skip leading whitespace */
while (isspace((unsigned char)*p)) {
p++;
}
/* Check for empty string after whitespace */
if (*p == '\0') {
return -1;
}
/* Handle optional sign */
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
/* Check for empty string after sign */
if (*p == '\0') {
return -1;
}
/* Parse digits */
while (isdigit((unsigned char)*p)) {
val = val * 10 + (*p - '0');
p++;
}
/* Ensure no trailing characters */
if (*p != '\0') {
return -1;
}
*out = sign * val;
return 0;
}#include <ctype.h>
#include <stddef.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
int sign = 1;
int val = 0;
/* Skip leading whitespace */
while (isspace((unsigned char)*p)) {
p++;
}
/* Check for empty string after whitespace */
if (*p == '\0') {
return -1;
}
/* Handle optional sign */
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
/* Check for empty string after sign */
if (*p == '\0') {
return -1;
}
/* Parse digits */
while (isdigit((unsigned char)*p)) {
val = val * 10 + (*p - '0');
p++;
}
/* Ensure no trailing characters */
if (*p != '\0') {
return -1;
}
*out = sign * val;
return 0;
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
#include <stddef.h>
#include <stdbool.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int current_idx = 0;
bool in_quotes = false;
bool field_started = false;
size_t out_len = 0;
while (*p != '\0') {
char c = *p;
if (!in_quotes) {
if (c == '"') {
in_quotes = true;
field_started = true;
p++;
continue;
} else if (c == ',') {
if (field_started) {
current_idx++;
field_started = false;
}
if (current_idx == idx) {
// Found the target field, but it's empty
if (cap > 0) out[0] = '\0';
return 0;
}
p++;
continue;
} else {
field_started = true;
}
}
if (current_idx == idx) {
if (in_quotes) {
if (c == '"') {
// Check for escaped quote ""
if (*(p + 1) == '"') {
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = false;
p++;
// Check if immediately followed by comma or end
if (*p == ',' || *p == '\0') {
if (out_len < cap - 1) {
out[out_len] = '\0';
}
return (int)out_len;
}
// RFC 4180: quotes must be followed by comma or end
// If not, it's malformed, but we treat as end of field for robustness
in_quotes = false;
p++;
if (out_len < cap - 1) {
out[out_len] = '\0';
}
return (int)out_len;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = c;
}
p++;
continue;
}
} else {
// Unquoted field
if (c == ',') {
if (out_len < cap - 1) {
out[out_len] = '\0';
}
return (int)out_len;
}
if (out_len < cap - 1) {
out[out_len++] = c;
}
p++;
continue;
}
} else {
// Skipping fields before the target
if (in_quotes) {
if (c == '"') {
if (*(p + 1) == '"') {
p += 2;
} else {
in_quotes = false;
p++;
}
} else {
p++;
}
} else {
if (c == ',') {
current_idx++;
field_started = false;
p++;
} else {
p++;
}
}
}
}
// End of line reached
if (current_idx == idx) {
if (out_len < cap - 1) {
out[out_len] = '\0';
}
return (int)out_len;
}
return -1;
}#include <stddef.h>
#include <stdbool.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int current_idx = 0;
bool in_quotes = false;
bool field_started = false;
size_t out_len = 0;
while (*p != '\0') {
char c = *p;
if (!in_quotes) {
if (c == '"') {
in_quotes = true;
field_started = true;
p++;
continue;
} else if (c == ',') {
if (field_started) {
current_idx++;
field_started = false;
}
if (current_idx == idx) {
// Found the target field, but it's empty
if (cap > 0) out[0] = '\0';
return 0;
}
p++;
continue;
} else {
field_started = true;
}
}
if (current_idx == idx) {
if (in_quotes) {
if (c == '"') {
// Check for escaped quote ""
if (*(p + 1) == '"') {
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = false;
p++;
// Check if immediately followed by comma or end
if (*p == ',' || *p == '\0') {
if (out_len < cap - 1) {
out[out_len] = '\0';
}
return (int)out_len;
}
// RFC 4180: quotes must be followed by comma or end
// If not, it's malformed, but we treat as end of field for robustness
in_quotes = false;
p++;
if (out_len < cap - 1) {
out[out_len] = '\0';
}
return (int)out_len;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = c;
}
p++;
continue;
}
} else {
// Unquoted field
if (c == ',') {
if (out_len < cap - 1) {
out[out_len] = '\0';
}
return (int)out_len;
}
if (out_len < cap - 1) {
out[out_len++] = c;
}
p++;
continue;
}
} else {
// Skipping fields before the target
if (in_quotes) {
if (c == '"') {
if (*(p + 1) == '"') {
p += 2;
} else {
in_quotes = false;
p++;
}
} else {
p++;
}
} else {
if (c == ',') {
current_idx++;
field_started = false;
p++;
} else {
p++;
}
}
}
}
// End of line reached
if (current_idx == idx) {
if (out_len < cap - 1) {
out[out_len] = '\0';
}
return (int)out_len;
}
return -1;
}#include <stddef.h>
#include <stdbool.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int current_idx = 0;
bool in_quotes = false;
bool field_started = false;
size_t out_len = 0;
while (*p != '\0') {
char c = *p;
if (!in_quotes) {
if (c == '"') {
in_quotes = true;
field_started = true;
p++;
continue;
} else if (c == ',') {
if (field_started) {
current_idx++;
field_started = false;
}
if (current_idx == idx) {
// Found the target field, but it's empty
if (cap > 0) out[0] = '\0';
return 0;
}
p++;
continue;
} else {
field_started = true;
}
}
if (current_idx == idx) {
if (in_quotes) {
if (c == '"') {
// Check for escaped quote ""
if (*(p + 1) == '"') {
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = false;
p++;
// Check if immediately followed by comma or end
if (*p == ',' || *p == '\0') {
if (out_len < cap - 1) {
out[out_len] = '\0';
}
return (int)out_len;
}
// RFC 4180: quotes must be followed by comma or end
// If not, it's malformed, but we treat as end of field for robustness
in_quotes = false;
p++;
if (out_len < cap - 1) {
out[out_len] = '\0';
}
return (int)out_len;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = c;
}
p++;
continue;
}
} else {
// Unquoted field
if (c == ',') {
if (out_len < cap - 1) {
out[out_len] = '\0';
}
return (int)out_len;
}
if (out_len < cap - 1) {
out[out_len++] = c;
}
p++;
continue;
}
} else {
// Skipping fields before the target
if (in_quotes) {
if (c == '"') {
if (*(p + 1) == '"') {
p += 2;
} else {
in_quotes = false;
p++;
}
} else {
p++;
}
} else {
if (c == ',') {
current_idx++;
field_started = false;
p++;
} else {
p++;
}
}
}
}
// End of line reached
if (current_idx == idx) {
if (out_len < cap - 1) {
out[out_len] = '\0';
}
return (int)out_len;
}
return -1;
}Served via llama.cpp CUDA · — tok/s · — GB RAM · coding total 91/126
Implement in C11: `void reverse_string(char *s)`. Reverses s in place. Reply with only a C code block. No main function, no tests, no explanation.
void reverse_string(char *s) {
if (s == NULL) return;
char *start = s;
char *end = s;
while (*end != '\0') end++;
end--;
while (start < end) {
char tmp = *start;
*start = *end;
*end = tmp;
start++;
end--;
}
}void reverse_string(char *s) {
if (s == NULL) return;
char *start = s;
char *end = s;
while (*end != '\0') end++;
end--;
while (start < end) {
char tmp = *start;
*start = *end;
*end = tmp;
start++;
end--;
}
}void reverse_string(char *s) {
if (s == NULL) return;
char *start = s;
char *end = s;
while (*end != '\0') end++;
end--;
while (start < end) {
char tmp = *start;
*start = *end;
*end = tmp;
start++;
end--;
}
}Implement in C11: `void itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
#include <limits.h>
void itoa(int value, char *buf) {
char *p = buf;
int negative = 0;
if (value == 0) {
*p++ = '0';
*p = '\0';
return;
}
if (value == INT_MIN) {
negative = 1;
value = INT_MAX;
} else if (value < 0) {
negative = 1;
value = -value;
}
while (value > 0) {
*p++ = (char)('0' + (value % 10));
value /= 10;
}
if (negative) {
*p++ = '-';
}
*p = '\0';
/* Reverse the digits */
char *start = buf;
char *end = p - 1;
while (start < end) {
char tmp = *start;
*start++ = *end;
*end-- = tmp;
}
}#include <limits.h>
void itoa(int value, char *buf) {
char *p = buf;
int negative = 0;
if (value == 0) {
*p++ = '0';
*p = '\0';
return;
}
if (value == INT_MIN) {
*p++ = '-';
unsigned int u = (unsigned int)(-(value + 1)) + 1u;
negative = 1;
value = 0;
} else if (value < 0) {
*p++ = '-';
u = (unsigned int)(-value);
negative = 1;
value = 0;
}
if (value != 0) {
unsigned int u = (unsigned int)value;
char tmp[12];
char *t = tmp;
while (u > 0) {
*t++ = (char)('0' + (u % 10));
u /= 10;
}
while (t > tmp) {
*p++ = *--t;
}
}
*p = '\0';
}#include <limits.h>
void itoa(int value, char *buf) {
char *p = buf;
int negative = 0;
if (value == 0) {
*p++ = '0';
*p = '\0';
return;
}
if (value == INT_MIN) {
*p++ = '-';
unsigned int u = (unsigned int)(-(value + 1)) + 1u;
negative = 1;
value = 0;
} else if (value < 0) {
*p++ = '-';
u = (unsigned int)(-value);
negative = 1;
value = 0;
}
if (value != 0) {
unsigned int u = (unsigned int)value;
char tmp[12];
char *t = tmp;
while (u > 0) {
*t++ = (char)('0' + (u % 10));
u /= 10;
}
while (t > tmp) {
*p++ = *--t;
}
}
*p = '\0';
}Implement in C11: `void trim(char *s)`. Strips leading and trailing whitespace (space, tab, newline) from s in place. Reply with only a C code block. No main function, no tests, no explanation.
void trim(char *s) {
char *end;
while (*s == ' ' || *s == '\t' || *s == '\n')
s++;
if (*s == '\0')
return;
end = s + strlen(s) - 1;
while (end > s && (*end == ' ' || *end == '\t' || *end == '\n'))
end--;
end[1] = '\0';
}#include <ctype.h>
void trim(char *s) {
if (s == NULL) return;
char *start = s;
while (*start && isspace((unsigned char)*start)) {
start++;
}
if (*start == '\0') {
*s = '\0';
return;
}
char *end = start + strlen(start) - 1;
while (end >= start && isspace((unsigned char)*end)) {
end--;
}
end[1] = '\0';
if (start != s) {
char *dest = s;
while (*start) {
*dest++ = *start++;
}
*dest = '\0';
}
}#include <ctype.h>
void trim(char *s) {
if (s == NULL) return;
char *start = s;
while (*start && isspace((unsigned char)*start)) {
start++;
}
if (*start == '\0') {
*s = '\0';
return;
}
char *end = start + strlen(start) - 1;
while (end >= start && isspace((unsigned char)*end)) {
end--;
}
end[1] = '\0';
if (start != s) {
char *dest = s;
while (*start) {
*dest++ = *start++;
}
*dest = '\0';
}
}Implement in C11: `int atoi_strict(const char *s, int *out)`. Parses an optionally negative decimal integer from s into out. Returns 0 on success, -1 if s is not exactly one valid integer (empty, junk, trailing chars). No overflow tests. Reply with only a C code block. No main function, no tests, no explanation.
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) return -1;
const char *p = s;
int neg = 0;
if (*p == '-') {
neg = 1;
++p;
} else if (*p == '+') {
++p;
}
if (*p == '\0') return -1;
long val = 0;
while (*p >= '0' && *p <= '9') {
val = val * 10 + (*p - '0');
++p;
}
if (*p != '\0') return -1;
if (neg) val = -val;
*out = (int)val;
return 0;
}int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) return -1;
const char *p = s;
int neg = 0;
if (*p == '-') {
neg = 1;
++p;
} else if (*p == '+') {
++p;
}
if (*p == '\0') return -1;
long val = 0;
while (*p >= '0' && *p <= '9') {
val = val * 10 + (*p - '0');
++p;
}
if (*p != '\0') return -1;
if (neg) val = -val;
*out = (int)val;
return 0;
}Implement in C11: `int glob_match(const char *pat, const char *str)`. Returns 1 if str matches the shell-style pattern pat, else 0. '*' matches any sequence (including empty), '?' matches exactly one character. All other characters match literally. The whole string must match. Reply with only a C code block. No main function, no tests, no explanation.
int glob_match(const char *pat, const char *str) {
const char *p = pat, *s = str;
const char *star = NULL;
const char *mark = NULL;
while (*s) {
if (*p == '*') {
star = p++;
mark = s;
} else if (*p == '?' || *p == *s) {
p++;
s++;
} else if (star) {
p = star + 1;
s = ++mark;
} else {
return 0;
}
}
while (*p == '*') p++;
return *p == '\0';
}int glob_match(const char *pat, const char *str) {
const char *p = pat, *s = str;
const char *star = NULL;
const char *mark = NULL;
while (*s) {
if (*p == '*') {
star = p++;
mark = s;
} else if (*p == '?' || *p == *s) {
p++;
s++;
} else if (star) {
p = star + 1;
s = ++mark;
} else {
return 0;
}
}
while (*p == '*') p++;
return *p == '\0';
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) return -1;
if (idx < 0) return -1;
const char *p = line;
int cur = 0;
while (*p != '\0') {
if (*p == '"') {
/* quoted field */
++p;
size_t len = 0;
while (*p != '\0' && *p != '"') {
if (*p == '"') {
if (p[1] == '"') {
if (len + 1 < cap) out[len++] = '"';
p += 2;
} else {
++p; /* closing quote */
break;
}
} else {
if (len + 1 < cap) out[len++] = *p;
++p;
}
}
/* skip to end of field */
while (*p != '\0' && *p != ',') ++p;
if (*p == ',') ++p;
if (cur == idx) {
if (len + 1 < cap) out[len] = '\0';
else out[cap - 1] = '\0';
return (int)len;
}
++cur;
} else {
/* unquoted field */
const char *start = p;
while (*p != '\0' && *p != ',') ++p;
size_t len = (size_t)(p - start);
if (cur == idx) {
if (len >= cap) len = cap - 1;
for (size_t i = 0; i < len; ++i) out[i] = start[i];
out[len] = '\0';
return (int)len;
}
if (*p == ',') ++p;
++cur;
}
}
return -1;
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) return -1;
if (idx < 0) return -1;
const char *p = line;
int cur = 0;
while (*p != '\0') {
if (*p == '"') {
/* quoted field */
++p;
size_t len = 0;
while (*p != '\0' && *p != '"') {
if (*p == '"') {
if (p[1] == '"') {
if (len + 1 < cap) out[len++] = '"';
p += 2;
} else {
++p; /* closing quote */
break;
}
} else {
if (len + 1 < cap) out[len++] = *p;
++p;
}
}
/* skip to end of field */
while (*p != '\0' && *p != ',') ++p;
if (*p == ',') ++p;
if (cur == idx) {
if (len + 1 < cap) out[len] = '\0';
else out[cap - 1] = '\0';
return (int)len;
}
++cur;
} else {
/* unquoted field */
const char *start = p;
while (*p != '\0' && *p != ',') ++p;
size_t len = (size_t)(p - start);
if (cur == idx) {
if (len + 1 < cap) {
for (size_t i = 0; i < len; ++i) out[i] = start[i];
out[len] = '\0';
} else {
for (size_t i = 0; i < cap - 1; ++i) out[i] = start[i];
out[cap - 1] = '\0';
}
return (int)len;
}
if (*p == ',') ++p;
++cur;
}
}
return -1;
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) return -1;
if (idx < 0) return -1;
const char *p = line;
int cur = 0;
while (*p != '\0') {
if (*p == '"') {
/* quoted field */
++p;
size_t len = 0;
while (*p != '\0' && *p != '"') {
if (*p == '"') {
if (p[1] == '"') {
if (len + 1 < cap) out[len++] = '"';
p += 2;
} else {
++p; /* closing quote */
break;
}
} else {
if (len + 1 < cap) out[len++] = *p;
++p;
}
}
/* skip to end of field */
while (*p != '\0' && *p != ',') ++p;
if (*p == ',') ++p;
if (cur == idx) {
if (len + 1 < cap) out[len] = '\0';
else out[cap - 1] = '\0';
return (int)len;
}
++cur;
} else {
/* unquoted field */
const char *start = p;
while (*p != '\0' && *p != ',') ++p;
size_t len = (size_t)(p - start);
if (cur == idx) {
if (len + 1 < cap) {
for (size_t i = 0; i < len; ++i) out[i] = start[i];
out[len] = '\0';
} else {
for (size_t i = 0; i < cap - 1; ++i) out[i] = start[i];
out[cap - 1] = '\0';
}
return (int)len;
}
if (*p == ',') ++p;
++cur;
}
}
return -1;
}Served via llama.cpp CUDA · — tok/s · — GB RAM · coding total 87/126
Implement in C11: `void itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdbool.h>
#include <limits.h>
void itoa(int value, char *buf) {
if (buf == NULL) return;
bool is_negative = false;
if (value < 0) {
is_negative = true;
if (value == INT_MIN) {
value = -(value / 10);
buf[0] = '-';
buf[1] = '2';
buf[2] = '1';
buf[3] = '4';
buf[4] = '7';
buf[5] = '4';
buf[6] = '8';
buf[7] = '3';
buf[6] = '\0';
return;
}
value = -value;
}
char *ptr = buf;
if (is_negative) {
*ptr++ = '-';
}
char temp[12];
char *temp_ptr = temp;
do {
*temp_ptr++ = (char)(value % 10 + '0');
value /= 10;
} while (value > 0);
if (is_negative) {
*temp_ptr++ = '6';
*temp_ptr++ = '8';
}
do {
*ptr++ = *--temp_ptr;
} while (temp_ptr != temp);
*ptr = '\0';
}#include <stdbool.h>
#include <limits.h>
void itoa(int value, char *buf) {
if (buf == NULL) return;
bool is_negative = false;
if (value < 0) {
is_negative = true;
if (value == INT_MIN) {
value = -(value / 10);
buf[0] = '-';
buf[1] = '2';
buf += 2;
} else {
value = -value;
}
}
char *start = buf;
do {
*buf++ = '0' - value % 10;
value /= 10;
} while (value != 0);
if (is_negative) {
*buf++ = '-';
}
*buf = '\0';
char *end = buf - 1;
char *p = start;
while (p < end) {
char tmp = *p;
*p++ = *end;
*end-- = tmp;
}
}#include <stdbool.h>
#include <limits.h>
void itoa(int value, char *buf) {
if (buf == NULL) return;
bool is_negative = false;
if (value < 0) {
is_negative = true;
if (value == INT_MIN) {
value = -(value / 10);
buf[0] = '-';
buf[1] = '2';
buf += 2;
} else {
value = -value;
}
}
char *start = buf;
do {
*buf++ = '0' - value % 10;
value /= 10;
} while (value != 0);
if (is_negative) {
*buf++ = '-';
}
*buf = '\0';
char *end = buf - 1;
char *p = start;
while (p < end) {
char tmp = *p;
*p++ = *end;
*end-- = tmp;
}
}Implement in C11: `void rot13(char *s)`. Applies ROT13 to s in place: letters rotate by 13, all other characters unchanged. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
void rot13(char *s) {
if (s == NULL) return;
for (int i = 0; s[i] != '\0'; i++) {
char c = s[i];
if (isalpha(c)) {
char base = islower(c) ? 'a' : 'A';
c = (c - base + 13) % 26 + base;
}
s[i] = c;
}
}#include <ctype.h>
void rot13(char *s) {
if (s == NULL) return;
for (int i = 0; s[i] != '\0'; i++) {
char c = s[i];
if (isalpha(c)) {
char base = islower(c) ? 'a' : 'A';
c = (c - base + 13) % 26 + base;
}
s[i] = c;
}
}#include <ctype.h>
void rot13(char *s) {
if (s == NULL) return;
for (int i = 0; s[i] != '\0'; i++) {
char c = s[i];
if (isalpha(c)) {
char base = islower(c) ? 'a' : 'A';
c = (c - base + 13) % 26 + base;
}
s[i] = c;
}
}Implement in C11: `int parse_csv_ints(const char *s, int *out, int max)`. Parses comma-separated integers from s into out (at most max). Returns the count stored. Input is well-formed (digits, optional leading minus, commas). Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
#include <stdbool.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
bool negative = false;
int num = 0;
while (*s && count < max) {
if (*s == '-') {
negative = true;
s++;
} else if (*s == ',') {
if (negative) num = -num;
out[count++] = num;
num = 0;
negative = false;
s++;
} else {
num = num * 10 + (*s - '0');
s++;
}
}
if (count < max) {
if (negative) num = -num;
out[count++] = num;
}
return count;
}#include <ctype.h>
#include <stdbool.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
bool negative = false;
int num = 0;
while (*s && count < max) {
if (*s == '-') {
negative = true;
s++;
} else if (*s == ',') {
if (negative) num = -num;
out[count++] = num;
num = 0;
negative = false;
s++;
} else {
num = num * 10 + (*s - '0');
s++;
}
}
if (count < max) {
if (negative) num = -num;
out[count++] = num;
}
return count;
}#include <ctype.h>
#include <stdbool.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
bool negative = false;
int num = 0;
while (*s && count < max) {
if (*s == '-') {
negative = true;
s++;
} else if (*s == ',') {
if (negative) num = -num;
out[count++] = num;
num = 0;
negative = false;
s++;
} else {
num = num * 10 + (*s - '0');
s++;
}
}
if (count < max) {
if (negative) num = -num;
out[count++] = num;
}
return count;
}Implement in C11: `void trim(char *s)`. Strips leading and trailing whitespace (space, tab, newline) from s in place. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
#include <string.h>
void trim(char *s) {
if (s == NULL) return;
char *start = s;
char *end = s + strlen(s);
while (start < end && isspace((unsigned char)*start)) {
start++;
}
while (end > start && isspace((unsigned char)*end - 1)) {
end--;
}
if (start < end) {
memmove(s, start, end - start);
}
s[end - start] = '\0';
}Implement in C11: `int atoi_strict(const char *s, int *out)`. Parses an optionally negative decimal integer from s into out. Returns 0 on success, -1 if s is not exactly one valid integer (empty, junk, trailing chars). No overflow tests. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || *s == '\0') {
return -1;
}
int sign = 1;
if (*s == '-') {
sign = -1;
s++;
} else if (*s == '+') {
s++;
}
int value = 0;
while (*s != '\0') {
if (!isdigit((unsigned char)*s)) {
return -1;
}
value = value * 10 + (*s - '0');
s++;
}
*out = sign * value;
return 0;
}#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || *s == '\0') {
return -1;
}
int sign = 1;
if (*s == '-') {
sign = -1;
s++;
} else if (*s == '+') {
s++;
}
int value = 0;
while (*s != '\0') {
if (!isdigit((unsigned char)*s)) {
return -1;
}
value = value * 10 + (*s - '0');
s++;
}
*out = sign * value;
return 0;
}#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || *s == '\0') {
return -1;
}
int sign = 1;
if (*s == '-') {
sign = -1;
s++;
} else if (*s == '+') {
s++;
}
int value = 0;
while (*s != '\0') {
if (!isdigit((unsigned char)*s)) {
return -1;
}
value = value * 10 + (*s - '0');
s++;
}
*out = sign * value;
return 0;
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
#include <stddef.h>
#include <ctype.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
const char *p = line;
int field_idx = 0;
int in_quotes = 0;
size_t out_len = 0;
while (*p) {
if (in_quotes) {
if (*p == '"') {
if (*(p + 1) == '"') {
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
} else {
in_quotes = 0;
p++;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = *p++;
} else {
p++;
}
}
} else {
if (*p == '"') {
in_quotes = 1;
p++;
} else if (*p == ',') {
if (field_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
field_idx++;
p++;
} else {
if (field_idx == idx) {
if (out_len < cap - 1) {
out[out_len++] = *p++;
} else {
p++;
}
} else {
p++;
}
}
}
}
if (field_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
return -1;
}#include <stddef.h>
#include <ctype.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
const char *p = line;
int field_idx = 0;
int in_quotes = 0;
size_t out_len = 0;
while (*p) {
if (in_quotes) {
if (*p == '"') {
if (*(p + 1) == '"') {
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
} else {
in_quotes = 0;
p++;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = *p++;
} else {
p++;
}
}
} else {
if (*p == '"') {
in_quotes = 1;
p++;
} else if (*p == ',') {
if (field_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
field_idx++;
p++;
} else {
if (field_idx == idx) {
if (out_len < cap - 1) {
out[out_len++] = *p++;
} else {
p++;
}
} else {
p++;
}
}
}
}
if (field_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
return -1;
}Served via llama.cpp CUDA · — tok/s · — GB RAM · coding total 82/126
Implement in C11: `void reverse_string(char *s)`. Reverses s in place. Reply with only a C code block. No main function, no tests, no explanation.
void reverse_string(char *s) {
if (s == NULL || *s == '\0') {
return;
}
char *end = s;
while (*end != '\0') {
end++;
}
end--;
char *start = s;
while (start < end) {
char temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}void reverse_string(char *s) {
if (s == NULL) {
return;
}
int len = 0;
while (s[len] != '\0') {
len++;
}
for (int i = 0; i < len / 2; i++) {
char temp = s[i];
s[i] = s[len - 1 - i];
s[len - 1 - i] = temp;
}
}void reverse_string(char *s) {
if (s == NULL) {
return;
}
int len = 0;
while (s[len] != '\0') {
len++;
}
for (int i = 0; i < len / 2; i++) {
char temp = s[i];
s[i] = s[len - 1 - i];
s[len - 1 - i] = temp;
}
}Implement in C11: `int count_words(const char *s)`. Counts words in s. Words are separated by one or more spaces; leading/trailing spaces possible. Reply with only a C code block. No main function, no tests, no explanation.
int count_words(const char *s) {
if (s == NULL) return 0;
int count = 0;
int in_word = 0;
while (*s != '\0') {
if (*s != ' ') {
if (!in_word) {
count++;
in_word = 1;
}
} else {
in_word = 0;
}
s++;
}
return count;
}int count_words(const char *s) {
if (s == NULL) {
return 0;
}
int count = 0;
int in_word = 0;
while (*s != '\0') {
if (*s != ' ') {
if (!in_word) {
count++;
in_word = 1;
}
} else {
in_word = 0;
}
s++;
}
return count;
}int count_words(const char *s) {
if (s == NULL) {
return 0;
}
int count = 0;
int in_word = 0;
while (*s != '\0') {
if (*s != ' ') {
if (!in_word) {
count++;
in_word = 1;
}
} else {
in_word = 0;
}
s++;
}
return count;
}Implement in C11: `void itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
(no code extracted)(no code extracted)Implement in C11: `void rot13(char *s)`. Applies ROT13 to s in place: letters rotate by 13, all other characters unchanged. Reply with only a C code block. No main function, no tests, no explanation.
void rot13(char *s) {
if (s == NULL) return;
for (; *s; ++s) {
if (*s >= 'A' && *s <= 'Z') {
*s = (*s - 'A' + 13) % 26 + 'A';
} else if (*s >= 'a' && *s <= 'z') {
*s = (*s - 'a' + 13) % 26 + 'a';
}
}
}Implement in C11: `int parse_csv_ints(const char *s, int *out, int max)`. Parses comma-separated integers from s into out (at most max). Returns the count stored. Input is well-formed (digits, optional leading minus, commas). Reply with only a C code block. No main function, no tests, no explanation.
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
const char *current = s;
while (count < max && *current != '\0') {
const char *end = current;
while (*end != ',' && *end != '\0') {
end++;
}
int num;
sscanf(current, "%d", &num);
out[count++] = num;
if (*end == '\0') {
break;
}
current = end + 1;
}
return count;
}int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
const char *current = s;
while (count < max && *current != '\0') {
const char *end = current;
while (*end != ',' && *end != '\0') {
end++;
}
int num;
sscanf(current, "%d", &num);
out[count++] = num;
if (*end == '\0') {
break;
}
current = end + 1;
}
return count;
}Implement in C11: `void trim(char *s)`. Strips leading and trailing whitespace (space, tab, newline) from s in place. Reply with only a C code block. No main function, no tests, no explanation.
void trim(char *s) {
if (s == NULL) return;
char *start = s;
while (*start != '\0' && (*start == ' ' || *start == '\t' || *start == '\n')) {
start++;
}
char *end = s;
while (*end != '\0') {
end++;
}
end--;
while (end > start && (*end == ' ' || *end == '\t' || *end == '\n')) {
end--;
}
char *dest = s;
while (start <= end) {
*dest++ = *start++;
}
*dest = '\0';
}void trim(char *s) {
if (s == NULL) return;
char *start = s;
while (*start != '\0' && (*start == ' ' || *start == '\t' || *start == '\n')) {
start++;
}
char *end = s;
while (*end != '\0') {
end++;
}
end--;
while (end > start && (*end == ' ' || *end == '\t' || *end == '\n')) {
end--;
}
char *dest = s;
while (start <= end) {
*dest++ = *start++;
}
*dest = '\0';
}Implement in C11: `int cmp_desc(const void *a, const void *b)`. qsort comparator ordering int values descending. Must be safe against overflow. Reply with only a C code block. No main function, no tests, no explanation.
(no code extracted)(no code extracted)(no code extracted)Implement in C11: `int atoi_strict(const char *s, int *out)`. Parses an optionally negative decimal integer from s into out. Returns 0 on success, -1 if s is not exactly one valid integer (empty, junk, trailing chars). No overflow tests. Reply with only a C code block. No main function, no tests, no explanation.
int atoi_strict(const char *s, int *out) {
if (s == NULL || *s == '\0') {
return -1;
}
int sign = 1;
size_t i = 0;
if (s[i] == '+' || s[i] == '-') {
sign = (s[i] == '-') ? -1 : 1;
i++;
if (s[i] == '\0') {
return -1;
}
}
int value = 0;
while (s[i] >= '0' && s[i] <= '9') {
value = value * 10 + (s[i] - '0');
i++;
}
if (s[i] != '\0') {
return -1;
}
*out = sign * value;
return 0;
}int atoi_strict(const char *s, int *out) {
if (s == NULL || *s == '\0') {
return -1;
}
int i = 0;
int sign = 1;
if (s[i] == '+' || s[i] == '-') {
sign = (s[i] == '-') ? -1 : 1;
i++;
if (s[i] == '\0') {
return -1;
}
}
for (; s[i] != '\0'; i++) {
if (s[i] < '0' || s[i] > '9') {
return -1;
}
}
int value = 0;
for (int j = i; s[j] != '\0'; j++) {
value = value * 10 + (s[j] - '0');
}
*out = sign * value;
return 0;
}int atoi_strict(const char *s, int *out) {
if (s == NULL || *s == '\0') {
return -1;
}
int i = 0;
int sign = 1;
if (s[i] == '+' || s[i] == '-') {
sign = (s[i] == '-') ? -1 : 1;
i++;
if (s[i] == '\0') {
return -1;
}
}
for (; s[i] != '\0'; i++) {
if (s[i] < '0' || s[i] > '9') {
return -1;
}
}
int value = 0;
for (int j = i; s[j] != '\0'; j++) {
value = value * 10 + (s[j] - '0');
}
*out = sign * value;
return 0;
}Implement in C11: `int glob_match(const char *pat, const char *str)`. Returns 1 if str matches the shell-style pattern pat, else 0. '*' matches any sequence (including empty), '?' matches exactly one character. All other characters match literally. The whole string must match. Reply with only a C code block. No main function, no tests, no explanation.
(no code extracted)(no code extracted)(no code extracted)Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
(no code extracted)(no code extracted)(no code extracted)Served via llama.cpp CUDA · — tok/s · — GB RAM · coding total 61/126
Implement in C11: `void reverse_string(char *s)`. Reverses s in place. Reply with only a C code block. No main function, no tests, no explanation.
void reverse_string(char *s) {
int i, j;
char c;
for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}void reverse_string(char *s) {
int i, j;
char c;
for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}void reverse_string(char *s) {
int i, j;
char c;
for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}Implement in C11: `int max_subarray(const int *a, int n)`. Returns the maximum sum of any non-empty contiguous subarray (Kadane). n >= 1. Reply with only a C code block. No main function, no tests, no explanation.
int max_subarray(const int *a, int n) {
int max_sum = a[0];
int current_sum = max_sum;
for (int i = 1; i < n; i++) {
current_sum = fmax(current_sum + a[i], a[i]);
max_sum = fmax(max_sum, current_sum);
}
return max_sum;
}int max_subarray(const int *a, int n) {
int max_so_far = a[0];
int current_sum = a[0];
for (int i = 1; i < n; i++) {
current_sum = fmax(current_sum + a[i], a[i]);
max_so_far = fmax(max_so_far, current_sum);
}
return max_so_far;
}int max_subarray(const int *a, int n) {
int max_so_far = a[0];
int current_sum = a[0];
for (int i = 1; i < n; i++) {
current_sum = fmax(current_sum + a[i], a[i]);
max_so_far = fmax(max_so_far, current_sum);
}
return max_so_far;
}Implement in C11: `void itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
#include <limits.h>
#include <stdio.h>
void itoa(int value, char *buf) {
if (value == 0) {
strcpy(buf, "0");
return;
}
int neg = value < 0;
if (neg)
value = -value;
int i = 0;
do {
buf[i++] = '0' + value % 10;
value /= 10;
} while (value);
if (neg)
buf[i++] = '-';
buf[i] = '\0';
reverse(buf, i);
}
void reverse(char *str, size_t len) {
for (size_t i = 0; i < len / 2; i++) {
char tmp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = tmp;
}
}#include <limits.h>
#include <stdio.h>
void itoa(int value, char *buf) {
if (value == 0) {
strcpy(buf, "0");
return;
}
int neg = value < 0;
if (neg)
value = -value;
int i = 0;
do {
buf[i++] = '0' + value % 10;
value /= 10;
} while (value);
if (neg)
buf[i++] = '-';
buf[i] = '\0';
reverse(buf, i);
}
void reverse(char *str, size_t len) {
for (size_t i = 0; i < len / 2; i++) {
char tmp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = tmp;
}
}#include <limits.h>
#include <stdio.h>
void itoa(int value, char *buf) {
if (value == 0) {
strcpy(buf, "0");
return;
}
int neg = value < 0;
if (neg)
value = -value;
int i = 0;
do {
buf[i++] = '0' + value % 10;
value /= 10;
} while (value);
if (neg)
buf[i++] = '-';
buf[i] = '\0';
reverse(buf, i);
}
void reverse(char *str, size_t len) {
for (size_t i = 0; i < len / 2; i++) {
char tmp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = tmp;
}
}Implement in C11: `void rot13(char *s)`. Applies ROT13 to s in place: letters rotate by 13, all other characters unchanged. Reply with only a C code block. No main function, no tests, no explanation.
#include <string.h>
void rot13(char *s) {
for (char *p = s; *p; p++) {
if (isalpha(*p)) {
*p = isupper(*p) ? 'A' + (*p - 'A' + 13) % 26 : 'a' + (*p - 'a' + 13) % 26;
}
}
}#include <string.h>
void rot13(char *s) {
for (char *p = s; *p; p++) {
if (isalpha(*p)) {
*p = isupper(*p) ? 'A' + (*p - 'A' + 13) % 26 : 'a' + (*p - 'a' + 13) % 26;
}
}
}#include <string.h>
void rot13(char *s) {
for (char *p = s; *p; p++) {
if (isalpha(*p)) {
*p = isupper(*p) ? 'A' + (*p - 'A' + 13) % 26 : 'a' + (*p - 'a' + 13) % 26;
}
}
}Implement in C11: `int parse_csv_ints(const char *s, int *out, int max)`. Parses comma-separated integers from s into out (at most max). Returns the count stored. Input is well-formed (digits, optional leading minus, commas). Reply with only a C code block. No main function, no tests, no explanation.
#include <stdio.h>
#include <string.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
int i = 0;
int j = 0;
int neg = 0;
while (s[i] != '\0') {
if (s[i] == '-') {
neg = 1;
i++;
}
if (isdigit(s[i])) {
out[count] = neg * atoi(s + i);
count++;
i += strlen(s + i);
neg = 0;
}
if (s[i] == ',') {
i++;
}
}
return count;
}#include <stdio.h>
#include <string.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
int i = 0;
int j = 0;
int neg = 0;
while (s[i] != '\0') {
if (s[i] == '-') {
neg = 1;
i++;
}
if (isdigit(s[i])) {
out[count] = neg * atoi(s + i);
count++;
i += strlen(s + i);
neg = 0;
}
if (s[i] == ',') {
i++;
}
}
return count;
}#include <stdio.h>
#include <string.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
int i = 0;
int j = 0;
int neg = 0;
while (s[i] != '\0') {
if (s[i] == '-') {
neg = 1;
i++;
}
if (isdigit(s[i])) {
out[count] = neg * atoi(s + i);
count++;
i += strlen(s + i);
neg = 0;
}
if (s[i] == ',') {
i++;
}
}
return count;
}Implement in C11: `void trim(char *s)`. Strips leading and trailing whitespace (space, tab, newline) from s in place. Reply with only a C code block. No main function, no tests, no explanation.
#include <string.h>
void trim(char *s) {
size_t i, j;
for (i = 0; s[i] && strchr(" \t\n", s[i]); i++);
for (j = strlen(s) - 1; j >= 0 && strchr(" \t\n", s[j]); j--);
s[j + 1] = 0;
memmove(s, s + i, j - i + 1);
}#include <string.h>
void trim(char *s) {
size_t i, j;
for (i = 0; s[i] && strchr(" \t\n", s[i]); i++);
for (j = strlen(s) - 1; j >= 0 && strchr(" \t\n", s[j]); j--);
s[j + 1] = 0;
memmove(s, s + i, j - i + 1);
}Implement in C11: `void ring_init(Ring *r); int ring_push(Ring *r, int v); int ring_pop(Ring *r, int *out)`. Circular buffer, capacity 8, FIFO. Include this exact definition in your code: typedef struct { int data[8]; int head; int count; } Ring; push returns 0 on success, -1 when full; pop returns 0 on success, -1 when empty. Pushes after pops must reuse freed slots (wraparound).
Reply with only a C code block. No main function, no tests, no explanation.#include <stdint.h>
typedef struct {
int data[8];
int head;
int count;
} Ring;
void ring_init(Ring *r) {
r->head = 0;
r->count = 0;
}
int ring_push(Ring *r, int v) {
if (r->count == 8) {
return -1;
}
r->data[r->head] = v;
r->head = (r->head + 1) % 8;
r->count++;
return 0;
}
int ring_pop(Ring *r, int *out) {
if (r->count == 0) {
return -1;
}
*out = r->data[r->head];
r->head = (r->head + 1) % 8;
r->count--;
return 0;
}#include <stdint.h>
typedef struct {
int data[8];
int head;
int count;
} Ring;
void ring_init(Ring *r) {
r->head = 0;
r->count = 0;
}
int ring_push(Ring *r, int v) {
if (r->count == 8) {
return -1;
}
r->data[r->head] = v;
r->head = (r->head + 1) % 8;
r->count++;
return 0;
}
int ring_pop(Ring *r, int *out) {
if (r->count == 0) {
return -1;
}
*out = r->data[r->head];
r->head = (r->head + 1) % 8;
r->count--;
return 0;
}#include <stdint.h>
typedef struct {
int data[8];
int head;
int count;
} Ring;
void ring_init(Ring *r) {
r->head = 0;
r->count = 0;
}
int ring_push(Ring *r, int v) {
if (r->count == 8) {
return -1;
}
r->data[r->head] = v;
r->head = (r->head + 1) % 8;
r->count++;
return 0;
}
int ring_pop(Ring *r, int *out) {
if (r->count == 0) {
return -1;
}
*out = r->data[r->head];
r->head = (r->head + 1) % 8;
r->count--;
return 0;
}Implement in C11: `int atoi_strict(const char *s, int *out)`. Parses an optionally negative decimal integer from s into out. Returns 0 on success, -1 if s is not exactly one valid integer (empty, junk, trailing chars). No overflow tests. Reply with only a C code block. No main function, no tests, no explanation.
int atoi_strict(const char *s, int *out) {
int neg = 0, i;
if (s[0] == '-') {
neg = 1;
s++;
}
for (i = 0; s[i] >= '0' && s[i] <= '9'; i++)
;
if (i == 0 || s[i] != '\0')
return -1;
*out = neg ? -atoi(s) : atoi(s);
return 0;
}int atoi_strict(const char *s, int *out) {
int neg = 0, i;
if (s[0] == '-') {
neg = 1;
s++;
}
for (i = 0; s[i] >= '0' && s[i] <= '9'; i++)
;
if (i == 0 || s[i] != '\0')
return -1;
*out = neg ? -atoi(s) : atoi(s);
return 0;
}int atoi_strict(const char *s, int *out) {
int neg = 0, i;
if (s[0] == '-') {
neg = 1;
s++;
}
for (i = 0; s[i] >= '0' && s[i] <= '9'; i++)
;
if (i == 0 || s[i] != '\0')
return -1;
*out = neg ? -atoi(s) : atoi(s);
return 0;
}Implement in C11: `int glob_match(const char *pat, const char *str)`. Returns 1 if str matches the shell-style pattern pat, else 0. '*' matches any sequence (including empty), '?' matches exactly one character. All other characters match literally. The whole string must match. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdio.h>
#include <string.h>
int glob_match(const char *pat, const char *str) {
int p = 0, s = 0;
while (1) {
if (pat[p] == '*') {
if (str[s] == '\0') return 1;
p++;
} else if (pat[p] == '?') {
if (str[s] == '\0') return 0;
p++; s++;
} else if (pat[p] == str[s]) {
p++; s++;
} else {
if (str[s] == '\0') return 0;
p = 0;
}
}
}#include <stdio.h>
#include <string.h>
int glob_match(const char *pat, const char *str) {
int p = 0, s = 0;
while (1) {
if (pat[p] == '*') {
if (str[s] == '\0') return 1;
p++;
} else if (pat[p] == '?') {
if (str[s] == '\0') return 0;
p++; s++;
} else if (pat[p] == '\0') {
if (str[s] == '\0') return 1;
} else if (pat[p] != str[s]) {
return 0;
}
p++; s++;
}
}#include <stdio.h>
#include <string.h>
int glob_match(const char *pat, const char *str) {
int p = 0, s = 0;
while (1) {
if (pat[p] == '*') {
if (str[s] == '\0') return 1;
p++;
} else if (pat[p] == '?') {
if (str[s] == '\0') return 0;
p++; s++;
} else if (pat[p] == '\0') {
if (str[s] == '\0') return 1;
} else if (pat[p] != str[s]) {
return 0;
}
p++; s++;
}
}Implement in C11: `double rpn_eval(const char *expr)`. Evaluates a space-separated Reverse Polish Notation expression of non-negative integers and operators + - * /. Returns the result as a double. Input is always valid and non-empty. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
double rpn_eval(const char *expr) {
double stack[100];
int top = 0;
bool is_operator[100];
char token[10];
while (expr[0] != '\0') {
if (isspace(expr[0])) {
expr++;
continue;
}
if (isdigit(expr[0]) || expr[0] == '.') {
double num = strtod(expr, &token);
stack[top++] = num;
expr = token;
} else if (expr[0] == '+' || expr[0] == '-' || expr[0] == '*' || expr[0] == '/') {
is_operator[top - 1] = true;
expr++;
} else {
printf("Invalid token: %s\n", expr);
return 0.0;
}
}
while (top > 1) {
if (!is_operator[top - 2]) {
stack[top - 2] += stack[top - 1];
top--;
} else if (is_operator[top - 2] && (stack[top - 2] == 0 || stack[top - 1] == 0)) {
stack[top - 2] += stack[top - 1];
top--;
} else {
switch (stack[top - 2]) {
case '+':
stack[top - 2] += stack[top - 1];
break;
case '-':
stack[top - 2] -= stack[top - 1];
break;
case '*':
stack[top - 2] *= stack[top - 1];
break;
case '/':
stack[top - 2] /= stack[top - 1];
break;
}
top--;
}
}
return stack[0];
}#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
double rpn_eval(const char *expr) {
double stack[100];
int top = 0;
bool is_operator[100];
char token[10];
while (expr[0] != '\0') {
if (isspace(expr[0])) {
expr++;
continue;
}
if (isdigit(expr[0]) || expr[0] == '.') {
double num = strtod(expr, &token);
stack[top++] = num;
expr = token;
} else if (expr[0] == '+' || expr[0] == '-' || expr[0] == '*' || expr[0] == '/') {
is_operator[top - 1] = true;
expr++;
} else {
printf("Invalid token: %s\n", expr);
return 0.0;
}
}
while (top > 1) {
if (!is_operator[top - 2]) {
stack[top - 2] += stack[top - 1];
top--;
} else if (is_operator[top - 2] && (stack[top - 2] == 0 || stack[top - 1] == 0)) {
stack[top - 2] += stack[top - 1];
top--;
} else {
switch (stack[top - 2]) {
case '+':
stack[top - 2] = stack[top - 1] + stack[top - 2];
break;
case '-':
stack[top - 2] = stack[top - 1] - stack[top - 2];
break;
case '*':
stack[top - 2] = stack[top - 1] * stack[top - 2];
break;
case '/':
stack[top - 2] = stack[top - 1] / stack[top - 2];
break;
}
top--;
}
}
return stack[0];
}#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
double rpn_eval(const char *expr) {
double stack[100];
int top = 0;
bool is_operator[100];
char token[10];
while (expr[0] != '\0') {
if (isspace(expr[0])) {
expr++;
continue;
}
if (isdigit(expr[0]) || expr[0] == '.') {
double num = strtod(expr, &token);
stack[top++] = num;
expr = token;
} else if (expr[0] == '+' || expr[0] == '-' || expr[0] == '*' || expr[0] == '/') {
is_operator[top - 1] = true;
expr++;
} else {
printf("Invalid token: %s\n", expr);
return 0.0;
}
}
while (top > 1) {
if (!is_operator[top - 2]) {
stack[top - 2] += stack[top - 1];
top--;
} else if (is_operator[top - 2] && (stack[top - 2] == 0 || stack[top - 1] == 0)) {
stack[top - 2] += stack[top - 1];
top--;
} else {
switch (stack[top - 2]) {
case '+':
stack[top - 2] = stack[top - 1] + stack[top - 2];
break;
case '-':
stack[top - 2] = stack[top - 1] - stack[top - 2];
break;
case '*':
stack[top - 2] = stack[top - 1] * stack[top - 2];
break;
case '/':
stack[top - 2] = stack[top - 1] / stack[top - 2];
break;
}
top--;
}
}
return stack[0];
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdio.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
int i, j, in_quotes, len = 0;
const char *p = line;
if (idx < 0 || idx >= strcspn(line, ","))
return -1;
for (i = 0; i < idx; i++) {
while (*p && *p != ',' && *p != '"')
p++;
if (*p)
p++;
}
in_quotes = 0;
while (*p && (in_quotes || *p != ',')) {
if (*p == '"')
in_quotes = !in_quotes;
else if (in_quotes && *p == '\\') {
p++;
if (*p == '\"' || *p == '\\')
out[len++] = *p++;
} else
out[len++] = *p++;
}
out[len] = '\0';
return len;
}#include <stdio.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
int i, j, in_quotes, len = 0;
const char *p = line;
if (idx < 0 || idx >= strcspn(line, ","))
return -1;
for (i = 0; i < idx; i++) {
while (*p && *p != ',' && *p != '"')
p++;
if (!*p)
return -1;
p++;
}
in_quotes = *p == '"';
if (in_quotes)
p++;
for (i = 0; *p && i < cap - 1; i++) {
if (in_quotes && *p == '"') {
if (p[1] == '"')
out[i++] = '"';
p += 2;
} else if (*p != ',' && *p != '\r' && *p != '\n') {
out[i++] = *p;
p++;
}
}
out[i] = '\0';
return strlen(out);
}#include <stdio.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
int i, j, in_quotes, len = 0;
const char *p = line;
if (idx < 0 || idx >= strcspn(line, ","))
return -1;
for (i = 0; i < idx; i++) {
while (*p && *p != ',' && *p != '"')
p++;
if (!*p)
return -1;
p++;
}
in_quotes = *p == '"';
if (in_quotes)
p++;
for (i = 0; *p && i < cap - 1; i++) {
if (in_quotes && *p == '"') {
if (p[1] == '"')
out[i++] = '"';
p += 2;
} else if (*p != ',' && *p != '\r' && *p != '\n') {
out[i++] = *p;
p++;
}
}
out[i] = '\0';
return strlen(out);
}