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 Vulkan · 27.8 tok/s · 28.8 GB RAM · coding total 115/126
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 <stddef.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
if (s == NULL || out == NULL || max <= 0) {
return 0;
}
const char *p = s;
while (*p != '\0' && count < max) {
// Skip leading whitespace if any (though spec says well-formed, so maybe not needed, but safe)
// Spec says: digits, optional leading minus, commas. So no spaces expected.
int negative = 0;
if (*p == '-') {
negative = 1;
p++;
}
long long val = 0;
int has_digit = 0;
while (*p >= '0' && *p <= '9') {
val = val * 10 + (*p - '0');
has_digit = 1;
p++;
}
if (!has_digit) {
// Malformed, but spec says well-formed. Break or return current count.
break;
}
if (negative) {
val = -val;
}
// Clamp to int range if necessary, though spec implies well-formed ints.
if (val > INT_MAX) {
val = INT_MAX;
} else if (val < INT_MIN) {
val = INT_MIN;
}
out[count++] = (int)val;
if (*p == ',') {
p++;
} else if (*p != '\0') {
// Unexpected character, stop.
break;
}
}
return count;
}#include <stddef.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
if (s == NULL || out == NULL || max <= 0) {
return 0;
}
const char *p = s;
while (*p != '\0' && count < max) {
// Skip leading whitespace if any (though spec says well-formed, so maybe not needed, but safe)
// Spec says: digits, optional leading minus, commas. So no spaces expected.
int negative = 0;
if (*p == '-') {
negative = 1;
p++;
}
long long val = 0;
int has_digit = 0;
while (*p >= '0' && *p <= '9') {
val = val * 10 + (*p - '0');
has_digit = 1;
p++;
}
if (!has_digit) {
// Malformed, but spec says well-formed. Break or return current count.
break;
}
if (negative) {
val = -val;
}
// Clamp to int range if necessary, though spec implies well-formed ints.
if (val > INT_MAX) val = INT_MAX;
if (val < INT_MIN) val = INT_MIN;
out[count++] = (int)val;
if (*p == ',') {
p++;
} else if (*p != '\0') {
// Unexpected character, stop parsing
break;
}
}
return count;
}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>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (!line || !out || cap == 0) {
if (out && cap > 0) out[0] = '\0';
return -1;
}
const char *p = line;
int current_idx = 0;
size_t out_len = 0;
int in_quotes = 0;
int field_started = 0;
/* Skip to the target field */
while (current_idx < idx) {
if (*p == '\0') {
/* Out of range */
if (out && cap > 0) out[0] = '\0';
return -1;
}
if (*p == '"') {
/* Quoted field */
p++;
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
/* Escaped quote */
p += 2;
} else {
/* End of quoted field */
p++;
break;
}
} else {
p++;
}
}
/* After closing quote, expect comma or end of line */
if (*p == ',') {
p++;
current_idx++;
} else if (*p == '\0') {
/* End of line, no more fields */
if (out && cap > 0) out[0] = '\0';
return -1;
} else {
/* Invalid format, but treat as end */
if (out && cap > 0) out[0] = '\0';
return -1;
}
} else {
/* Unquoted field */
while (*p != '\0' && *p != ',') {
p++;
}
if (*p == ',') {
p++;
current_idx++;
} else if (*p == '\0') {
/* End of line */
if (out && cap > 0) out[0] = '\0';
return -1;
} else {
if (out && cap > 0) out[0] = '\0';
return -1;
}
}
}
/* Now p points to the start of the target field */
/* Extract the field */
if (*p == '"') {
/* Quoted field */
p++;
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
/* Escaped quote */
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
} else {
/* End of quoted field */
p++;
break;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
}
} else {
/* Unquoted field */
while (*p != '\0' && *p != ',') {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
}
out[out_len] = '\0';
/* Calculate the full unescaped length (not truncated) */
/* We need to re-scan to get the true length */
/* Let's re-scan from the start of this field */
/* Actually, let's compute it during extraction properly */
/* Re-do: compute true length */
/* Reset and compute true length */
size_t true_len = 0;
const char *q = p;
/* We need to find where the field started */
/* Let's just re-parse from the beginning for the target field */
/* Simpler: re-parse to get true length */
/* Go back to start */
p = line;
current_idx = 0;
while (current_idx < idx) {
if (*p == '"') {
p++;
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
p += 2;Served via llama.cpp Vulkan · 51.4 tok/s · 0.3 GB RAM · coding total 109/126
Implement in C11: `unsigned popcount(unsigned x)`. Returns the number of set bits in x. Reply with only a C code block. No main function, no tests, no explanation.
/* Count the number of set bits in an unsigned integer (C11) */
static inline unsigned popcount(unsigned x)
{
/* 32‑bit version of the "parallel" popcount algorithm */
x = x - ((x >> 1) & 0x55555555u);
x = (x & 0x33333333u) + ((x >> 2) & 0x33333333u);
x = (x + (x >> 4)) & 0x0F0F0F0Fu;
x = x + (x >> 8);
x = x + (x >> 16);
return x & 0x3Fu; /* max 32 set bits fits in 6 bits */
}/* Count the number of set bits in an unsigned integer (C11) */
static inline unsigned popcount(unsigned x)
{
/* 32‑bit version of the "parallel" popcount algorithm */
x = x - ((x >> 1) & 0x55555555u);
x = (x & 0x33333333u) + ((x >> 2) & 0x33333333u);
x = (x + (x >> 4)) & 0x0F0F0F0Fu;
x = x + (x >> 8);
x = x + (x >> 16);
return x & 0x3Fu; /* max 32 bits => result fits in 6 bits */
}/* Count the number of set bits in an unsigned integer (C11) */
static inline unsigned popcount(unsigned x)
{
/* 32‑bit parallel count (works for any width of unsigned) */
x = x - ((x >> 1) & 0x55555555u);
x = (x & 0x33333333u) + ((x >> 2) & 0x33333333u);
x = (x + (x >> 4)) & 0x0F0F0F0Fu;
x = x + (x >> 8);
x = x + (x >> 16);
return x & 0x3Fu; /* max 32 bits => result fits in 6 bits */
}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 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 || idx < 0) return -1;
size_t out_written = 0; /* bytes actually stored (may be truncated) */
size_t field_len = 0; /* unescaped length of the target field */
int cur_idx = 0;
const char *p = line;
while (true) {
bool capture = (cur_idx == idx);
if (*p == '"') { /* quoted field */
++p; /* skip opening quote */
while (true) {
if (*p == '\0') { /* malformed, treat as end */
break;
}
if (*p == '"') {
if (p[1] == '"') { /* escaped quote */
if (capture && out_written + 1 < cap) out[out_written] = '"';
++out_written;
++field_len;
p += 2;
continue;
} else { /* closing quote */
++p;
break;
}
} else { /* regular character */
if (capture && out_written + 1 < cap) out[out_written] = *p;
++out_written;
++field_len;
++p;
}
}
if (*p == ',') ++p; /* skip field separator */
} else { /* unquoted field */
while (*p != ',' && *p != '\0') {
if (capture && out_written + 1 < cap) out[out_written] = *p;
++out_written;
++field_len;
++p;
}
if (*p == ',') ++p; /* skip field separator */
}
if (capture) { /* target field processed */
break;
}
++cur_idx;
if (*p == '\0') { /* no more fields */
break;
}
}
/* If we never captured the requested index, it is out of range */
if (cur_idx != idx) {
return -1;
}
/* NUL‑terminate the output buffer */
if (cap > 0) {
size_t term = (out_written < cap) ? out_written : cap - 1;
out[term] = '\0';
}
return (int)field_len;
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap)
{
if (idx < 0) return -1;
size_t write_pos = 0; /* bytes actually written to out */
size_t field_len = 0; /* unescaped length of the target field */
int cur = 0; /* current field index */
const char *p = line;
while (1) {
int is_target = (cur == idx);
int quoted = (*p == '"');
if (quoted) {
++p; /* skip opening quote */
while (1) {
char c = *p;
if (c == '\0') /* malformed, treat as end */
break;
if (c == '"') {
if (p[1] == '"') { /* escaped quote */
if (is_target && write_pos + 1 < cap)
out[write_pos] = '"';
if (write_pos + 1 < cap) ++write_pos;
++field_len;
p += 2;
continue;
} else { /* closing quote */
++p;
break;
}
} else {
if (is_target && write_pos + 1 < cap)
out[write_pos] = c;
if (write_pos + 1 < cap) ++write_pos;
++field_len;
++p;
}
}
if (*p == ',')
++p; /* move to next field */
} else {
while (*p != ',' && *p != '\0') {
char c = *p;
if (is_target && write_pos + 1 < cap)
out[write_pos] = c;
if (write_pos + 1 < cap) ++write_pos;
++field_len;
++p;
}
if (*p == ',')
++p; /* move to next field */
}
if (cur == idx) /* target field processed */
break;
++cur; /* advance to next field */
if (*p == '\0') /* no more fields */
break;
}
if (cur != idx) /* idx out of range */
return -1;
if (cap > 0) {
size_t term = (write_pos < cap) ? write_pos : cap - 1;
out[term] = '\0';
}
return (int)field_len;
}Served via llama.cpp Vulkan · 33.6 tok/s · 0.3 GB RAM · coding total 107/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 Vulkan · 47.1 tok/s · 0.5 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 <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;
}#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;
}
}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;
int current_idx = 0;
const char *p = line;
while (true) {
if (current_idx == idx) {
size_t written = 0;
bool in_quotes = false;
if (*p == '"') {
in_quotes = true;
p++;
}
while (*p != '\0') {
if (in_quotes) {
if (*p == '"') {
if (*(p + 1) == '"') {
// Escaped quote
if (written < cap - 1) {
out[written++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = false;
p++;
break;
}
} else {
if (written < cap - 1) {
out[written++] = *p;
}
p++;
}
} else {
// This case handles if idx was reached but we aren't in quotes
// (shouldn't happen with correct logic, but for safety)
break;
}
}
// If we were in quotes and hit end of string without closing quote,
// it's technically malformed, but we treat it as end of field.
out[written] = '\0';
return (int)written;
}
// Skip the current field to find the next comma
bool in_quotes = false;
while (*p != '\0') {
if (*p == '"') {
if (in_quotes && *(p + 1) == '"') {
p += 2; // Skip escaped quote
continue;
}
in_quotes = !in_quotes;
} else if (*p == ',' && !in_quotes) {
p++; // Move past comma
break;
}
p++;
if (*p == '\0') break;
}
if (current_idx == idx) return -1; // Should not be reachable
if (*p == '\0') {
// If we reached end of line and haven't found idx
return -1;
}
current_idx++;
if (*p == ',') p++; // Move to start of next field
if (current_idx > idx) 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;
int current_idx = 0;
const char *p = line;
while (true) {
if (current_idx == idx) {
size_t written = 0;
bool in_quotes = false;
if (*p == '"') {
in_quotes = true;
p++;
}
while (*p != '\0') {
if (in_quotes) {
if (*p == '"') {
if (*(p + 1) == '"') {
// Escaped quote
if (written < cap - 1) {
out[written++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = false;
p++;
break;
}
} else {
if (written < cap - 1) {
out[written++] = *p;
}
p++;
}
} else {
// This case handles if idx was reached but we aren't in quotes
// (shouldn't happen with correct logic, but for safety)
break;
}
}
// If we were in quotes and hit end of string without closing quote,
// it's technically malformed, but we treat it as end of field.
out[written] = '\0';
return (int)written;
}
// Skip the current field to find the next comma
bool in_quotes = false;
while (*p != '\0') {
if (*p == '"') {
if (in_quotes && *(p + 1) == '"') {
p += 2; // Skip escaped quote
continue;
}
in_quotes = !in_quotes;
} else if (*p == ',' && !in_quotes) {
p++; // Move past comma
break;
}
p++;
if (*p == '\0') break;
}
if (current_idx == idx) return -1; // Should not be reachable
if (*p == '\0') {
// If we reached end of line and haven't found idx
return -1;
}
current_idx++;
if (*p == ',') p++; // Move to start of next field
if (current_idx > idx) return -1;
}
}Served via llama.cpp Vulkan · 55.5 tok/s · 1.4 GB RAM · coding total 105/126
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 || *s == '\0') return;
// Find start of trimmed string
char *start = s;
while (*start == ' ' || *start == '\t' || *start == '\n') {
start++;
}
// If string is all whitespace
if (*start == '\0') {
*s = '\0';
return;
}
// Find end of trimmed string
char *end = start;
while (*end != '\0') {
end++;
}
end--;
// Move backwards to find last non-whitespace character
while (end > start && (*end == ' ' || *end == '\t' || *end == '\n')) {
end--;
}
// Copy trimmed string to beginning of original buffer
memmove(s, start, end - start + 1);
s[end - start + 1] = '\0';
}void trim(char *s) {
if (s == NULL || *s == '\0') return;
// Find start of non-whitespace
char *start = s;
while (*start == ' ' || *start == '\t' || *start == '\n') {
start++;
}
// If string is all whitespace
if (*start == '\0') {
*s = '\0';
return;
}
// Find end of non-whitespace
char *end = start;
while (*end != '\0' && !(*end == ' ' || *end == '\t' || *end == '\n')) {
end++;
}
while (end > start && (end[-1] == ' ' || end[-1] == '\t' || end[-1] == '\n')) {
end--;
}
// Move trimmed string to beginning
memmove(s, start, end - start);
s[end - start] = '\0';
}void trim(char *s) {
if (s == NULL || *s == '\0') return;
// Find start of non-whitespace
char *start = s;
while (*start == ' ' || *start == '\t' || *start == '\n') {
start++;
}
// If string is all whitespace
if (*start == '\0') {
*s = '\0';
return;
}
// Find end of non-whitespace
char *end = start;
while (*end != '\0' && !(*end == ' ' || *end == '\t' || *end == '\n')) {
end++;
}
while (end > start && (end[-1] == ' ' || end[-1] == '\t' || end[-1] == '\n')) {
end--;
}
// Move trimmed string to beginning
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 || !out) return -1;
const char *p = s;
int sign = 1;
int result = 0;
// Skip leading whitespace
while (isspace((unsigned char)*p)) p++;
// Check for sign
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
// Check for at least one digit
if (!isdigit((unsigned char)*p)) return -1;
// Parse digits
while (isdigit((unsigned char)*p)) {
result = result * 10 + (*p - '0');
p++;
}
// Skip trailing whitespace
while (isspace((unsigned char)*p)) p++;
// Check if we consumed the entire string
if (*p != '\0') return -1;
*out = sign * result;
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;
int result = 0;
// Skip leading whitespace
while (isspace((unsigned char)*p)) p++;
// Check for sign
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
// Check for at least one digit
if (!isdigit((unsigned char)*p)) return -1;
// Parse digits
while (isdigit((unsigned char)*p)) {
result = result * 10 + (*p - '0');
p++;
}
// Skip trailing whitespace
while (isspace((unsigned char)*p)) p++;
// Check if we consumed the entire string
if (*p != '\0') return -1;
*out = sign * result;
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;
int result = 0;
// Skip leading whitespace
while (isspace((unsigned char)*p)) p++;
// Check for sign
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
// Check for at least one digit
if (!isdigit((unsigned char)*p)) return -1;
// Parse digits
while (isdigit((unsigned char)*p)) {
result = result * 10 + (*p - '0');
p++;
}
// Skip trailing whitespace
while (isspace((unsigned char)*p)) p++;
// Check if we consumed the entire string
if (*p != '\0') return -1;
*out = sign * result;
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 <string.h>
#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;
}
size_t pos = 0; // current position in line
int field_num = 0; // current field index
size_t out_len = 0; // length of output string
// Skip leading whitespace (not required by RFC-4180, but common practice)
while (line[pos] == ' ' || line[pos] == '\t') {
pos++;
}
while (field_num <= idx) {
// Check for end of line
if (line[pos] == '\0') {
return -1;
}
// Handle empty field
if (line[pos] == ',') {
if (field_num == idx) {
out[0] = '\0';
return 0;
}
pos++;
field_num++;
continue;
}
// Handle quoted field
if (line[pos] == '"') {
pos++; // skip opening quote
size_t field_start = pos;
int in_quotes = 1;
while (in_quotes && line[pos] != '\0') {
if (line[pos] == '"') {
if (line[pos + 1] == '"') {
// Escaped quote
if (field_num == idx && out_len + 1 < cap) {
out[out_len++] = '"';
}
pos += 2;
} else {
// End of quoted field
in_quotes = 0;
pos++;
}
} else {
if (field_num == idx && out_len + 1 < cap) {
out[out_len++] = line[pos];
}
pos++;
}
}
// Skip trailing whitespace after closing quote (not required by RFC-4180)
while (line[pos] == ' ' || line[pos] == '\t') {
pos++;
}
// Check for comma or end of line
if (line[pos] == ',') {
pos++;
} else if (line[pos] != '\0' && line[pos] != '\r' && line[pos] != '\n') {
// Invalid character after quoted field
return -1;
}
if (field_num == idx) {
out[out_len] = '\0';
return (int)out_len;
}
field_num++;
} else {
// Unquoted field
size_t field_start = pos;
while (line[pos] != ',' && line[pos] != '\0' &&
line[pos] != '\r' && line[pos] != '\n') {
if (field_num == idx && out_len + 1 < cap) {
out[out_len++] = line[pos];
}
pos++;
}
if (field_num == idx) {
out[out_len] = '\0';
return (int)out_len;
}
field_num++;
// Skip comma
if (line[pos] == ',') {
pos++;
}
}
}
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;
}
size_t pos = 0; // current position in line
int field_num = 0; // current field index
size_t out_len = 0; // length of output string
// Skip leading whitespace (not required by RFC-4180, but common practice)
while (line[pos] == ' ' || line[pos] == '\t') {
pos++;
}
while (field_num <= idx) {
// Check for end of line
if (line[pos] == '\0') {
return -1;
}
// Handle empty field
if (line[pos] == ',') {
if (field_num == idx) {
out[0] = '\0';
return 0;
}
pos++;
field_num++;
continue;
}
// Handle quoted field
if (line[pos] == '"') {
pos++; // skip opening quote
size_t field_start = pos;
int in_quotes = 1;
while (in_quotes && line[pos] != '\0') {
if (line[pos] == '"') {
if (line[pos + 1] == '"') {
// Escaped quote
if (field_num == idx && out_len + 1 < cap) {
out[out_len++] = '"';
}
pos += 2;
} else {
// End of quoted field
in_quotes = 0;
pos++;
}
} else {
if (field_num == idx && out_len + 1 < cap) {
out[out_len++] = line[pos];
}
pos++;
}
}
// Skip trailing whitespace after closing quote (not required by RFC-4180)
while (line[pos] == ' ' || line[pos] == '\t') {
pos++;
}
// Check for comma or end of line
if (line[pos] == ',') {
pos++;
} else if (line[pos] != '\0' && line[pos] != '\r' && line[pos] != '\n') {
// Invalid character after quoted field
return -1;
}
if (field_num == idx) {
out[out_len] = '\0';
return (int)out_len;
}
field_num++;
} else {
// Unquoted field
size_t field_start = pos;
while (line[pos] != ',' && line[pos] != '\0' &&
line[pos] != '\r' && line[pos] != '\n') {
if (field_num == idx && out_len + 1 < cap) {
out[out_len++] = line[pos];
}
pos++;
}
if (field_num == idx) {
out[out_len] = '\0';
return (int)out_len;
}
field_num++;
// Skip comma
if (line[pos] == ',') {
pos++;
}
}
}
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;
size_t out_pos = 0;
while (field <= idx) {
// Skip leading whitespace (not required by RFC-4180, but common practice)
while (*p == ' ' || *p == '\t') {
p++;
}
if (*p == '\0') {
return -1; // End of line reached before finding field
}
if (field == idx) {
// Extract this field
int in_quotes = 0;
size_t field_len = 0;
if (*p == '"') {
in_quotes = 1;
p++;
}
while (*p != '\0') {
if (in_quotes) {
if (*p == '"') {
if (p[1] == '"') {
// Escaped quote
if (out_pos < cap - 1) {
out[out_pos++] = '"';
}
field_len++;
p += 2;
} else {
// End of quoted field
p++;
// Skip trailing whitespace and comma
while (*p == ' ' || *p == '\t') {
p++;
}
if (*p == ',') {
p++;
}
break;
}
} else {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
field_len++;
p++;
}
} else {
if (*p == ',') {
p++;
break;
} else if (*p == '"') {
// Unquoted field containing a quote is invalid per RFC-4180
// but we'll treat it as part of the field
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
field_len++;
p++;
} else {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
field_len++;
p++;
}
}
}
out[out_pos] = '\0';
return (int)field_len;
}
// Skip current field
int in_quotes = 0;
if (*p == '"') {
in_quotes = 1;
p++;
}
while (*p != '\0') {
if (in_quotes) {
if (*p == '"') {
if (p[1] == '"') {
p += 2;
} else {
p++;
in_quotes = 0;
}
} else {
p++;
}
} else {
if (*p == ',') {
p++;
break;
} else {
p++;
}
}
}
field++;
}
return -1;
}Served via llama.cpp Vulkan · 12.2 tok/s · 2.6 GB RAM · coding total 105/126
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;
while (1) {
/* Skip leading whitespace? RFC 4180 doesn't specify, but typically no.
We just process the field as-is. */
size_t len = 0;
int in_quotes = 0;
if (*p == '"') {
in_quotes = 1;
p++;
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
/* Doubled quote: unescape to single quote */
if (len < cap - 1) {
out[len++] = '"';
}
p += 2;
} else {
/* Closing quote */
p++;
break;
}
} else {
if (len < cap - 1) {
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 (len < cap - 1) {
out[len++] = *p;
}
p++;
}
if (*p == ',') {
p++;
}
}
/* NUL-terminate */
out[len] = '\0';
if (field_idx == idx) {
return (int)len;
}
/* If we've reached the end of the line, no more fields */
if (*p == '\0') {
return -1;
}
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;
while (1) {
/* Skip leading whitespace? RFC 4180 doesn't specify, but typically no.
We just process the field as-is. */
size_t len = 0;
int in_quotes = 0;
if (*p == '"') {
in_quotes = 1;
p++;
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
/* Doubled quote: unescape to single quote */
if (len < cap - 1) {
out[len++] = '"';
}
p += 2;
} else {
/* Closing quote */
p++;
break;
}
} else {
if (len < cap - 1) {
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 (len < cap - 1) {
out[len++] = *p;
}
p++;
}
if (*p == ',') {
p++;
}
}
/* NUL-terminate */
out[len] = '\0';
if (field_idx == idx) {
return (int)len;
}
/* If we've reached the end of the line, no more fields */
if (*p == '\0') {
return -1;
}
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;
while (1) {
/* Skip leading whitespace? RFC 4180 doesn't specify, but typically no.
We just process the field as-is. */
size_t len = 0;
int in_quotes = 0;
if (*p == '"') {
in_quotes = 1;
p++;
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
/* Doubled quote: unescape to single quote */
if (len < cap - 1) {
out[len++] = '"';
}
p += 2;
} else {
/* Closing quote */
p++;
break;
}
} else {
if (len < cap - 1) {
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 (len < cap - 1) {
out[len++] = *p;
}
p++;
}
if (*p == ',') {
p++;
}
}
/* NUL-terminate */
out[len] = '\0';
if (field_idx == idx) {
return (int)len;
}
/* If we've reached the end of the line, no more fields */
if (*p == '\0') {
return -1;
}
field_idx++;
}
}Served via llama.cpp Vulkan · 72.3 tok/s · 0.2 GB RAM · coding total 104/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>
#include <string.h>
void reverse_string(char *s)
{
if (!s) return;
size_t len = strlen(s);
size_t i = 0, j = len - 1;
while (i < j) {
char tmp = s[i];
s[i] = s[j];
s[j] = tmp;
++i;
--j;
}
}#include <stddef.h>
#include <string.h>
void reverse_string(char *s)
{
if (!s) return;
size_t len = strlen(s);
size_t i = 0, j = len - 1;
while (i < j) {
char tmp = s[i];
s[i] = s[j];
s[j] = tmp;
++i;
--j;
}
}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)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)```c
#include <stddef.h>
#include <limits.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (idx < 0) return -1;
if (!line) return -1;
const char *p = line;
int cur = 0;
size_t out_len = 0;
size_t write_cap = (cap > 0) ? cap - 1 : 0;
char *out_write = out;
/* Empty line -> one empty field */
if (*p == '\0') {
if (idx == 0) {
if (cap > 0) out[0] = '\0';
return 0;
}
return -1;
}
while (*p != '\0') {
int quoted = (*p == '"');
if (quoted) p++; /* skip opening quote */
while (1) {
if (*p == '\0') break;
if (quoted) {
if (*p == '"') {
if (*(p + 1) == '"') { /* escaped quote */
if (out_len < write_cap) *out_write++ = '"';Served via llama.cpp Vulkan · 17.5 tok/s · 2.9 GB RAM · coding total 104/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) {
if (*s == '-' && !started) {
sign = -1;
started = 1;
} else if (isdigit((unsigned char)*s)) {
started = 1;
val = val * 10 + (*s - '0');
} else {
return -1;
}
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) {
if (*s == '-' && !started) {
sign = -1;
started = 1;
} else if (isdigit((unsigned char)*s)) {
started = 1;
val = val * 10 + (*s - '0');
} else {
return -1;
}
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) {
if (*s == '-' && !started) {
sign = -1;
started = 1;
} else if (isdigit((unsigned char)*s)) {
started = 1;
val = val * 10 + (*s - '0');
} else {
return -1;
}
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) {
if (*p == '"') {
p++;
if (*p != '"') break;
p++;
} else {
p++;
}
}
} else {
while (*p && *p != ',') p++;
}
if (*p == ',') p++;
field_idx++;
}
return -1;
}Served via llama.cpp Vulkan · 63.7 tok/s · 0.3 GB RAM · coding total 103/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;
// Check for empty string
if (*p == '\0') {
return -1;
}
// 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;
while (*p != '\0') {
if (*p < '0' || *p > '9') {
return -1;
}
// Check for overflow before adding the next digit
if (result > (LONG_MAX - (*p - '0')) / 10) {
return -1;
}
result = result * 10 + (*p - '0');
p++;
}
// Convert to int
*out = (int)result;
// Check for overflow into negative range
if (negative && result > INT_MAX) {
return -1;
}
return 0;
}#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;
// Check for empty string
if (*p == '\0') {
return -1;
}
// 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;
while (*p >= '0' && *p <= '9') {
result = result * 10 + (*p - '0');
p++;
}
// Check for trailing characters
if (*p != '\0') {
return -1;
}
// Apply sign and check for overflow
if (negative) {
if (result > INT_MAX + 1L) {
return -1;
}
*out = -(int)result;
} else {
if (result > INT_MAX) {
return -1;
}
*out = (int)result;
}
return 0;
}#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;
// Check for empty string
if (*p == '\0') {
return -1;
}
// 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;
while (*p >= '0' && *p <= '9') {
result = result * 10 + (*p - '0');
p++;
}
// Check for trailing characters
if (*p != '\0') {
return -1;
}
// Apply sign and check for overflow
if (negative) {
if (result > INT_MAX + 1L) {
return -1;
}
*out = -(int)result;
} else {
if (result > INT_MAX) {
return -1;
}
*out = (int)result;
}
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) {
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 != '\0' && *p != ',' && *p != '\n') {
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) {
if (in_quotes) {
start = p;
} else {
start = p;
while (*p != '\0' && *p != ',' && *p != '\n') {
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) {
if (in_quotes) {
start = p;
} else {
start = p;
while (*p != '\0' && *p != ',' && *p != '\n') {
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;
}Served via llama.cpp Vulkan · 86.2 tok/s · 0.3 GB RAM · coding total 102/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';
}#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++] = '-';
}
int j;
for (j = i - 1; j >= 0; j--) {
buf[i++] = temp[j];
}
buf[i] = '\0';
}Implement in C11: `unsigned popcount(unsigned x)`. Returns the number of set bits in x. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdint.h>
static inline unsigned popcount(unsigned x) {
x ^= x >> 16;
x *= 0x0000ffffu;
x += x >> 8;
x &= 0x1f1f1f1fu;
x *= 0x01010101u;
return x >> 24;
}#include <stdint.h>
static inline unsigned popcount(unsigned x) {
x = x - ((x >> 1) & 0x55555555);
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
x = (x + (x >> 4)) & 0x0F0F0F0F;
return (x * 0x01010101) >> 24;
}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: `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;
// Find first non-whitespace character
char *start = s;
while (*start && isspace((unsigned char)*start)) {
start++;
}
// Find last non-whitespace character
char *end = s;
char *last_non_ws = s;
while (*end) {
if (!isspace((unsigned char)*end)) {
last_non_ws = end;
}
end++;
}
// If all whitespace or empty string
if (*start == '\0') {
*s = '\0';
return;
}
// Move the trimmed string to the beginning
char *src = start;
char *dst = s;
while (*src) {
*dst++ = *src++;
}
*dst = '\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.
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) return -1;
size_t out_idx = 0;
int field_idx = 0;
int in_quotes = 0;
int escaped = 0;
size_t i = 0;
while (line[i] != '\0' && field_idx <= idx) {
char c = line[i];
if (field_idx == idx) {
if (out_idx < cap - 1) {
if (escaped) {
out[out_idx++] = '"';
escaped = 0;
} else if (in_quotes && c == '"') {
if (line[i+1] == '"' || line[i+1] == ',' || line[i+1] == '\0') {
out[out_idx++] = '"';
escaped = 1;
} else {
in_quotes = 0;
}
} else {
out[out_idx++] = c;
}
}
}
if (c == '"') {
if (!in_quotes) {
in_quotes = 1;
} else if (escaped) {
escaped = 0;
} else {
escaped = 1;
}
} else {
escaped = 0;
}
if (c == ',' && !in_quotes) {
field_idx++;
if (field_idx == idx) break;
}
i++;
}
if (out_idx < cap) out[out_idx] = '\0';
else if (cap > 0) out[cap - 1] = '\0';
if (field_idx < idx) return -1;
int unescaped_len = 0;
for (size_t j = 0; j < out_idx; j++) {
if (out[j] != '"') unescaped_len++;
}
return (int)out_idx;
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (idx < 0) return -1;
size_t out_idx = 0;
int field_idx = 0;
int in_quotes = 0;
int escaped = 0;
size_t i = 0;
while (line[i] != '\0' && field_idx <= idx) {
char c = line[i];
if (field_idx == idx) {
if (out_idx < cap - 1) {
if (escaped) {
out[out_idx++] = '"';
escaped = 0;
} else if (in_quotes && c == '"') {
if (line[i+1] == '"' || line[i+1] == ',' || line[i+1] == '\0') {
out[out_idx++] = '"';
escaped = 1;
} else {
in_quotes = 0;
}
} else {
out[out_idx++] = c;
}
}
}
if (c == '"') {
if (!in_quotes) {
in_quotes = 1;
} else if (escaped) {
escaped = 0;
} else {
escaped = 1;
}
} else {
escaped = 0;
}
if (c == ',' && !in_quotes) {
field_idx++;
if (field_idx == idx) break;
}
i++;
}
if (field_idx != idx) return -1;
out[out_idx] = '\0';
return (int)out_idx;
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (idx < 0) return -1;
size_t out_idx = 0;
int field_idx = 0;
int in_quotes = 0;
int escaped = 0;
const char *p = line;
while (*p) {
if (field_idx == idx) {
if (out_idx < cap - 1) {
if (escaped) {
out[out_idx++] = '"';
escaped = 0;
} else if (in_quotes && *p == '"') {
if (p[1] == '"') {
escaped = 1;
} else {
in_quotes = 0;
}
} else {
out[out_idx++] = *p;
}
} else {
break;
}
}
if (*p == '"') {
if (in_quotes) {
if (p[1] == '"') {
if (field_idx == idx) p++;
} else {
in_quotes = 0;
}
} else {
in_quotes = 1;
}
} else if (*p == ',') {
if (field_idx == idx) {
if (out_idx < cap - 1 || escaped) {
if (escaped) {
if (out_idx < cap - 1) out[out_idx++] = '"';
}
}
}
field_idx++;
in_quotes = 0;
escaped = 0;
}
p++;
}
if (field_idx < idx) return -1;
if (out_idx < cap - 1 && escaped) {
out[out_idx++] = '"';
}
if (out_idx < cap) out[out_idx] = '\0';
else if (cap > 0) out[cap - 1] = '\0';
return (int)out_idx;
}Served via llama.cpp Vulkan · 12.5 tok/s · 2.6 GB RAM · coding total 100/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) {
end++;
}
end--; // Move back one position to point to the last character
// Swap characters from both ends moving towards the center
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) {
end++;
}
end--; // Move back one position to point to the last character
// Swap characters from both ends moving towards the center
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) {
end++;
}
end--; // Move back one position to point to the last character
// Swap characters from both ends moving towards the center
while (start < end) {
char temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}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 the buffer
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 the buffer
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 the buffer
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) {
size_t out_len = 0;
if (*p == '"') {
// Quoted field
p++; // skip opening quote
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
// Escaped quote
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
} else {
// End of quoted field
p++; // skip closing quote
break;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
}
out[out_len] = '\0';
// Skip to next comma or end
if (*p == ',') {
p++;
}
return (int)out_len;
} else {
// Unquoted field
while (*p != '\0' && *p != ',') {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
out[out_len] = '\0';
if (*p == ',') {
p++;
}
return (int)out_len;
}
}
// 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++;
}
}
} 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 ended with a field (no trailing comma), current_idx was incremented after that field
// So if idx == current_idx - 1, we should have caught it in the loop
// Actually, let's reconsider: if the line is empty or we've gone past all fields
if (idx < 0) {
return -1;
}
// Check if we're at the end and the last field was the one we wanted
// The loop processes fields and increments current_idx after each
// If the line ends with a field, that field is processed and current_idx is incremented
// So if idx == current_idx - 1, it was processed in the loop
// If we get here, idx was not found
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) {
size_t out_len = 0;
if (*p == '"') {
// Quoted field
p++; // skip opening quote
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
// Escaped quote
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
} else {
// End of quoted field
p++; // skip closing quote
break;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
}
out[out_len] = '\0';
// Skip to next comma or end
if (*p == ',') {
p++;
}
return (int)out_len;
} else {
// Unquoted field
while (*p != '\0' && *p != ',') {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
out[out_len] = '\0';
if (*p == ',') {
p++;
}
return (int)out_len;
}
}
// 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++;
}
}
} else {
while (*p != '\0' && *p != ',') {
p++;
}
}
if (*p == ',') {
p++;
}
current_idx++;
}
// If we reached here, idx was out of range
return -1;
}Served via llama.cpp Vulkan · 14.7 tok/s · 0.5 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: `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) {
if (s == NULL) return 0;
bool in_word = false;
int count = 0;
while (*s) {
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) {
if (s == NULL) return 0;
bool in_word = false;
int count = 0;
while (*s) {
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) {
if (s == NULL) return 0;
bool in_word = false;
int count = 0;
while (*s) {
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.
#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 = 463648286; // (INT_MAX + 1) % 10
*buf++ = '4';
value = 746340269; // (INT_MAX + 1) / 10
*buf++ = '6';
value = 322384093; // (INT_MAX + 1) % 10
*buf++ = '3';
value = 644768186; // (INT_MAX + 1) / 10
*buf++ = '4';
value = 188495440; // (INT_MAX + 1) % 10
*buf++ = '8';
value = 37699088; // (INT_MAX + 1) / 10
*buf++ = '2';
value = 18849544; // (INT_MAX + 1) % 10
*buf++ = '8';
value = 1884954; // (INT_MAX + 1) / 10
*buf++ = '4';
value = 942477; // (INT_MAX + 1) % 10
*buf++ = '7';
value = 94247; // (INT_MAX + 1) / 10
*buf++ = '7';
value = 47123; // (INT_MAX + 1) % 10
*buf++ = '3';
value = 4712; // (INT_MAX + 1) / 10
*buf++ = '2';
value = 359; // (INT_MAX + 1) % 10
*buf++ = '5';
value = 35; // (INT_MAX + 1) / 10
*buf++ = '9';
value = 5; // (INT_MAX + 1) % 10
*buf++ = '5';
*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';
}```c
#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 = 463648286; // (INT_MAX + 1) % 10
*buf++ = '4';
value = 746340269; // (INT_MAX + 1) / 10
*buf++ = '6';
value = 322384093; // (INT_MAX + 1) % 10
*buf++ = '3';
value = 644768186; // (INT_MAX + 1) / 10
*buf++ = '4';
value = 188495421; // (INT_MAX + 1) % 10
*buf++ = '8';
value = 376990842; // (INT_MAX + 1) / 10
*buf++ = '2';
value = 156227705; // (INT_MAX + 1) % 10
*buf++ = '8';
value = 313768019; // (INT_MAX + 1) / 10
*buf++ = '3';
value = 1431655765; // (INT_MAX + 1) % 10
*buf++ = '2';
value = 1431655765; // (INT_MAX + 1) / 10
*buf++ = '1';
value = 463648286; // (INT_MAX + 1) % 10
*buf++ = '4';
value = 746340269; // (INT_MAX + 1) / 10
*buf++ = '6';
value = 322384093; // (INT_MAX + 1) % 10
*buf++ = '3';
value = 644768186; // (INT_MAX + 1) / 10
*buf++ = '4';
value = 188495421; // (INT_MAX + 1) % 10
*buf++ = '8';
value = 376990842; // (INT_MAX + 1) / 10
*buf++ = '2';
value = 156227705; // (INT_MAX + 1) % 10
*buf++ = '8';
value = 313768019; // (INT_MAX + 1) / 10
*buf++ = '3';
value = 1431655765; // (INT_MAX + 1) % 10
*buf++ = '2';
value = 1431655765; // (INT_MAX + 1) / 10
*buf++ = '1';
value = 463648286; // (INT_MAX + 1) % 10
*buf++ = '4';
value = 746340269; // (INT_MAX + 1) / 10
*buf++ = '6';
value = 322384093; // (INT_MAX + 1) % 10
*buf++ = '3';
*buf = '\0';
return;
}
value = -value;
}
char temp[12];
int i = 0;
do {
temp[i++] = '0' + (value % 10);
value /= 10;
} while (value > 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 = 1431655765; // (INT_MAX + 1) / 10
*buf++ = '1';
value = 463648286; // (INT_MAX + 1) % 10
*buf++ = '4';
value = 746340269; // (INT_MAX + 1) / 10
*buf++ = '6';
value = 322384093; // (INT_MAX + 1) % 10
*buf++ = '3';
value = 644768186; // (INT_MAX + 1) / 10
*buf++ = '4';
value = 188495440; // (INT_MAX + 1) % 10
*buf++ = '8';
value = 37699088; // (INT_MAX + 1) / 10
*buf++ = '2';
value = 18849544; // (INT_MAX + 1) % 10
*buf++ = '8';
value = 1884954; // (INT_MAX + 1) / 10
*buf++ = '4';
value = 942477; // (INT_MAX + 1) % 10
*buf++ = '7';
value = 94247; // (INT_MAX + 1) / 10
*buf++ = '7';
value = 47123; // (INT_MAX + 1) % 10
*buf++ = '3';
value = 4712; // (INT_MAX + 1) / 10
*buf++ = '2';
value = 359; // (INT_MAX + 1) % 10
*buf++ = '5';
value = 35; // (INT_MAX + 1) / 10
*buf++ = '9';
value = 5; // (INT_MAX + 1) % 10
*buf++ = '5';
*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';
}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 *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 == NULL) 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 == NULL) 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: `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;
} else {
in_quotes = 0;
}
} else {
if (out_pos < cap - 1) out[out_pos++] = *p;
}
} else {
if (*p == '"') {
in_quotes = 1;
} else if (*p == ',') {
field++;
if (field == idx) break;
out_pos = 0;
} else if (!isspace((unsigned char)*p)) {
if (out_pos < cap - 1) out[out_pos++] = *p;
}
}
p++;
}
if (field != idx) return -1;
if (out_pos >= cap) out_pos = cap - 1;
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 Vulkan · 58.7 tok/s · 1.2 GB RAM · coding total 95/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) {
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) {
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) {
if (isspace((unsigned char)*s)) {
in_word = 0;
} else {
if (!in_word) {
count++;
in_word = 1;
}
}
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>
void itoa(int value, char *buf) {
char *p = buf;
int is_negative = 0;
if (value == INT_MIN) {
// Handle INT_MIN separately to avoid overflow when negating
*p++ = '-';
value = -(value + 1); // value becomes -(INT_MIN + 1) = INT_MAX
// We will print INT_MAX + 1 digits, then add the last '1'
// Actually, simpler approach: print digits of -(value+1) then append '1'
// But standard recursion/loop logic is easier if we just handle the sign and magnitude carefully.
// Let's use a different strategy for INT_MIN:
// It's -2147483648.
// We can't do -value.
// We can do: value = value + 1 (becomes -2147483647), negate it (2147483647), print, then append '1'.
// Or just handle the digits manually.
// Let's stick to a robust digit extraction method that works for INT_MIN.
// We will extract digits from the end.
// Since we can't negate INT_MIN, we can work with the absolute value of (value + 1) and adjust.
// Or simpler: use a temporary buffer and reverse.
// Let's rewrite the logic to be generic and safe.
// We will use a pointer to the end of the buffer to build the string backwards.
// But we need to know the length or just fill from end.
// Since we don't know the length, we can fill from the end of the buffer if we assume a max size,
// but the function signature doesn't give size.
// So we must build from the end or use a temporary buffer.
// Given the constraints, let's use a small static buffer for intermediate storage or just handle the logic carefully.
// Actually, the standard way without size limit is to build backwards from a known end or use recursion.
// Recursion is safe for stack depth (max 11 chars).
// Let's restart the logic with a recursive helper or a fixed buffer approach.
// Since I cannot use a fixed buffer of unknown size in the signature, I will use a local buffer.
char temp[12];
char *tp = temp + 11;
*tp = '\0';
if (value == INT_MIN) {
*p++ = '-';
// We need to print 2147483648
// We can't negate it.
// Let's do: value = value + 1 -> -2147483647
// Then negate -> 2147483647
// Then print, then append '1' at the end? No, that's not how digits work.
// 2147483648 = 2147483647 + 1.
// Let's just handle the digits manually for INT_MIN.
// Or, simpler: use a temporary buffer to store digits.
// We can't negate INT_MIN, so we can't use standard % 10 logic easily if we want to avoid overflow.
// But wait, -2147483648 % 10 is -8 in C11 (truncation towards zero).
// -2147483648 / 10 is -214748364.
// So we can extract digits as negative numbers and then negate them for printing.
// This works perfectly in C11.
}
// Reset p to temp for the generic logic
p = temp + 11;
*p = '\0';
if (value == INT_MIN) {
*p-- = '8';
value = -(value + 1); // value is now 2147483647
// Now we can proceed with the standard logic for the rest
} else if (value < 0) {
*p-- = '-';
value = -value;
}
// Now value is non-negative (except the special case handled above where we already put '8')
// Wait, if value was INT_MIN, we put '8', then set value to#include <limits.h>
void itoa(int value, char *buf) {
int i = 0;
int is_negative = 0;
if (value == INT_MIN) {
// Handle INT_MIN separately to avoid overflow when negating
buf[i++] = '-';
value = -(value + 1); // value becomes -(INT_MIN + 1) = INT_MAX
// We will print INT_MAX + 1 digits, then add '1' at the end
// Actually, simpler approach: convert -(INT_MIN + 1) which is INT_MAX,
// then append '1' at the end.
// Let's do standard conversion for the positive part of -(INT_MIN + 1)
// which is INT_MAX.
// Wait, -(INT_MIN) is undefined. -(INT_MIN + 1) is INT_MAX.
// So we print INT_MAX, then append '1'.
// But standard logic:
// value = -(value + 1); // value is now INT_MAX
// convert value to string
// append '1'
// But this is tricky.
// Better: use a temporary buffer or handle digits manually.
// Let's use the standard approach with a temporary buffer for digits.
} else if (value < 0) {
buf[i++] = '-';
value = -value;
}
if (value == 0) {
buf[i++] = '0';
} else {
char temp[12]; // Enough for INT_MIN
int j = 0;
int temp_val = value;
// If value was INT_MIN, we handled it above? No, let's restart logic cleanly.
}
}#include <limits.h>
void itoa(int value, char *buf) {
char *p = buf;
int is_negative = 0;
if (value == INT_MIN) {
// Handle INT_MIN separately to avoid overflow when negating
*p++ = '-';
value = -(value + 1); // value becomes -(INT_MIN + 1) = INT_MAX
// We will print INT_MAX + 1 digits, then add the last '1'
// Actually, simpler approach: print digits of -(value+1) then append '1'
// But standard recursion/loop logic is easier if we just handle the sign and magnitude carefully.
// Let's use a different strategy for INT_MIN:
// It's -2147483648.
// We can't do -value.
// We can do: value = value + 1 (becomes -2147483647), negate to 2147483647, print, then append '1'.
// Or just handle the digits manually.
// Let's stick to a robust digit extraction method that works for INT_MIN.
// We will extract digits from the end.
// Since we can't negate INT_MIN, we work with negative numbers throughout the loop.
// -2147483648 % 10 = -8.
// -2147483648 / 10 = -214748364.
// This works in C11 (truncation towards zero).
// So we can just keep the number negative.
} else if (value < 0) {
*p++ = '-';
value = -value;
}
if (value == 0) {
*p++ = '0';
} else {
// We need to handle the case where value is INT_MIN carefully if we used the logic above.
// If we kept it negative, we need to handle the sign logic differently.
// Let's restart the logic to be purely negative-based to handle INT_MIN safely.
}
// Reset and implement the robust negative-based approach
p = buf;
if (value == INT_MIN) {
*p++ = '-';
// We will process -2147483648 as a negative number
// Digits will be extracted as negative remainders
// We need a temporary buffer or reverse logic.
// Let's use a temporary buffer to store digits in reverse order.
char temp[12]; // Enough for INT_MIN + sign
int idx = 0;
int n = value;
while (n != 0) {
int rem = n % 10;
n /= 10;
// rem is negative (e.g., -8)
temp[idx++] = '0' - rem;
}
temp[idx] = '\0';
// Reverse temp into buf
int i = 0;
while (idx > 0) {
buf[i++] = temp[--idx];
}
buf[i] = '\0';
return;
}
if (value < 0) {
*p++ = '-';
value = -value;
}
if (value == 0) {
*p++ = '0';
} else {
char temp[12];
int idx = 0;
while (value > 0) {
temp[idx++] = (char)('0' + (value % 10));
value /= 10;
}
while (idx > 0) {
*p++ = temp[--idx];
}
}
*p = '\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.
int cmp_desc(const void *a, const void *b) {
int ia = *(const int *)a;
int ib = *(const int *)b;
if (ia > ib) return 1;
if (ia < ib) return -1;
return 0;
}int cmp_desc(const void *a, const void *b) {
int ia = *(const int *)a;
int ib = *(const int *)b;
if (ia > ib) return 1;
if (ia < ib) return -1;
return 0;
}int cmp_desc(const void *a, const void *b) {
int ia = *(const int *)a;
int ib = *(const int *)b;
if (ia > ib) return 1;
if (ia < ib) return -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>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
if (*p == '-') {
p++;
} else if (*p == '+') {
p++;
}
if (*p == '\0' || !isdigit((unsigned char)*p)) {
return -1;
}
while (isdigit((unsigned char)*p)) {
p++;
}
if (*p != '\0') {
return -1;
}
long val = 0;
int negative = (s != NULL && *s == '-');
const char *curr = s;
if (*curr == '-' || *curr == '+') {
curr++;
}
while (isdigit((unsigned char)*curr)) {
val = val * 10 + (*curr - '0');
curr++;
}
if (negative) {
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;
if (*p == '-') {
p++;
} else if (*p == '+') {
p++;
}
if (*p == '\0' || !isdigit((unsigned char)*p)) {
return -1;
}
while (isdigit((unsigned char)*p)) {
p++;
}
if (*p != '\0') {
return -1;
}
long val = 0;
int negative = (s != NULL && *s == '-');
const char *curr = s;
if (*curr == '-' || *curr == '+') {
curr++;
}
while (isdigit((unsigned char)*curr)) {
val = val * 10 + (*curr - '0');
curr++;
}
if (negative) {
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;
if (*p == '-') {
p++;
} else if (*p == '+') {
p++;
}
if (*p == '\0' || !isdigit((unsigned char)*p)) {
return -1;
}
while (isdigit((unsigned char)*p)) {
p++;
}
if (*p != '\0') {
return -1;
}
long val = 0;
int negative = (s != NULL && *s == '-');
const char *curr = s;
if (*curr == '-' || *curr == '+') {
curr++;
}
while (isdigit((unsigned char)*curr)) {
val = val * 10 + (*curr - '0');
curr++;
}
if (negative) {
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 *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_s = NULL;
while (*s) {
if (*p == '*') {
star = p;
match_s = s;
p++;
} else if (*p == '?' || *p == *s) {
p++;
s++;
} else {
if (star) {
p = star + 1;
match_s++;
s = match_s;
} 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_s = NULL;
while (*s) {
if (*p == '*') {
star = p;
match_s = s;
p++;
} else if (*p == '?' || *p == *s) {
p++;
s++;
} else {
if (star) {
p = star + 1;
match_s++;
s = match_s;
} else {
return 0;
}
}
}
while (*p == '*') {
p++;
}
return *p == '\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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
double rpn_eval(const char *expr) {
double stack[1024];
int top = 0;
const char *p = expr;
char *endptr;
while (*p) {
while (isspace((unsigned char)*p)) {
p++;
}
if (*p == '\0') break;
double val = strtod(p, &endptr);
if (endptr == p) {
// It's an operator
char op = *p;
if (op == '+' || op == '-' || op == '*' || op == '/') {
if (top < 2) return 0.0; // Should not happen per valid input guarantee
double b = stack[--top];
double a = stack[--top];
switch (op) {
case '+': stack[top++] = a + b; break;
case '-': stack[top++] = a - b; break;
case '*': stack[top++] = a * b; break;
case '/': stack[top++] = a / b; break;
}
}
p = endptr;
} else {
// It's a number
stack[top++] = val;
p = endptr;
}
}
return stack[0];
}Served via llama.cpp Vulkan · 58.5 tok/s · 1.2 GB RAM · coding total 94/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) {
char temp[12];
int i = 0;
int is_negative = 0;
if (value == INT_MIN) {
// Handle INT_MIN separately to avoid overflow when negating
// INT_MIN is -2147483648
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++] = '-';
}
buf[i] = '\0';
// 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--;
}
}#include <limits.h>
#include <string.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
// INT_MIN is -2147483648
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++] = '-';
}
buf[i] = '\0';
// 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--;
}
}#include <limits.h>
#include <string.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
// INT_MIN is -2147483648
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++] = '-';
}
buf[i] = '\0';
// 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--;
}
}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 = '\0';
end--;
}
}#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 = '\0';
end--;
}
}#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 = '\0';
end--;
}
}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 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') {
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 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') {
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 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') {
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 *s = str;
const char *p = pat;
const char *star = NULL;
const char *match = NULL;
while (*s) {
if (*p == '?') {
s++;
p++;
} else if (*p == '*') {
star = p++;
match = s;
} else if (*p == *s) {
s++;
p++;
} else {
if (star) {
p = star + 1;
match++;
s = match;
} else {
return 0;
}
}
}
while (*p == '*') {
p++;
}
return *p == '\0';
}int glob_match(const char *pat, const char *str) {
const char *s = str;
const char *p = pat;
const char *star = NULL;
const char *match = NULL;
while (*s) {
if (*p == '?') {
s++;
p++;
} else if (*p == '*') {
star = p++;
match = s;
} else if (*p == *s) {
s++;
p++;
} else {
if (star) {
p = star + 1;
match++;
s = match;
} else {
return 0;
}
}
}
while (*p == '*') {
p++;
}
return *p == '\0';
}Served via llama.cpp Vulkan · 22.2 tok/s · 2.4 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) {
if (s == NULL) {
return 0;
}
int count = 0;
int in_word = 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) {
if (s == NULL) {
return 0;
}
int count = 0;
int in_word = 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) {
if (s == NULL) {
return 0;
}
int count = 0;
int in_word = 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] = '\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] = '\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 valid 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 integer.
if (*p == '\0') {
return -1; // Empty string
}
int sign = 1;
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
if (!isdigit((unsigned char)*p)) {
return -1; // No digits found after optional sign
}
long long val = 0;
while (isdigit((unsigned char)*p)) {
val = val * 10 + (*p - '0');
p++;
}
// Check for trailing characters
if (*p != '\0') {
return -1;
}
*out = (int)(val * sign);
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 valid 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 integer.
if (*p == '\0') {
return -1; // Empty string
}
int sign = 1;
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
if (!isdigit((unsigned char)*p)) {
return -1; // No digits found after optional sign
}
long long val = 0;
while (isdigit((unsigned char)*p)) {
val = val * 10 + (*p - '0');
p++;
}
// Check for trailing characters
if (*p != '\0') {
return -1;
}
*out = (int)(val * sign);
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;
int val = 0;
int has_digit = 0;
if (*p == '\0') {
return -1;
}
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
if (*p == '\0') {
return -1;
}
while (*p != '\0') {
if (!isdigit((unsigned char)*p)) {
return -1;
}
val = val * 10 + (*p - '0');
has_digit = 1;
p++;
}
if (!has_digit) {
return -1;
}
*out = 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_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');
}Served via llama.cpp Vulkan · 12.5 tok/s · 2.6 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 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 = start + strlen(start) - 1;
while (end > start && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
if (start != s) {
memmove(s, start, (size_t)(end - start + 2));
}
}#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 = start + strlen(start) - 1;
while (end > start && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
if (start != s) {
memmove(s, start, (size_t)(end - start + 2));
}
}#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 = start + strlen(start) - 1;
while (end > start && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
if (start != s) {
memmove(s, start, (size_t)(end - start + 2));
}
}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 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_p = NULL;
const char *star_s = NULL;
while (*s) {
if (*p == '*') {
star_p = p;
star_s = s;
p++;
continue;
}
if (*p == '?' || *p == *s) {
p++;
s++;
} else {
if (star_p) {
p = star_p + 1;
star_s++;
s = star_s;
} 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_p = NULL;
const char *star_s = NULL;
while (*s) {
if (*p == '*') {
star_p = p;
star_s = s;
p++;
continue;
}
if (*p == '?' || *p == *s) {
p++;
s++;
} else {
if (star_p) {
p = star_p + 1;
star_s++;
s = star_s;
} 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_p = NULL;
const char *star_s = NULL;
while (*s) {
if (*p == '*') {
star_p = p;
star_s = s;
p++;
continue;
}
if (*p == '?' || *p == *s) {
p++;
s++;
} else {
if (star_p) {
p = star_p + 1;
star_s++;
s = star_s;
} 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.
```c
#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;
size_t out_len = 0;
bool in_quotes = false;
bool field_started = false;
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;
}
p++;
continue;
} else {
field_started = true;
}
} else {
if (c == '"') {
if (*(p + 1) == '"') {
// Escaped quote
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = false;
p++;
continue;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = c;
}
p++;
continue;
}
}
// Unquoted character processing
if (current_idx == idx) {
if (out_len < cap - 1) {
out[out_len++] = c;
}
}
p++;
}
// Handle the last field if we are still inside it
if (current_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
// Check if we reached the end and the requested index was not found
// We need to verify if the last field was the one we wanted or if we ran out
// The loop above increments current_idx on commas.
// If we finished the loop and current_idx <= idx, we need to check the last field logic again.
// Actually, the logic above processes characters. Let's refine the state tracking.
// Re-evaluating logic: The previous loop structure was slightly flawed for the last field.
// Let's restart with a cleaner state machine approach.
p = line;
current_idx = 0;
out_len = 0;
in_quotes = false;
bool collecting = false;
while (*p != '\0') {
char c = *p;
if (!in_quotes) {
if (c == '"') {
in_quotes = true;
collecting = true;
p++;
continue;
} else if (c == ',') {
if (collecting) {
current_idx++;
collecting = false;
}
p++;
continue;
} else {
collecting = true;
}
} else {
if (c == '"') {
if (*(p + 1) == '"') {
if (current_idx == idx) {
if (out_len < cap - 1) {
out[out_len++] = '"';
}
}
p += 2;
continue;
} else {
in_quotes = false;
p++;
continue;
}
} else {
if (current_idx == idx) {
if (out_len < cap - 1) {
out[out_len++] = c;
}
}
p++;
continue;
}
}
// Unquoted char
if (current_idx == idx) {
if (out_len < cap - 1) {
out[out_len++] = c;
}
}
p++;
}
// Check if the last field was the target
// If we are still in_quotes, the line is malformed per RFC 4180, but we treat it as end of field.
// If we finished a field, current_idx is the index of the field we just finished.
// If we are currently collecting (unquoted or quoted), current_idx#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;
size_t out_len = 0;
bool in_quotes = false;
bool field_started = false;
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++;
continue;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = c;
}
p++;
continue;
}
} else {
if (c == ',') {
// End of unquoted field
break;
}
if (out_len < cap - 1) {
out[out_len++] = c;
}
p++;
continue;
}
} else {
// Skipping fields before the target index
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++;
}
}
}
}
// Handle end of string
if (current_idx == idx) {
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;
size_t out_len = 0;
bool in_quotes = false;
bool field_started = false;
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;
}
p++;
continue;
} else {
field_started = true;
}
} else {
if (c == '"') {
if (*(p + 1) == '"') {
// Escaped quote
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = false;
p++;
continue;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = c;
}
p++;
continue;
}
}
// Unquoted character processing
if (current_idx == idx) {
if (out_len < cap - 1) {
out[out_len++] = c;
}
}
p++;
}
// Handle the last field if we are still inside it
if (current_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
// Check if we reached the end and the requested index was not found
// We need to verify if the last field was the one we wanted or if we ran out
// The loop above increments current_idx on commas.
// If we finished the loop and current_idx <= idx, we need to check the last field logic again.
// Re-evaluate logic for the last field which doesn't end with a comma
// The previous loop structure was slightly flawed for the last field accumulation.
// Let's restart with a cleaner state machine approach.
p = line;
current_idx = 0;
out_len = 0;
in_quotes = false;
bool collecting = false;
while (*p != '\0') {
char c = *p;
if (!in_quotes) {
if (c == '"') {
in_quotes = true;
collecting = true;
p++;
continue;
} else if (c == ',') {
if (collecting) {
current_idx++;
collecting = false;
}
p++;
continue;
} else {
collecting = true;
}
} else {
if (c == '"') {
if (*(p + 1) == '"') {
if (current_idx == idx) {
if (out_len < cap - 1) {
out[out_len++] = '"';
}
}
p += 2;
continue;
} else {
in_quotes = false;
p++;
continue;
}
} else {
if (current_idx == idx) {
if (out_len < cap - 1) {
out[out_len++] = c;
}
}
p++;
continue;
}
}
// Unquoted char
if (current_idx == idx) {
if (out_len < cap - 1) {
out[out_len++] = c;
}
}
p++;
}
// Check if the last field was the target
if (current_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
// If we are here, idx was out of range
return -1;
}Served via llama.cpp Vulkan · 48.3 tok/s · 0.4 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';
} else {
if (value == INT_MIN) {
*p++ = '-';
unsigned int u = (unsigned int)(-(value + 1)) + 1u;
while (u > 0) {
*p++ = (char)('0' + (u % 10u));
u /= 10u;
}
} else {
if (value < 0) {
negative = 1;
value = -value;
}
while (value > 0) {
*p++ = (char)('0' + (value % 10));
value /= 10;
}
if (negative) {
*p++ = '-';
}
}
}
*p = '\0';
char *start = buf;
char *end = buf + (p - buf - 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 = value < 0;
if (negative) {
*p++ = '-';
if (value == INT_MIN) {
value = INT_MAX;
} else {
value = -value;
}
}
if (value == 0) {
*p++ = '0';
} else {
char tmp[12];
int i = 0;
while (value > 0) {
tmp[i++] = (char)('0' + (value % 10));
value /= 10;
}
while (i > 0) {
*p++ = tmp[--i];
}
}
*p = '\0';
}#include <limits.h>
void itoa(int value, char *buf) {
char *p = buf;
int negative = value < 0;
if (negative) {
*p++ = '-';
if (value == INT_MIN) {
value = INT_MAX;
} else {
value = -value;
}
}
if (value == 0) {
*p++ = '0';
} else {
char *start = p;
while (value > 0) {
*p++ = (char)('0' + (value % 10));
value /= 10;
}
while (p > start) {
*--p = *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.
void trim(char *s) {
char *end;
while (*s == ' ' || *s == '\t' || *s == '\n')
s++;
if (*s == '\0') {
*s = '\0';
return;
}
end = s + strlen(s) - 1;
while (end > s && (*end == ' ' || *end == '\t' || *end == '\n'))
*end-- = '\0';
}void trim(char *s) {
char *end;
while (*s == ' ' || *s == '\t' || *s == '\n')
s++;
if (*s == '\0') {
*s = '\0';
return;
}
end = s + strlen(s) - 1;
while (end > s && (*end == ' ' || *end == '\t' || *end == '\n'))
*end-- = '\0';
}#include <ctype.h>
void trim(char *s) {
if (s == NULL) return;
// Skip leading whitespace
while (*s && isspace((unsigned char)*s)) {
s++;
}
// Find end of string
char *end = s;
while (*end) {
end++;
}
// Skip trailing whitespace
while (end > s && isspace((unsigned char)end[-1])) {
end--;
}
// Null-terminate at the new end
*end = '\0';
// Copy the trimmed string back to the original start
char *dest = s;
while (*s) {
*dest++ = *s++;
}
*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;
}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 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) {
int field = 0;
int in_quotes = 0;
size_t len = 0;
while (*line) {
char c = *line;
if (in_quotes) {
if (c == '"') {
if (line[1] == '"') {
if (len + 1 < cap) {
out[len++] = '"';
}
line += 2;
continue;
} else {
in_quotes = 0;
line++;
continue;
}
} else {
if (len + 1 < cap) {
out[len++] = c;
}
line++;
continue;
}
} else {
if (c == '"') {
in_quotes = 1;
line++;
continue;
} else if (c == ',') {
if (field == idx) {
break;
}
field++;
line++;
continue;
} else {
if (len + 1 < cap) {
out[len++] = c;
}
line++;
continue;
}
}
}
if (field != idx) {
return -1;
}
out[len] = '\0';
return (int)len;
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0 || idx < 0)
return -1;
int cur = 0;
int in_quotes = 0;
size_t len = 0;
for (const char *p = line; ; ++p) {
if (*p == '\0') {
if (cur == idx) {
if (len >= cap - 1)
len = cap - 1;
out[len] = '\0';
return (int)len;
}
return -1;
}
if (in_quotes) {
if (*p == '"') {
if (p[1] == '"') {
if (len < cap - 1)
out[len++] = '"';
++p;
} else {
in_quotes = 0;
}
} else {
if (len < cap - 1)
out[len++] = *p;
}
} else {
if (*p == '"') {
in_quotes = 1;
} else if (*p == ',') {
if (cur == idx) {
if (len >= cap - 1)
len = cap - 1;
out[len] = '\0';
return (int)len;
}
++cur;
} else {
if (len < cap - 1)
out[len++] = *p;
}
}
}
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0 || idx < 0)
return -1;
int cur = 0;
int in_quotes = 0;
size_t len = 0;
for (const char *p = line; ; ++p) {
if (*p == '\0') {
if (cur == idx) {
if (len >= cap - 1)
len = cap - 1;
out[len] = '\0';
return (int)len;
}
return -1;
}
if (in_quotes) {
if (*p == '"') {
if (p[1] == '"') {
if (len < cap - 1)
out[len++] = '"';
++p;
} else {
in_quotes = 0;
}
} else {
if (len < cap - 1)
out[len++] = *p;
}
} else {
if (*p == '"') {
in_quotes = 1;
} else if (*p == ',') {
if (cur == idx) {
if (len >= cap - 1)
len = cap - 1;
out[len] = '\0';
return (int)len;
}
++cur;
} else {
if (len < cap - 1)
out[len++] = *p;
}
}
}
}Served via llama.cpp Vulkan · 85.0 tok/s · 0.3 GB RAM · coding total 87/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;
}
if (negative) {
value = -value;
}
int i = 0;
while (value > 0) {
buf[i++] = '0' + (value % 10);
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 <string.h>
#include <stdbool.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;
bool in_quotes = false;
// Skip to the desired field
while (current_idx < idx) {
if (*p == '"') {
in_quotes = !in_quotes;
p++;
if (in_quotes && *p == '"') {
p++; // Skip the second quote
}
} else if (*p == ',' && !in_quotes) {
current_idx++;
} else if (*p == '\0') {
return -1;
} else {
p++;
}
}
// Skip leading comma if needed
if (*p == ',') p++;
// Handle quoted field
if (*p == '"') {
in_quotes = true;
p++;
size_t out_pos = 0;
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++;
break;
}
} else {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
p++;
}
}
out[out_pos] = '\0';
return out_pos;
} else {
// Unquoted field
size_t out_pos = 0;
while (*p != ',' && *p != '\0') {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
p++;
}
out[out_pos] = '\0';
return out_pos;
}
}#include <string.h>
#include <stdbool.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;
bool in_quotes = false;
// Skip to the desired field
while (current_idx < idx) {
if (*p == '"') {
in_quotes = !in_quotes;
p++;
if (in_quotes && *p == '"') p++; // Skip escaped quote
} else if (*p == ',' && !in_quotes) {
current_idx++;
p++;
} else {
p++;
}
if (*p == '\0') return -1;
}
// Skip leading comma if needed
if (*p == ',') p++;
// Handle quoted field
if (*p == '"') {
in_quotes = true;
p++;
size_t out_pos = 0;
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++;
break;
}
} else {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
p++;
}
}
out[out_pos] = '\0';
return out_pos;
}
// Handle unquoted field
size_t out_pos = 0;
while (*p != '\0' && (*p != ',' || in_quotes)) {
if (*p == '"') {
in_quotes = !in_quotes;
} else if (*p == ',' && !in_quotes) {
break;
} else {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
}
p++;
}
out[out_pos] = '\0';
return out_pos;
}#include <string.h>
#include <stdbool.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;
bool in_quotes = false;
// Skip to the desired field
while (current_idx < idx) {
if (*p == '"') {
in_quotes = !in_quotes;
p++;
if (in_quotes && *p == '"') {
p++; // Skip the second quote in a quoted field
}
} else if (*p == ',' && !in_quotes) {
current_idx++;
} else if (*p == '\0') {
return -1; // Field not found
} else {
p++;
}
}
// Skip leading commas and whitespace
while (*p == ',' || *p == ' ' || *p == '\t') {
p++;
}
// Check if we're at the end of the line
if (*p == '\0') return -1;
// Extract the field
size_t out_pos = 0;
bool escaped = false;
while (*p != '\0' && (in_quotes || *p != ',')) {
if (escaped) {
if (*p == '"') {
if (out_pos < cap - 1) {
out[out_pos++] = '"';
}
escaped = false;
} else {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
escaped = false;
}
} else if (*p == '"') {
if (in_quotes) {
// Check if this is a double quote
if (*(p + 1) == '"') {
if (out_pos < cap - 1) {
out[out_pos++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = false;
}
} else {
// Start of quoted field
in_quotes = true;
}
} else if (*p == '\\' && in_quotes) {
escaped = true;
} else {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
}
p++;
}
// Null terminate
out[out_pos] = '\0';
// Skip trailing commas and whitespace
while (*p == ',' || *p == ' ' || *p == '\t') {
p++;
}
// If we're not at the end of the line, there should be a comma or end of line
if (*p != '\0' && *p != ',') {
// This is an error condition, but we'll just return what we have
}
return (int)out_pos;
}Served via llama.cpp Vulkan · 10.2 tok/s · 0.6 GB RAM · coding total 86/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 *left = s;
char *right = s;
while (*right != '\0') {
right++;
}
right--;
while (left < right) {
char temp = *left;
*left = *right;
*right = temp;
left++;
right--;
}
}void reverse_string(char *s) {
if (s == NULL) {
return;
}
char *left = s;
char *right = s;
while (*right != '\0') {
right++;
}
if (right == s) {
return;
}
right--;
while (left < right) {
char temp = *left;
*left = *right;
*right = temp;
left++;
right--;
}
}void reverse_string(char *s) {
if (s == NULL) {
return;
}
char *end = s;
while (*end != '\0') {
end++;
}
end--;
char *start = s;
while (start < end) {
char temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}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 {
if (in_word) {
in_word = 0;
}
}
s++;
}
return count;
}(no code extracted)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 {
if (in_word) {
in_word = 0;
}
}
s++;
}
return count;
}Implement in C11: `unsigned popcount(unsigned x)`. Returns the number of set bits in x. Reply with only a C code block. No main function, no tests, no explanation.
(no code extracted)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 value = 0;
int sign = 1;
const char *num_start = current;
if (*num_start == '-') {
sign = -1;
num_start++;
}
while (num_start < end) {
value = value * 10 + (*num_start - '0');
num_start++;
}
value *= sign;
out[count++] = value;
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++;
}
if (*start == '\0') {
*s = '\0';
return;
}
char *end = start;
while (*end != '\0') {
end++;
}
end--;
while (end > start && (*end == ' ' || *end == '\t' || *end == '\n')) {
end--;
}
char *dst = s;
while (dst <= end) {
*dst++ = *start++;
}
*dst = '\0';
}```c
#include <string.h>
void trim(char *s) {
if (s == NULL) {
return;
}
char *start = s;
while (*start != '\0' && (*start == ' ' || *start == '\t' || *start == '\n')) {
start++;
}
if (*start == '\0') {
*s = '\0';
return;
}
charImplement 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)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 == '\0') {
return -1;
}
const char *p = s;
int sign = 1;
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
if (*p == '\0') {
return -1;
}
while (*p != '\0') {
if (!isdigit((unsigned char)*p)) {
return -1;
}
p++;
}
int result = 0;
const char *digit_start = s;
if (s[0] == '+' || s[0] == '-') {
digit_start = s + 1;
}
for(no code extracted)(no code extracted)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: `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 <stddef.h>
double rpn_eval(const char *expr) {
double stack[1024];
int stack_top = 0;
size_t len = strlen(expr);
char *expr_copy = malloc(len + 1);
memcpy(expr_copy, expr, len + 1);
char *token = strtok(expr_copy, " ");
while (token != NULL) {
if (token[0] == '+' || token[0] == '-' || token[0] == '*' || token[0] == '/') {
double a = stack[--stack_top];
double b = stack[--stack_top];
double res;
switch (token[0]) {
case '+': res = b + a; break;
case '-': resImplement 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 Vulkan · 42.2 tok/s · 0.2 GB RAM · coding total 82/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 = -(INT_MIN / 10);
buf[0] = '-';
buf[1] = '2';
buf[2] = '1';
buf += 3;
} else {
value = -value;
}
}
char *start = buf;
do {
*buf++ = (char)('0' + (value % 10));
value /= 10;
} while (value > 0);
if (is_negative) {
*buf++ = '-';
}
*buf = '\0';
char *end = buf - 1;
while (start < end) {
char tmp = *start;
*start++ = *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 = -(INT_MIN / 10);
buf[0] = '-';
buf[1] = '2';
buf[2] = '1';
buf += 3;
} else {
value = -value;
}
}
char *start = buf;
do {
*buf++ = (char)('0' + (value % 10));
value /= 10;
} while (value > 0);
if (is_negative) {
*buf++ = '-';
}
*buf = '\0';
char *end = buf - 1;
while (start < end) {
char tmp = *start;
*start++ = *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 = -(INT_MIN / 10);
buf[0] = '-';
buf[1] = '2';
buf[2] = '1';
buf += 3;
} else {
value = -value;
}
}
char *start = buf;
do {
*buf++ = (char)('0' + (value % 10));
value /= 10;
} while (value > 0);
if (is_negative) {
*buf++ = '-';
}
*buf = '\0';
char *end = buf - 1;
while (start < end) {
char tmp = *start;
*start++ = *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: `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)) {
end--;
}
if (start < end) {
size_t len = end - start + 1;
memmove(s, start, len);
s[len] = '\0';
} else {
s[0] = '\0';
}
}#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)) {
end--;
}
if (start < end) {
size_t len = end - start + 1;
memmove(s, start, len);
s[len] = '\0';
} else {
s[0] = '\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>
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;
int in_quotes = 0;
size_t out_len = 0;
while (*p != '\0') {
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) break;
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) return -1;
if (in_quotes) {
while (*p != '\0') {
if (*p == '"' && *(p + 1) == '"') {
if (out_len < cap - 1) out[out_len++] = '"';
p += 2;
} else if (*p == '"') {
in_quotes = 0;
p++;
break;
} else {
if (out_len < cap - 1) out[out_len++] = *p++;
else p++;
}
}
}
if (in_quotes) return -1;
if (out_len < cap) out[out_len] = '\0';
else if (cap > 0) out[cap - 1] = '\0';
return (int)out_len;
}#include <stddef.h>
#include <ctype.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;
int in_quotes = 0;
size_t out_len = 0;
while (*p != '\0') {
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) break;
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) return -1;
if (in_quotes) {
if (*p == '\0') return -1;
p++;
}
if (out_len < cap) out[out_len] = '\0';
else if (cap > 0) out[cap - 1] = '\0';
return (int)out_len;
}#include <stddef.h>
#include <ctype.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;
int in_quotes = 0;
size_t out_len = 0;
while (*p != '\0') {
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) break;
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) return -1;
if (in_quotes) {
if (*p == '\0') return -1;
p++;
}
if (out_len < cap) out[out_len] = '\0';
else if (cap > 0) out[cap - 1] = '\0';
return (int)out_len;
}Served via llama.cpp Vulkan · 9.6 tok/s · 2.1 GB RAM · coding total 64/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 * (s[i] - '0');
neg = 0;
i++;
} else if (s[i] == ',' && s[i + 1] != ' ') {
i++;
count++;
} else if (s[i] == ' ' && s[i + 1] != ',') {
i++;
} else {
fprintf(stderr, "Invalid input at position %d\n", i);
return -1;
}
}
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 * (s[i] - '0');
neg = 0;
i++;
} else if (s[i] == ',' && s[i + 1] != ' ') {
i++;
count++;
} else if (s[i] == ' ' && s[i + 1] != ',') {
i++;
} else {
fprintf(stderr, "Invalid input at position %d\n", i);
return -1;
}
}
return count;
}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;
for (i = 0; s[i] == ' '; i++)
;
if (s[i] == '-') {
neg = 1;
i++;
}
if (s[i] < '0' || s[i] > '9')
return -1;
for (; s[i] >= '0' && s[i] <= '9'; i++)
;
if (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;
for (i = 0; s[i] == ' '; i++)
;
if (s[i] == '-') {
neg = 1;
i++;
}
if (s[i] < '0' || s[i] > '9')
return -1;
for (; s[i] >= '0' && s[i] <= '9'; i++)
;
if (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;
for (i = 0; s[i] == ' '; i++)
;
if (s[i] == '-') {
neg = 1;
i++;
}
if (s[i] < '0' || s[i] > '9')
return -1;
for (; s[i] >= '0' && s[i] <= '9'; i++)
;
if (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] == '\0') {
if (str[s] == '\0') return 1;
} else if (pat[p] == str[s]) {
p++; s++;
} else {
return 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]) {
p++; s++;
} else {
return 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]) {
p++; s++;
} else {
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 <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];
}#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;
char c;
for (i = 0; line[i] != '\0' && idx-- > 0; i++) {
if (line[i] == '"') {
in_quotes = !in_quotes;
while (line[i] == '"') i++;
}
}
if (idx < 0) {
return -1;
}
i--;
while (line[i] == ' ') i--;
while (line[i] != '\0' && line[i] != ',' && line[i] != '"') {
if (len < cap - 1) {
out[len++] = line[i++];
} else {
return len;
}
}
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;
char c;
for (i = 0; line[i] != '\0' && idx-- > 0; i++) {
if (line[i] == '"') {
in_quotes = !in_quotes;
while (line[i] == '"') i++;
}
}
if (idx < 0) {
return -1;
}
i--;
while (line[i] == ' ') i--;
while (line[i] != '\0' && line[i] != ',' && line[i] != '"') {
if (len < cap - 1) {
out[len++] = line[i++];
} else {
return len;
}
}
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;
char c;
for (i = 0; line[i] != '\0' && idx-- > 0; i++) {
if (line[i] == '"') {
in_quotes = !in_quotes;
while (line[i] == '"') i++;
}
}
if (idx < 0) {
return -1;
}
i--;
while (line[i] == ' ') i--;
while (line[i] != '\0' && line[i] != ',' && line[i] != '"') {
if (len < cap - 1) {
out[len++] = line[i++];
} else {
return len;
}
}
out[len] = '\0';
return len;
}