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 Metal · 33.0 tok/s · 39.0 GB RAM · coding total 108/126
No failures.
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) {
return -1;
}
const char *p = line;
int current_idx = 0;
size_t out_len = 0;
while (1) {
// Parse one field
const char *field_start = p;
int quoted = 0;
size_t field_len = 0; // unescaped length of this field
if (*p == '"') {
quoted = 1;
p++; // skip opening quote
// Scan until closing quote
while (*p) {
if (*p == '"') {
if (*(p + 1) == '"') {
// doubled quote, counts as one char in unescaped
field_len++;
p += 2;
} else {
// closing quote
p++; // skip closing quote
break;
}
} else {
field_len++;
p++;
}
}
// After closing quote, expect comma or end of line
if (*p == ',') {
p++;
} else if (*p != '\0') {
// Malformed, but we'll just continue
// Actually, per RFC, after closing quote should be comma or EOL
// If not, treat as part of field? Let's be lenient and just move on.
// But for correctness, if it's not comma or EOL, it's invalid.
// We'll just break out of parsing this field.
}
} else {
// Unquoted field
while (*p && *p != ',') {
field_len++;
p++;
}
if (*p == ',') {
p++;
}
}
// Check if this is the target field
if (current_idx == idx) {
// Extract the field content into out
const char *src = field_start;
size_t remaining = field_len;
size_t copy_len = (remaining < cap - 1) ? remaining : cap - 1;
if (quoted) {
// Skip opening quote
src++;
size_t copied = 0;
while (copied < copy_len && *src) {
if (*src == '"') {
if (*(src + 1) == '"') {
out[out_len++] = '"';
src += 2;
copied++;
} else {
// closing quote, stop
break;
}
} else {
out[out_len++] = *src;
src++;
copied++;
}
}
// If we stopped early due to closing quote but need more chars,
// this shouldn't happen if field_len is correct.
// But if copy_len < field_len, we truncated.
// Ensure we don't exceed cap-1
if (out_len >= cap) {
out_len = cap - 1;
}
} else {
// Unquoted: just copy
size_t to_copy = (field_len < cap - 1) ? field_len : cap - 1;
memcpy(out, src, to_copy);
out_len = to_copy;
}
out[out_len] = '\0';
return (int)field_len;
}
current_idx++;
// If we reached end of line and haven't found the field, it's out of range
if (*p == '\0') {
// Check if there was a trailing comma creating an empty field
// Actually, if we just parsed a field and p is at '\0',
// and current_idx > idx, then idx is out of range.
// But what if the line ends with a comma? Then there's an empty field after it.
// Let's handle that: if the last character before '\0' was a comma,
// there's one more empty field.
// Actually, our loop structure: after parsing a field, if *p == '\0', we break.
// But if the line ends with ',', then after parsing the last non-empty field,
// p points to '\0' after consuming the comma? No.
// Let's trace: "a,b,"
// Parse "a": p moves past 'a', sees ',', p```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) {
return -1;
}
const char *p = line;
int current_idx = 0;
size_t out_len = 0;
while (1) {
// Parse one field
const char *field_start = p;
int quoted = 0;
size_t field_len = 0; // unescaped length of this field
if (*p == '"') {
quoted = 1;
p++; // skip opening quote
// Scan until closing quote
while (*p) {
if (*p == '"') {
if (*(p + 1) == '"') {
// doubled quote, counts as one char in unescaped
field_len++;
p += 2;
} else {
// closing quote
p++; // skip closing quote
break;
}
} else {
field_len++;
p++;
}
}
// After closing quote, expect comma or end of line
if (*p == ',') {
p++;
} else if (*p != '\0') {
// Malformed, but we'll just continue
// Actually, per RFC, after closing quote should be comma or EOL
// If not, treat as part of field? Let's be lenient and just move on.
// But for correctness, if it's not comma or EOL, it's invalid.
// We'll just break out of parsing this field.
}
} else {
// Unquoted field
while (*p && *p != ',') {
field_len++;
p++;
}
if (*p == ',') {
p++;
}
}
// Check if this is the target field
if (current_idx == idx) {
// Extract the field content into out
const char *src = field_start;
size_t remaining = field_len;
size_t copy_len = (remaining < cap - 1) ? remaining : cap - 1;
if (quoted) {
// Skip opening quote
src++;
size_t copied = 0;
while (copied < copy_len && *src) {
if (*src == '"') {
if (*(src + 1) == '"') {
out[out_len++] = '"';
src += 2;
copied++;
} else {
// closing quote, stop
break;
}
} else {
out[out_len++] = *src;
src++;
copied++;
}
}
// If we stopped early due to closing quote but need more chars,
// this shouldn't happen if field_len is correct.
// But if copy_len < field_len, we truncated.
// Ensure we don't exceed cap-1
if (out_len >= cap) {
out_len = cap - 1;
}
} else {
// Unquoted: just copy
size_t to_copy = (field_len < cap - 1) ? field_len : cap - 1;
memcpy(out, src, to_copy);
out_len = to_copy;
}
out[out_len] = '\0';
return (int)field_len;
}
current_idx++;
// If we reached end of line and haven't found the field, it's out of range
if (*p == '\0') {
// Check if there was a trailing comma creating an empty field
// Actually, if we just parsed a field and p is at '\0',
// and current_idx > idx, then idx is out of range.
// But what if the line ends with a comma? Then there's an empty field after it.
// Let's handle that: if the last character before '\0' was a comma,
// there's one more empty field.
// Actually, our loop structure: after parsing a field, if *p == '\0', we break.
// But if the line ends with ',', then after parsing the last non-empty field,
// p points to '\0' after consuming the comma? No.
// Let's trace: "a,b,"
// Parse "a": p moves past 'a', sees ',', p```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) {
return -1;
}
const char *p = line;
int current_idx = 0;
size_t out_len = 0;
while (1) {
// Parse one field
const char *field_start = p;
int quoted = 0;
size_t field_len = 0; // unescaped length of this field
if (*p == '"') {
quoted = 1;
p++; // skip opening quote
// Scan until closing quote
while (*p) {
if (*p == '"') {
if (*(p + 1) == '"') {
// doubled quote, counts as one char in unescaped
field_len++;
p += 2;
} else {
// closing quote
p++; // skip closing quote
break;
}
} else {
field_len++;
p++;
}
}
// After closing quote, expect comma or end of line
if (*p == ',') {
p++;
} else if (*p != '\0') {
// Malformed, but we'll just continue
// Actually, per RFC, after closing quote should be comma or EOL
// If not, treat as part of field? Let's be lenient and just move on.
// But for correctness, if it's not comma or EOL, it's invalid.
// We'll just break out of parsing this field.
}
} else {
// Unquoted field
while (*p && *p != ',') {
field_len++;
p++;
}
if (*p == ',') {
p++;
}
}
// Check if this is the target field
if (current_idx == idx) {
// Extract the field content into out
const char *src = field_start;
size_t remaining = field_len;
size_t copy_len = (remaining < cap - 1) ? remaining : cap - 1;
if (quoted) {
// Skip opening quote
src++;
size_t copied = 0;
while (copied < copy_len && *src) {
if (*src == '"') {
if (*(src + 1) == '"') {
out[out_len++] = '"';
src += 2;
copied++;
} else {
// closing quote, stop
break;
}
} else {
out[out_len++] = *src;
src++;
copied++;
}
}
// If we stopped early due to closing quote but need more chars,
// this shouldn't happen if field_len is correct.
// But if copy_len < field_len, we truncated.
// Ensure we don't exceed cap-1
if (out_len >= cap) {
out_len = cap - 1;
}
} else {
// Unquoted: just copy
size_t to_copy = (field_len < cap - 1) ? field_len : cap - 1;
memcpy(out, src, to_copy);
out_len = to_copy;
}
out[out_len] = '\0';
return (int)field_len;
}
current_idx++;
// If we reached end of line and haven't found the field, it's out of range
if (*p == '\0') {
// Check if there was a trailing comma creating an empty field
// Actually, if we just parsed a field and p is at '\0',
// and current_idx > idx, then idx is out of range.
// But what if the line ends with a comma? Then there's an empty field after it.
// Let's handle that: if the last character before '\0' was a comma,
// there's one more empty field.
// Actually, our loop structure: after parsing a field, if *p == '\0', we break.
// But if the line ends with ',', then after parsing the last non-empty field,
// p points to '\0' after consuming the comma? No.
// Let's trace: "a,b,"
// Parse "a": p moves past 'a', sees ',', pServed via llama.cpp Metal · 61.6 tok/s · 51.7 GB RAM · coding total 106/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 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 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 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 */
}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 >> 24) & 0x000000FFU) |
((x >> 8) & 0x0000FF00U) |
((x << 8) & 0x00FF0000U) |
((x << 24) & 0xFF000000U);
}#include <stdint.h>
static inline uint32_t bswap32(uint32_t x)
{
return ((x >> 24) & 0x000000FFU) |
((x >> 8) & 0x0000FF00U) |
((x << 8) & 0x00FF0000U) |
((x << 24) & 0xFF000000U);
}No failures.
Served via llama.cpp Metal · 83.1 tok/s · 11.8 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.
(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.
#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap)
{
const char *p = line;
int cur = 0; /* current field index */
size_t out_len = 0; /* number of chars written to out */
size_t field_len = 0; /* unescaped length of the target field */
while (1) {
int quoted = (*p == '"');
if (quoted) {
/* quoted field */
p++; /* skip opening quote */
while (*p) {
if (*p == '"') {
if (*(p + 1) == '"') {
/* escaped quote */
if (cur == idx && out_len + 1 < cap)
out[out_len++] = '"';
field_len++;
p += 2;
} else {
/* closing quote */
p++; /* skip closing quote */
break;
}
} else {
if (cur == idx && out_len + 1 < cap)
out[out_len++] = *p;
field_len++;
p++;
}
}
/* after quoted field, skip following comma if present */
if (*p == ',')
p++;
} else {
/* unquoted field */
while (*p && *p != ',') {
if (cur == idx && out_len + 1 < cap)
out[out_len++] = *p;
field_len++;
p++;
}
if (*p == ',')
p++;
}
/* field processed */
if (cur == idx)
break; /* we have extracted the requested field */
cur++;
if (*p == '\0')
break; /* end of line reached */
}
if (cur != idx)
return -1; /* idx out of range */
if (cap > 0)
out[out_len] = '\0';
return (int)field_len;
}```c
#include <stddef.h>
#include <limits.h>
/* Extract the idx-th (0‑based) field from an RFC‑4180 CSV line.
* The field is unescaped and NUL‑terminated into out, truncated to cap‑1 chars.
* Return the unescaped length, or -1 if idx is out of range. */
int csv_field(const char *line, int idx, char *out, size_t cap)
{
const char *p = line;
int field = 0;
/* Determine if the line ends with a trailing comma (empty field). */
const char *end = line;
while (*end && *end != '\r' && *end != '\n')
++end;
int trailing_comma = (end > line && *(end - 1) == ',');
/* Skip fields up to idx. */
while (field < idx) {
/* If we reach the end before skipping enough fields, idx is out of range. */
if (*p == '\0' || *p == '\r' || *p == '\n')
return -1;
/* Skip one field(no code extracted)Served via llama.cpp Metal · 60.7 tok/s · 16.6 GB RAM · coding total 102/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) {
count++;
in_word = true;
}
}
s++;
}
return count;
}#include <ctype.h>
#include <stdbool.h>
int count_words(const char *s) {
if (s == NULL) {
return 0;
}
int count = 0;
bool in_word = false;
while (*s != '\0') {
if (isspace((unsigned char)*s)) {
in_word = false;
} else {
if (!in_word) {
count++;
in_word = true;
}
}
s++;
}
return count;
}#include <ctype.h>
#include <stdbool.h>
int count_words(const char *s) {
if (s == NULL) {
return 0;
}
int count = 0;
bool in_word = false;
while (*s != '\0') {
if (isspace((unsigned char)*s)) {
in_word = false;
} else {
if (!in_word) {
count++;
in_word = true;
}
}
s++;
}
return count;
}Implement in C11: `int cmp_desc(const void *a, const void *b)`. qsort comparator ordering int values descending. Must be safe against overflow. Reply with only a C code block. No main function, no tests, no explanation.
int cmp_desc(const void *a, const void *b) {
int val_a = *(const int *)a;
int val_b = *(const int *)b;
if (val_a > val_b) {
return 1;
} else if (val_a < val_b) {
return -1;
} else {
return 0;
}
}int cmp_desc(const void *a, const void *b) {
int val_a = *(const int *)a;
int val_b = *(const int *)b;
if (val_a > val_b) {
return 1;
} else if (val_a < val_b) {
return -1;
} else {
return 0;
}
}int cmp_desc(const void *a, const void *b) {
int val_a = *(const int *)a;
int val_b = *(const int *)b;
if (val_a > val_b) {
return 1;
} else if (val_a < val_b) {
return -1;
} else {
return 0;
}
}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) {
bool in_quotes = false;
size_t written = 0;
bool field_found = false;
if (current_idx == idx) {
field_found = true;
}
// Check if we are at the start of a field
// A field starts at the beginning of the string or after a comma
// However, we must skip the content of previous fields first.
// This loop logic handles skipping or extracting
// We need to parse the structure of the current field
// If we are looking for the current idx, we process it.
// If we are skipping, we just advance p.
// Let's refine: we iterate through fields one by one.
}
return -1; // Placeholder for logic structure
}#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) {
bool in_quotes = false;
size_t written = 0;
bool field_found = false;
if (current_idx == idx) {
field_found = true;
}
// Check if we are at the start of a field
// A field starts at the beginning of the string or after a comma
// However, we must skip the content of previous fields first.
// This loop logic handles skipping or extracting
// We need to parse the structure of the current field
// If we are looking for the current idx, we process it.
// If we are skipping, we just advance p.
// Let's refine: we iterate through fields one by one.
}
return -1; // Placeholder for logic structure
}#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) {
bool in_quotes = false;
size_t written = 0;
bool field_found = false;
if (current_idx == idx) {
field_found = true;
}
// Check if we are at the start of a field
// A field starts at the beginning of the string or after a comma
// However, we must skip the content of previous fields first.
// This loop logic handles skipping or extracting
// We need to parse the structure of the current field
// If we are looking for the current idx, we process it.
// If we are skipping, we just advance p.
// Let's refine: we iterate through fields one by one.
}
return -1; // Placeholder for logic structure
}Served via llama.cpp Metal · 14.4 tok/s · 15.6 GB RAM · coding total 102/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 + strlen(start) - 1;
while (end > start && isspace((unsigned char)*end)) {
end--;
}
if (start != s) {
size_t len = (size_t)(end - start + 1);
memmove(s, start, len);
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 + strlen(start) - 1;
while (end > start && isspace((unsigned char)*end)) {
end--;
}
if (start != s) {
size_t len = (size_t)(end - start + 1);
memmove(s, start, len);
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 + strlen(start) - 1;
while (end > start && isspace((unsigned char)*end)) {
end--;
}
if (start != s) {
size_t len = (size_t)(end - start + 1);
memmove(s, start, len);
s[len] = '\0';
} else {
end[1] = '\0';
}
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int field_idx = 0;
size_t out_len = 0;
while (1) {
/* Skip leading whitespace? RFC 4180 doesn't specify, but typically no.
We just process the field as-is. */
/* Check if field is quoted */
if (*p == '"') {
p++; /* skip opening quote */
while (1) {
if (*p == '\0') {
/* Unterminated quote; treat rest as field content?
RFC 4180 says this is invalid, but we'll just stop. */
break;
}
if (*p == '"') {
if (*(p + 1) == '"') {
/* Doubled quote: unescape to single quote */
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
} else {
/* Closing quote */
p++;
break;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
}
/* After closing quote, expect comma or end of line */
if (*p == ',') {
p++;
}
/* If not comma and not end, it's malformed; we'll just continue */
} else {
/* Unquoted field: read until comma or end of line */
while (*p != '\0' && *p != ',') {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
if (*p == ',') {
p++;
}
}
/* We've parsed one field */
if (field_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
/* Check if we've reached end of line */
if (*p == '\0') {
return -1; /* idx out of range */
}
field_idx++;
}
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int field_idx = 0;
size_t out_len = 0;
while (1) {
/* Skip leading whitespace? RFC 4180 doesn't specify, but typically no.
We just process the field as-is. */
/* Check if field is quoted */
if (*p == '"') {
p++; /* skip opening quote */
while (1) {
if (*p == '\0') {
/* Unterminated quote; treat rest as field content?
RFC 4180 says this is invalid, but we'll just stop. */
break;
}
if (*p == '"') {
if (*(p + 1) == '"') {
/* Doubled quote: unescape to single quote */
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
} else {
/* Closing quote */
p++;
break;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
}
/* After closing quote, expect comma or end of line */
if (*p == ',') {
p++;
}
/* If not comma and not end, it's malformed; we'll just continue */
} else {
/* Unquoted field: read until comma or end of line */
while (*p != '\0' && *p != ',') {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
if (*p == ',') {
p++;
}
}
/* We've parsed one field */
if (field_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
/* Check if we've reached end of line */
if (*p == '\0') {
return -1; /* idx out of range */
}
field_idx++;
}
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int field_idx = 0;
size_t out_len = 0;
while (1) {
/* Skip leading whitespace? RFC 4180 doesn't specify, but typically no.
We just process the field as-is. */
/* Check if field is quoted */
if (*p == '"') {
p++; /* skip opening quote */
while (1) {
if (*p == '\0') {
/* Unterminated quote; treat rest as field content?
RFC 4180 says this is invalid, but we'll just stop. */
break;
}
if (*p == '"') {
if (*(p + 1) == '"') {
/* Doubled quote: unescape to single quote */
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
} else {
/* Closing quote */
p++;
break;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
}
/* After closing quote, expect comma or end of line */
if (*p == ',') {
p++;
}
/* If not comma and not end, it's malformed; we'll just continue */
} else {
/* Unquoted field: read until comma or end of line */
while (*p != '\0' && *p != ',') {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
if (*p == ',') {
p++;
}
}
/* We've parsed one field */
if (field_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
/* Check if we've reached end of line */
if (*p == '\0') {
return -1; /* idx out of range */
}
field_idx++;
}
}Served via llama.cpp Metal · 20.4 tok/s · 70.5 GB RAM · coding total 102/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;
}#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;
}#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 Metal · 39.7 tok/s · 61.6 GB RAM · coding total 101/126
Implement in C11: `int count_words(const char *s)`. Counts words in s. Words are separated by one or more spaces; leading/trailing spaces possible. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdbool>
int count_words(const char *s) {
int count = 0;
bool in_word = false;
while (*s) {
if (*s == ' ') {
in_word = false;
} else if (!in_word) {
in_word = true;
count++;
}
s++;
}
return count;
}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 Metal · 81.1 tok/s · 16.9 GB RAM · coding total 100/126
Implement in C11: `void itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
#include <limits.h>
void itoa(int value, char *buf) {
char 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++] = '-';
}
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++] = '-';
}
for (int j = 0; j < i; j++) {
buf[j] = temp[i - 1 - j];
}
buf[i] = '\0';
}Implement in C11: `uint32_t bswap32(uint32_t x)`. Returns x with its four bytes reversed (endian swap). Include <stdint.h> yourself. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdint.h>
static inline uint32_t bswap32(uint32_t x) {
return ((x & 0x000000FFU) << 24) |
((x & 0x0000FF00U) << 8) |
((x & 0x00FF0000U) >> 8) |
((x & 0xFF000000U) >> 24);
}#include <stdint.h>
static inline uint32_t bswap32(uint32_t x) {
return ((x & 0x000000FFU) << 24) |
((x & 0x0000FF00U) << 8) |
((x & 0x00FF0000U) >> 8) |
((x & 0xFF000000U) >> 24);
}#include <stdint.h>
static inline uint32_t bswap32(uint32_t x) {
return ((x & 0x000000FFU) << 24) |
((x & 0x0000FF00U) << 8) |
((x & 0x00FF0000U) >> 8) |
((x & 0xFF000000U) >> 24);
}Implement in C11: `int cmp_desc(const void *a, const void *b)`. qsort comparator ordering int values descending. Must be safe against overflow. Reply with only a C code block. No main function, no tests, no explanation.
int cmp_desc(const void *a, const void *b) {
int ia = *(const int *)a;
int ib = *(const int *)b;
return (ia > ib) - (ia < ib);
}int cmp_desc(const void *a, const void *b) {
int ia = *(const int *)a;
int ib = *(const int *)b;
return (ia > ib) - (ia < ib);
}int cmp_desc(const void *a, const void *b) {
int ia = *(const int *)a;
int ib = *(const int *)b;
return (ia > ib) - (ia < ib);
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (idx < 0 || !line || !out || cap == 0) return -1;
const char *p = line;
int current_idx = 0;
size_t out_pos = 0;
while (current_idx <= idx && *p) {
if (current_idx == idx) {
if (*p == '"') {
p++;
while (*p) {
if (*p == '"') {
if (*(p + 1) == '"') {
if (out_pos < cap - 1) out[out_pos++] = '"';
p += 2;
} else if (*(p + 1) == ',' || *(p + 1) == '\0' || *(p + 1) == '\n') {
p++;
break;
} else {
if (out_pos < cap - 1) out[out_pos++] = '"';
p++;
}
} else {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
}
if (*p) p++;
if (out_pos < cap - 1) out[out_pos] = '\0';
return (int)out_pos;
} else {
while (*p && *p != ',' && *p != '\n') {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
if (out_pos < cap - 1) out[out_pos] = '\0';
return (int)out_pos;
}
}
if (*p == '"') {
p++;
while (*p) {
if (*p == '"') {
if (*(p + 1) == '"') p += 2;
else break;
} else {
p++;
}
}
if (*p) p++;
} else {
while (*p && *p != ',' && *p != '\n') p++;
}
if (*p == ',') p++;
current_idx++;
}
return -1;
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (idx < 0 || !line || !out || cap == 0) return -1;
const char *p = line;
int current_idx = 0;
size_t out_pos = 0;
while (current_idx <= idx && *p) {
if (current_idx == idx) {
if (*p == '"') {
p++;
while (*p) {
if (*p == '"') {
if (*(p + 1) == '"') {
if (out_pos < cap - 1) out[out_pos++] = '"';
p += 2;
} else if (*(p + 1) == ',' || *(p + 1) == '\0' || *(p + 1) == '\n') {
p++;
break;
} else {
if (out_pos < cap - 1) out[out_pos++] = '"';
p++;
}
} else {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
}
if (*p) p++;
if (out_pos < cap - 1) out[out_pos] = '\0';
return (int)out_pos;
} else {
while (*p && *p != ',' && *p != '\n') {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
if (out_pos < cap - 1) out[out_pos] = '\0';
return (int)out_pos;
}
}
if (*p == '"') {
p++;
while (*p) {
if (*p == '"') {
if (*(p + 1) == '"') p += 2;
else break;
} else {
p++;
}
}
if (*p) p++;
} else {
while (*p && *p != ',' && *p != '\n') p++;
}
if (*p == ',') p++;
current_idx++;
}
return -1;
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (idx < 0 || !line || !out || cap == 0) return -1;
const char *p = line;
int current_idx = 0;
size_t out_pos = 0;
while (current_idx <= idx && *p) {
if (current_idx == idx) {
if (*p == '"') {
p++;
while (*p) {
if (*p == '"') {
if (*(p + 1) == '"') {
if (out_pos < cap - 1) out[out_pos++] = '"';
p += 2;
} else if (*(p + 1) == ',' || *(p + 1) == '\0' || *(p + 1) == '\n') {
p++;
break;
} else {
if (out_pos < cap - 1) out[out_pos++] = '"';
p++;
}
} else {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
}
if (*p) p++;
if (out_pos < cap - 1) out[out_pos] = '\0';
return (int)out_pos;
} else {
while (*p && *p != ',' && *p != '\n') {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
if (out_pos < cap - 1) out[out_pos] = '\0';
return (int)out_pos;
}
}
if (*p == '"') {
p++;
while (*p) {
if (*p == '"') {
if (*(p + 1) == '"') p += 2;
else break;
} else {
p++;
}
}
if (*p) p++;
} else {
while (*p && *p != ',' && *p != '\n') p++;
}
if (*p == ',') p++;
current_idx++;
}
return -1;
}Served via llama.cpp Metal · 54.6 tok/s · 45.6 GB RAM · coding total 99/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 || *s == '\0') return;
// Find start of trimmed string
char *start = s;
while (*start && isspace((unsigned char)*start)) {
start++;
}
// If string is all whitespace
if (*start == '\0') {
*s = '\0';
return;
}
// Find end of trimmed string
char *end = start;
while (*end && !isspace((unsigned char)*end)) {
end++;
}
while (end > start && isspace((unsigned char)*(end - 1))) {
end--;
}
// Move trimmed string to beginning
memmove(s, start, end - start);
s[end - start] = '\0';
}#include <ctype.h>
void trim(char *s) {
if (s == NULL || *s == '\0') return;
// Find start of trimmed string
char *start = s;
while (*start && isspace((unsigned char)*start)) {
start++;
}
// If string is all whitespace
if (*start == '\0') {
*s = '\0';
return;
}
// Find end of trimmed string
char *end = start;
while (*end && !isspace((unsigned char)*end)) {
end++;
}
while (end > start && isspace((unsigned char)*(end - 1))) {
end--;
}
// Move trimmed string to beginning
memmove(s, start, end - start);
s[end - start] = '\0';
}#include <ctype.h>
void trim(char *s) {
if (s == NULL || *s == '\0') return;
// Find start of trimmed string
char *start = s;
while (*start && isspace((unsigned char)*start)) {
start++;
}
// If string is all whitespace
if (*start == '\0') {
*s = '\0';
return;
}
// Find end of trimmed string
char *end = start;
while (*end && !isspace((unsigned char)*end)) {
end++;
}
while (end > start && isspace((unsigned char)*(end - 1))) {
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;
// Skip leading whitespace
while (isspace((unsigned char)*p)) p++;
// Check for sign
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
// Must have at least one digit
if (!isdigit((unsigned char)*p)) return -1;
int result = 0;
while (isdigit((unsigned char)*p)) {
result = result * 10 + (*p - '0');
p++;
}
// Skip trailing whitespace
while (isspace((unsigned char)*p)) p++;
// Must have consumed entire string
if (*p != '\0') return -1;
*out = 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;
// Skip leading whitespace
while (isspace((unsigned char)*p)) p++;
// Check for sign
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
// Must have at least one digit
if (!isdigit((unsigned char)*p)) return -1;
int result = 0;
while (isdigit((unsigned char)*p)) {
result = result * 10 + (*p - '0');
p++;
}
// Skip trailing whitespace
while (isspace((unsigned char)*p)) p++;
// Must have consumed entire string
if (*p != '\0') return -1;
*out = 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;
// Skip leading whitespace
while (isspace((unsigned char)*p)) p++;
// Check for sign
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
// Must have at least one digit
if (!isdigit((unsigned char)*p)) return -1;
int result = 0;
while (isdigit((unsigned char)*p)) {
result = result * 10 + (*p - '0');
p++;
}
// Skip trailing whitespace
while (isspace((unsigned char)*p)) p++;
// Must have consumed entire string
if (*p != '\0') return -1;
*out = 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 <stddef.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int field = 0;
int in_quotes = 0;
size_t out_pos = 0;
while (*p != '\0') {
if (field == idx) {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
if (*p == '"') {
if (p[1] == '"') {
p++;
if (out_pos < cap - 1) {
out[out_pos++] = '"';
}
} else {
in_quotes = !in_quotes;
}
}
} else {
if (*p == '"') {
if (p[1] == '"') {
p++;
} else {
in_quotes = !in_quotes;
}
}
}
if (!in_quotes && *p == ',') {
if (field == idx) {
break;
}
field++;
p++;
continue;
}
p++;
}
if (field == idx) {
out[out_pos] = '\0';
return (int)out_pos;
} else {
return -1;
}
}#include <stddef.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int field = 0;
int in_quotes = 0;
size_t out_pos = 0;
while (*p != '\0') {
if (field == idx) {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
if (*p == '"') {
if (p[1] == '"') {
p++;
if (out_pos < cap - 1) {
out[out_pos++] = '"';
}
} else {
in_quotes = !in_quotes;
}
}
} else {
if (*p == '"') {
if (p[1] == '"') {
p++;
} else {
in_quotes = !in_quotes;
}
}
}
if (!in_quotes && *p == ',') {
if (field == idx) {
break;
}
field++;
p++;
continue;
}
p++;
}
if (field == idx) {
out[out_pos] = '\0';
return (int)out_pos;
} else {
return -1;
}
}#include <stddef.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int field = 0;
int in_quotes = 0;
size_t out_pos = 0;
while (*p != '\0') {
if (field == idx) {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
if (*p == '"') {
if (p[1] == '"') {
p++;
if (out_pos < cap - 1) {
out[out_pos++] = '"';
}
} else {
in_quotes = !in_quotes;
}
}
} else {
if (*p == '"') {
if (p[1] == '"') {
p++;
} else {
in_quotes = !in_quotes;
}
}
}
if (!in_quotes && *p == ',') {
if (field == idx) {
break;
}
field++;
p++;
continue;
}
p++;
}
if (field == idx) {
out[out_pos] = '\0';
return (int)out_pos;
} else {
return -1;
}
}Served via llama.cpp Metal · 14.8 tok/s · 15.9 GB RAM · coding total 99/126
Implement in C11: `void reverse_string(char *s)`. Reverses s in place. Reply with only a C code block. No main function, no tests, no explanation.
void reverse_string(char *s) {
if (s == NULL) return;
size_t len = 0;
while (s[len] != '\0') {
len++;
}
for (size_t i = 0; i < len / 2; i++) {
char temp = s[i];
s[i] = s[len - 1 - i];
s[len - 1 - i] = temp;
}
}void reverse_string(char *s) {
if (s == NULL) return;
size_t len = 0;
while (s[len] != '\0') {
len++;
}
for (size_t i = 0; i < len / 2; i++) {
char temp = s[i];
s[i] = s[len - 1 - i];
s[len - 1 - i] = temp;
}
}void reverse_string(char *s) {
if (s == NULL) return;
size_t len = 0;
while (s[len] != '\0') {
len++;
}
for (size_t i = 0; i < len / 2; i++) {
char temp = s[i];
s[i] = s[len - 1 - i];
s[len - 1 - i] = temp;
}
}Implement in C11: `int count_words(const char *s)`. Counts words in s. Words are separated by one or more spaces; leading/trailing spaces possible. Reply with only a C code block. No main function, no tests, no explanation.
int count_words(const char *s) {
int count = 0;
int in_word = 0;
if (s == NULL) {
return 0;
}
while (*s) {
if (*s == ' ') {
in_word = 0;
} else {
if (!in_word) {
count++;
in_word = 1;
}
}
s++;
}
return count;
}int count_words(const char *s) {
int count = 0;
int in_word = 0;
if (s == NULL) {
return 0;
}
while (*s) {
if (*s == ' ') {
in_word = 0;
} else {
if (!in_word) {
count++;
in_word = 1;
}
}
s++;
}
return count;
}int count_words(const char *s) {
int count = 0;
int in_word = 0;
if (s == NULL) {
return 0;
}
while (*s) {
if (*s == ' ') {
in_word = 0;
} else {
if (!in_word) {
count++;
in_word = 1;
}
}
s++;
}
return count;
}Implement in C11: `void trim(char *s)`. Strips leading and trailing whitespace (space, tab, newline) from s in place. Reply with only a C code block. No main function, no tests, no explanation.
void trim(char *s) {
if (s == NULL) return;
// Find the first non-whitespace character
char *start = s;
while (*start == ' ' || *start == '\t' || *start == '\n') {
start++;
}
// If the string is all whitespace, terminate it
if (*start == '\0') {
*s = '\0';
return;
}
// Find the last non-whitespace character
char *end = s + strlen(s) - 1;
while (end > start && (*end == ' ' || *end == '\t' || *end == '\n')) {
end--;
}
// Move the trimmed substring to the beginning of s
size_t len = (size_t)(end - start + 1);
memmove(s, start, len);
s[len] = '\0';
}void trim(char *s) {
if (s == NULL) return;
// Find the first non-whitespace character
char *start = s;
while (*start == ' ' || *start == '\t' || *start == '\n') {
start++;
}
// If the string is all whitespace, terminate it
if (*start == '\0') {
*s = '\0';
return;
}
// Find the last non-whitespace character
char *end = s + strlen(s) - 1;
while (end > start && (*end == ' ' || *end == '\t' || *end == '\n')) {
end--;
}
// Move the trimmed substring to the beginning of s
size_t len = (size_t)(end - start + 1);
memmove(s, start, len);
s[len] = '\0';
}void trim(char *s) {
if (s == NULL) return;
// Find the first non-whitespace character
char *start = s;
while (*start == ' ' || *start == '\t' || *start == '\n') {
start++;
}
// If the string is all whitespace, terminate it
if (*start == '\0') {
*s = '\0';
return;
}
// Find the last non-whitespace character
char *end = s + strlen(s) - 1;
while (end > start && (*end == ' ' || *end == '\t' || *end == '\n')) {
end--;
}
// Move the trimmed substring to the beginning of s
size_t len = (size_t)(end - start + 1);
memmove(s, start, len);
s[len] = '\0';
}Implement in C11: `int atoi_strict(const char *s, int *out)`. Parses an optionally negative decimal integer from s into out. Returns 0 on success, -1 if s is not exactly one valid integer (empty, junk, trailing chars). No overflow tests. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
#include <stddef.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
int sign = 1;
long long val = 0;
// Skip leading whitespace
while (isspace((unsigned char)*p)) {
p++;
}
// Check for empty string after whitespace
if (*p == '\0') {
return -1;
}
// Handle optional sign
if (*p == '+') {
p++;
} else if (*p == '-') {
sign = -1;
p++;
}
// Check if there are digits
if (!isdigit((unsigned char)*p)) {
return -1;
}
// Parse digits
while (isdigit((unsigned char)*p)) {
val = val * 10 + (*p - '0');
p++;
}
// Check for trailing characters (must be end of string)
if (*p != '\0') {
return -1;
}
// Apply sign
val *= sign;
// Store result
*out = (int)val;
return 0;
}#include <ctype.h>
#include <stddef.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
int sign = 1;
long long val = 0;
// Skip leading whitespace
while (isspace((unsigned char)*p)) {
p++;
}
// Check for empty string after whitespace
if (*p == '\0') {
return -1;
}
// Handle optional sign
if (*p == '+') {
p++;
} else if (*p == '-') {
sign = -1;
p++;
}
// Check if there are digits
if (!isdigit((unsigned char)*p)) {
return -1;
}
// Parse digits
while (isdigit((unsigned char)*p)) {
val = val * 10 + (*p - '0');
p++;
}
// Check for trailing characters (must be end of string)
if (*p != '\0') {
return -1;
}
// Apply sign
val *= sign;
// Store result
*out = (int)val;
return 0;
}#include <ctype.h>
#include <stddef.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
int sign = 1;
long long val = 0;
// Skip leading whitespace
while (isspace((unsigned char)*p)) {
p++;
}
// Check for empty string after whitespace
if (*p == '\0') {
return -1;
}
// Handle optional sign
if (*p == '+') {
p++;
} else if (*p == '-') {
sign = -1;
p++;
}
// Check if there are digits
if (!isdigit((unsigned char)*p)) {
return -1;
}
// Parse digits
while (isdigit((unsigned char)*p)) {
val = val * 10 + (*p - '0');
p++;
}
// Check for trailing characters (must be end of string)
if (*p != '\0') {
return -1;
}
// Apply sign
val *= sign;
// Store result
*out = (int)val;
return 0;
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int current_idx = 0;
while (*p != '\0') {
if (current_idx == idx) {
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++;
}
}
/* Skip any trailing characters until comma or end of line */
while (*p != ',' && *p != '\0') {
p++;
}
out[out_len] = '\0';
return (int)out_len;
} else {
/* Unquoted field */
while (*p != ',' && *p != '\0') {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
out[out_len] = '\0';
return (int)out_len;
}
}
/* Skip to next field */
if (*p == '"') {
/* Quoted field: skip until closing quote */
p++; /* skip opening quote */
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
p += 2; /* skip escaped quote */
} else {
p++; /* skip closing quote */
break;
}
} else {
p++;
}
}
/* Skip any trailing characters until comma or end of line */
while (*p != ',' && *p != '\0') {
p++;
}
} else {
/* Unquoted field: skip until comma or end of line */
while (*p != ',' && *p != '\0') {
p++;
}
}
if (*p == ',') {
p++; /* skip comma */
}
current_idx++;
}
/* If we reached here, idx was out of range */
return -1;
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int current_idx = 0;
while (*p != '\0') {
if (current_idx == idx) {
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++;
}
}
/* Skip any trailing characters until comma or end of line */
while (*p != ',' && *p != '\0') {
p++;
}
out[out_len] = '\0';
return (int)out_len;
} else {
/* Unquoted field */
while (*p != ',' && *p != '\0') {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
out[out_len] = '\0';
return (int)out_len;
}
}
/* Skip to next field */
if (*p == '"') {
/* Quoted field: skip until closing quote */
p++; /* skip opening quote */
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
p += 2; /* skip escaped quote */
} else {
p++; /* skip closing quote */
break;
}
} else {
p++;
}
}
/* Skip any trailing characters until comma or end of line */
while (*p != ',' && *p != '\0') {
p++;
}
} else {
/* Unquoted field: skip until comma or end of line */
while (*p != ',' && *p != '\0') {
p++;
}
}
if (*p == ',') {
p++; /* skip comma */
}
current_idx++;
}
/* If we reached here, idx was out of range */
return -1;
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int current_idx = 0;
while (*p != '\0') {
if (current_idx == idx) {
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++;
}
}
/* Skip any trailing characters until comma or end of line */
while (*p != ',' && *p != '\0') {
p++;
}
out[out_len] = '\0';
return (int)out_len;
} else {
/* Unquoted field */
while (*p != ',' && *p != '\0') {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
out[out_len] = '\0';
return (int)out_len;
}
}
/* Skip to next field */
if (*p == '"') {
/* Quoted field: skip until closing quote */
p++; /* skip opening quote */
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
p += 2; /* skip escaped quote */
} else {
p++; /* skip closing quote */
break;
}
} else {
p++;
}
}
/* Skip any trailing characters until comma or end of line */
while (*p != ',' && *p != '\0') {
p++;
}
} else {
/* Unquoted field: skip until comma or end of line */
while (*p != ',' && *p != '\0') {
p++;
}
}
if (*p == ',') {
p++; /* skip comma */
}
current_idx++;
}
/* If we reached here, idx was out of range */
return -1;
}Served via llama.cpp Metal · 63.1 tok/s · 17.9 GB RAM · coding total 96/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) return;
// Strip leading whitespace
while (isspace((unsigned char)*s)) {
s++;
}
// Strip trailing whitespace
char *end = s;
while (*end != '\0') {
end++;
}
end--;
while (end > s && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
// Move the trimmed string to the beginning if leading whitespace was removed
if (s != end + 1) {
char *dst = s;
char *src = s;
while (*src) {
*dst++ = *src++;
}
*dst = '\0';
}
}#include <ctype.h>
void trim(char *s) {
if (!s) return;
// Strip leading whitespace
while (isspace((unsigned char)*s)) {
s++;
}
// Strip trailing whitespace
char *end = s;
while (*end != '\0') {
end++;
}
end--;
while (end > s && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
// Move the trimmed string to the beginning if leading whitespace was removed
if (s != end + 1) {
char *dst = s;
char *src = s;
while (*src) {
*dst++ = *src++;
}
*dst = '\0';
}
}#include <ctype.h>
void trim(char *s) {
if (!s) return;
// Strip leading whitespace
while (isspace((unsigned char)*s)) {
s++;
}
// Strip trailing whitespace
char *end = s;
while (*end != '\0') {
end++;
}
end--;
while (end > s && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
// Move the trimmed string to the beginning if leading whitespace was removed
if (s != end + 1) {
char *dst = s;
char *src = s;
while (*src) {
*dst++ = *src++;
}
*dst = '\0';
}
}Implement in C11: `int atoi_strict(const char *s, int *out)`. Parses an optionally negative decimal integer from s into out. Returns 0 on success, -1 if s is not exactly one valid integer (empty, junk, trailing chars). No overflow tests. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
int sign = 1;
// Skip leading whitespace
while (isspace((unsigned char)*p)) {
p++;
}
// Handle optional sign
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
// Check if there is at least one digit
if (!isdigit((unsigned char)*p)) {
return -1;
}
// Parse digits
int result = 0;
while (isdigit((unsigned char)*p)) {
int digit = *p - '0';
result = result * 10 + digit;
p++;
}
// Check for trailing non-whitespace characters
if (*p != '\0') {
// Allow trailing whitespace only if it was part of the initial skip?
// The spec says "exactly one valid integer", so trailing chars (including whitespace) make it invalid.
// However, typically atoi ignores trailing whitespace. But the spec says "trailing chars" are invalid.
// Let's interpret "trailing chars" as any non-whitespace after the number.
// But wait, the initial skip consumed leading whitespace. If there is trailing whitespace, is it invalid?
// The spec says: "s is not exactly one valid integer (empty, junk, trailing chars)".
// This implies that if there are any characters after the integer (even whitespace), it's invalid.
// Let's check if *p is not null terminator.
return -1;
}
*out = sign * result;
return 0;
}#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
int sign = 1;
// Skip leading whitespace
while (isspace((unsigned char)*p)) {
p++;
}
// Handle optional sign
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
// Check if there is at least one digit
if (!isdigit((unsigned char)*p)) {
return -1;
}
// Parse digits
int result = 0;
while (isdigit((unsigned char)*p)) {
int digit = *p - '0';
result = result * 10 + digit;
p++;
}
// Check for trailing non-whitespace characters
if (*p != '\0') {
// Allow trailing whitespace only if it was part of the initial skip?
// The spec says "exactly one valid integer", so trailing chars (including whitespace) make it invalid.
// However, typically atoi ignores trailing whitespace. But the spec says "trailing chars" are invalid.
// Let's interpret "trailing chars" as any non-whitespace after the number.
// But wait, the initial skip consumed leading whitespace. If there is trailing whitespace, is it invalid?
// The spec says: "s is not exactly one valid integer (empty, junk, trailing chars)".
// This implies that if there are any characters after the integer (even whitespace), it's invalid.
// Let's check if *p is not null terminator.
return -1;
}
*out = sign * result;
return 0;
}#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
int sign = 1;
// Skip leading whitespace
while (isspace((unsigned char)*p)) {
p++;
}
// Handle optional sign
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
// Check if there is at least one digit
if (!isdigit((unsigned char)*p)) {
return -1;
}
// Parse digits
int result = 0;
while (isdigit((unsigned char)*p)) {
int digit = *p - '0';
result = result * 10 + digit;
p++;
}
// Check for trailing non-whitespace characters
if (*p != '\0') {
// Allow trailing whitespace only if it was part of the initial skip?
// The spec says "exactly one valid integer", so trailing chars (including whitespace) make it invalid.
// However, typically atoi ignores trailing whitespace. But the spec says "trailing chars" are invalid.
// Let's interpret "trailing chars" as any non-whitespace after the number.
// But wait, the initial skip consumed leading whitespace. If there is trailing whitespace, is it invalid?
// The spec says: "s is not exactly one valid integer (empty, junk, trailing chars)".
// This implies that if there are any characters after the integer (even whitespace), it's invalid.
// Let's check if *p is not null terminator.
return -1;
}
*out = sign * result;
return 0;
}Implement in C11: `int glob_match(const char *pat, const char *str)`. Returns 1 if str matches the shell-style pattern pat, else 0. '*' matches any sequence (including empty), '?' matches exactly one character. All other characters match literally. The whole string must match. Reply with only a C code block. No main function, no tests, no explanation.
int glob_match(const char *pat, const char *str) {
const char *star = NULL;
const char *str_match = NULL;
while (*str) {
switch (*pat) {
case '?':
str++;
pat++;
break;
case '*':
star = pat++;
str_match = str;
break;
default:
if (*pat != *str) {
if (star) {
pat = star + 1;
str_match++;
str = str_match;
} else {
return 0;
}
} else {
str++;
pat++;
}
break;
}
}
while (*pat == '*') {
pat++;
}
return *pat == '\0';
}int glob_match(const char *pat, const char *str) {
const char *star = NULL;
const char *str_match = NULL;
while (*str) {
switch (*pat) {
case '?':
str++;
pat++;
break;
case '*':
star = pat++;
str_match = str;
break;
default:
if (*pat != *str) {
if (star) {
pat = star + 1;
str_match++;
str = str_match;
} else {
return 0;
}
} else {
str++;
pat++;
}
break;
}
}
while (*pat == '*') {
pat++;
}
return *pat == '\0';
}int glob_match(const char *pat, const char *str) {
const char *star = NULL;
const char *str_match = NULL;
while (*str) {
switch (*pat) {
case '?':
str++;
pat++;
break;
case '*':
star = pat++;
str_match = str;
break;
default:
if (*pat != *str) {
if (star) {
pat = star + 1;
str_match++;
str = str_match;
} else {
return 0;
}
} else {
str++;
pat++;
}
break;
}
}
while (*pat == '*') {
pat++;
}
return *pat == '\0';
}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 (current_idx < idx) {
// Skip current field
if (*p == '"') {
// Quoted field
p++;
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
p += 2; // Skip escaped quote
} else {
p++; // End of quoted field
break;
}
} else {
p++;
}
}
} else {
// Unquoted field
while (*p != '\0' && *p != ',') {
p++;
}
}
// Skip comma if not at end
if (*p == ',') {
p++;
} else {
// End of line before reaching idx
return -1;
}
current_idx++;
}
// Now p points to the start of field idx
size_t len = 0;
int in_quotes = 0;
if (*p == '"') {
in_quotes = 1;
p++;
}
while (*p != '\0' && *p != ',') {
if (in_quotes) {
if (*p == '"') {
if (*(p + 1) == '"') {
if (len < cap - 1) {
out[len++] = '"';
}
p += 2;
} else {
p++; // End of quoted field
break;
}
} else {
if (len < cap - 1) {
out[len++] = *p;
}
p++;
}
} else {
if (len < cap - 1) {
out[len++] = *p;
}
p++;
}
}
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) {
return -1;
}
const char *p = line;
int current_idx = 0;
while (current_idx < idx) {
// Skip current field
if (*p == '"') {
// Quoted field
p++;
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
p += 2; // Skip escaped quote
} else {
p++; // End of quoted field
break;
}
} else {
p++;
}
}
} else {
// Unquoted field
while (*p != '\0' && *p != ',') {
p++;
}
}
// Skip comma if not at end
if (*p == ',') {
p++;
} else {
// End of line before reaching idx
return -1;
}
current_idx++;
}
// Now p points to the start of field idx
size_t len = 0;
int in_quotes = 0;
if (*p == '"') {
in_quotes = 1;
p++;
}
while (*p != '\0' && *p != ',') {
if (in_quotes) {
if (*p == '"') {
if (*(p + 1) == '"') {
if (len < cap - 1) {
out[len++] = '"';
}
p += 2;
} else {
p++; // End of quoted field
break;
}
} else {
if (len < cap - 1) {
out[len++] = *p;
}
p++;
}
} else {
if (len < cap - 1) {
out[len++] = *p;
}
p++;
}
}
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) {
return -1;
}
const char *p = line;
int current_idx = 0;
while (current_idx < idx) {
// Skip current field
if (*p == '"') {
// Quoted field
p++;
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
p += 2; // Skip escaped quote
} else {
p++; // End of quoted field
break;
}
} else {
p++;
}
}
} else {
// Unquoted field
while (*p != '\0' && *p != ',') {
p++;
}
}
// Skip comma if not at end
if (*p == ',') {
p++;
} else {
// End of line before reaching idx
return -1;
}
current_idx++;
}
// Now p points to the start of field idx
size_t len = 0;
int in_quotes = 0;
if (*p == '"') {
in_quotes = 1;
p++;
}
while (*p != '\0' && *p != ',') {
if (in_quotes) {
if (*p == '"') {
if (*(p + 1) == '"') {
if (len < cap - 1) {
out[len++] = '"';
}
p += 2;
} else {
p++; // End of quoted field
break;
}
} else {
if (len < cap - 1) {
out[len++] = *p;
}
p++;
}
} else {
if (len < cap - 1) {
out[len++] = *p;
}
p++;
}
}
out[len] = '\0';
return (int)len;
}Served via llama.cpp Metal · 58.1 tok/s · 19.1 GB RAM · coding total 96/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: `int parse_csv_ints(const char *s, int *out, int max)`. Parses comma-separated integers from s into out (at most max). Returns the count stored. Input is well-formed (digits, optional leading minus, commas). Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
const char *p = s;
int sign = 1;
long val = 0;
int in_number = 0;
while (*p && count < max) {
if (isdigit((unsigned char)*p)) {
if (!in_number) {
in_number = 1;
sign = 1;
val = 0;
}
val = val * 10 + (*p - '0');
} else if (*p == '-' && !in_number) {
sign = -1;
in_number = 1;
} else if (*p == ',' || *p == '\0') {
if (in_number) {
out[count++] = (int)(val * sign);
in_number = 0;
}
if (*p == '\0') break;
}
p++;
}
return count;
}#include <ctype.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
const char *p = s;
int sign = 1;
long val = 0;
int in_number = 0;
while (*p && count < max) {
if (isdigit((unsigned char)*p)) {
if (!in_number) {
in_number = 1;
sign = 1;
val = 0;
}
val = val * 10 + (*p - '0');
} else if (*p == '-' && !in_number) {
sign = -1;
in_number = 1;
} else if (*p == ',' || *p == '\0') {
if (in_number) {
out[count++] = (int)(val * sign);
in_number = 0;
}
if (*p == '\0') break;
}
p++;
}
return count;
}#include <ctype.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
const char *p = s;
int sign = 1;
long val = 0;
int in_number = 0;
while (*p && count < max) {
if (isdigit((unsigned char)*p)) {
if (!in_number) {
in_number = 1;
sign = 1;
val = 0;
}
val = val * 10 + (*p - '0');
} else if (*p == '-' && !in_number) {
sign = -1;
in_number = 1;
} else if (*p == ',' || *p == '\0') {
if (in_number) {
out[count++] = (int)(val * sign);
in_number = 0;
}
if (*p == '\0') break;
}
p++;
}
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.
#include <limits.h>
int cmp_desc(const void *a, const void *b) {
int ia = *(const int *)a;
int ib = *(const int *)b;
if (ia > ib) {
if (ia > 0 && ib < 0) return 1;
if (ia < 0 && ib > 0) return -1;
if (ia - ib > 0) return 1;
return 1;
} else if (ia < ib) {
if (ia < 0 && ib > 0) return -1;
if (ia > 0 && ib < 0) return 1;
if (ia - ib < 0) return -1;
return -1;
}
return 0;
}#include <limits.h>
int cmp_desc(const void *a, const void *b) {
int ia = *(const int *)a;
int ib = *(const int *)b;
if (ia > ib) {
if (ia > 0 && ib < 0) return 1;
if (ia < 0 && ib > 0) return -1;
if (ia - ib > 0) return 1;
return 1;
} else if (ia < ib) {
if (ia < 0 && ib > 0) return -1;
if (ia > 0 && ib < 0) return 1;
if (ia - ib < 0) return -1;
return -1;
}
return 0;
}#include <limits.h>
int cmp_desc(const void *a, const void *b) {
int ia = *(const int *)a;
int ib = *(const int *)b;
if (ia > ib) {
if (ia > 0 && ib < 0) return 1;
if (ia < 0 && ib > 0) return -1;
if (ia - ib > 0) return 1;
return 1;
} else if (ia < ib) {
if (ia < 0 && ib > 0) return -1;
if (ia > 0 && ib < 0) return 1;
if (ia - ib < 0) return -1;
return -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 == '\0') {
return -1;
}
int sign = 1;
if (*p == '-') {
sign = -1;
p++;
if (*p == '\0') {
return -1;
}
} else if (*p == '+') {
p++;
if (*p == '\0') {
return -1;
}
}
if (!isdigit((unsigned char)*p)) {
return -1;
}
while (isdigit((unsigned char)*p)) {
p++;
}
if (*p != '\0') {
return -1;
}
*out = sign * (p - s - (s[0] == '-' || s[0] == '+') ? 1 : 0);
return 0;
}#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
if (*p == '\0') {
return -1;
}
int sign = 1;
if (*p == '-') {
sign = -1;
p++;
if (*p == '\0') {
return -1;
}
} else if (*p == '+') {
p++;
if (*p == '\0') {
return -1;
}
}
if (!isdigit((unsigned char)*p)) {
return -1;
}
while (isdigit((unsigned char)*p)) {
p++;
}
if (*p != '\0') {
return -1;
}
*out = sign * (p - s - (s[0] == '-' || s[0] == '+') ? 1 : 0);
return 0;
}#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
if (*p == '\0') {
return -1;
}
int sign = 1;
if (*p == '-') {
sign = -1;
p++;
if (*p == '\0') {
return -1;
}
} else if (*p == '+') {
p++;
if (*p == '\0') {
return -1;
}
}
if (!isdigit((unsigned char)*p)) {
return -1;
}
while (isdigit((unsigned char)*p)) {
p++;
}
if (*p != '\0') {
return -1;
}
*out = sign * (p - s - (s[0] == '-' || s[0] == '+') ? 1 : 0);
return 0;
}Implement in C11: `int glob_match(const char *pat, const char *str)`. Returns 1 if str matches the shell-style pattern pat, else 0. '*' matches any sequence (including empty), '?' matches exactly one character. All other characters match literally. The whole string must match. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdbool.h>
int glob_match(const char *pat, const char *str) {
const char *p = pat;
const char *s = str;
const char *star = NULL;
const char *match = NULL;
while (*s) {
if (*p == '*') {
star = p;
match = s;
p++;
} else if (*p == '?' || *p == *s) {
p++;
s++;
} else if (star) {
p = star + 1;
match++;
s = match;
} else {
return 0;
}
}
while (*p == '*') {
p++;
}
return *p == '\0';
}#include <stdbool.h>
int glob_match(const char *pat, const char *str) {
const char *p = pat;
const char *s = str;
const char *star = NULL;
const char *match = NULL;
while (*s) {
if (*p == '*') {
star = p;
match = s;
p++;
} else if (*p == '?' || *p == *s) {
p++;
s++;
} else if (star) {
p = star + 1;
match++;
s = match;
} else {
return 0;
}
}
while (*p == '*') {
p++;
}
return *p == '\0';
}#include <stdbool.h>
int glob_match(const char *pat, const char *str) {
const char *p = pat;
const char *s = str;
const char *star = NULL;
const char *match = NULL;
while (*s) {
if (*p == '*') {
star = p;
match = s;
p++;
} else if (*p == '?' || *p == *s) {
p++;
s++;
} else if (star) {
p = star + 1;
match++;
s = match;
} else {
return 0;
}
}
while (*p == '*') {
p++;
}
return *p == '\0';
}Served via llama.cpp Metal · 23.5 tok/s · 16.3 GB RAM · coding total 94/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 = 463648264; // (INT_MAX + 1) % 10
*buf++ = '4';
value = 746364826; // (INT_MAX + 1) / 10
*buf++ = '6';
value = 364826482; // (INT_MAX + 1) % 10
*buf++ = '3';
value = 64826482; // (INT_MAX + 1) / 10
*buf++ = '6';
value = 4826482; // (INT_MAX + 1) % 10
*buf++ = '4';
value = 826482; // (INT_MAX + 1) / 10
*buf++ = '8';
value = 26482; // (INT_MAX + 1) % 10
*buf++ = '2';
value = 2648; // (INT_MAX + 1) / 10
*buf++ = '6';
value = 648; // (INT_MAX + 1) % 10
*buf++ = '6';
value = 64; // (INT_MAX + 1) / 10
*buf++ = '4';
value = 4; // (INT_MAX + 1) % 10
*buf++ = '4';
*buf = '\0';
return;
}
value = -value;
}
char temp[12];
int i = 0;
do {
temp[i++] = '0' + (value % 10);
value /= 10;
} while (value > 0);
if (is_negative) {
*buf++ = '-';
}
while (i > 0) {
*buf++ = temp[--i];
}
*buf = '\0';
}#include <limits.h>
#include <stdbool.h>
void itoa(int value, char *buf) {
bool is_negative = false;
if (value < 0) {
is_negative = true;
if (value == INT_MIN) {
// Handle INT_MIN separately to avoid overflow
*buf++ = '-';
value = -(value + 1);
*buf++ = '2';
value = 1431655765; // (INT_MAX + 1) / 10
*buf++ = '1';
value = 463648264; // (INT_MAX + 1) % 10
*buf++ = '4';
value = 746364826; // (INT_MAX + 1) / 10
*buf++ = '6';
value = 364826482; // (INT_MAX + 1) % 10
*buf++ = '3';
value = 64826482; // (INT_MAX + 1) / 10
*buf++ = '6';
value = 4826482; // (INT_MAX + 1) % 10
*buf++ = '4';
value = 826482; // (INT_MAX + 1) / 10
*buf++ = '8';
value = 26482; // (INT_MAX + 1) % 10
*buf++ = '2';
value = 2648; // (INT_MAX + 1) / 10
*buf++ = '6';
value = 648; // (INT_MAX + 1) % 10
*buf++ = '6';
value = 64; // (INT_MAX + 1) / 10
*buf++ = '4';
value = 4; // (INT_MAX + 1) % 10
*buf++ = '4';
*buf = '\0';
return;
}
value = -value;
}
char temp[12];
int i = 0;
do {
temp[i++] = '0' + (value % 10);
value /= 10;
} while (value > 0);
if (is_negative) {
*buf++ = '-';
}
while (i > 0) {
*buf++ = temp[--i];
}
*buf = '\0';
}#include <limits.h>
#include <stdbool.h>
void itoa(int value, char *buf) {
bool is_negative = false;
if (value < 0) {
is_negative = true;
if (value == INT_MIN) {
// Handle INT_MIN separately to avoid overflow
*buf++ = '-';
value = -(value + 1);
*buf++ = '2';
value = 1431655765; // (INT_MAX + 1) / 10
*buf++ = '1';
value = 463648264; // (INT_MAX + 1) % 10
*buf++ = '4';
value = 746364826; // (INT_MAX + 1) / 10
*buf++ = '6';
value = 364826482; // (INT_MAX + 1) % 10
*buf++ = '3';
value = 64826482; // (INT_MAX + 1) / 10
*buf++ = '6';
value = 4826482; // (INT_MAX + 1) % 10
*buf++ = '4';
value = 826482; // (INT_MAX + 1) / 10
*buf++ = '8';
value = 26482; // (INT_MAX + 1) % 10
*buf++ = '2';
value = 2648; // (INT_MAX + 1) / 10
*buf++ = '6';
value = 648; // (INT_MAX + 1) % 10
*buf++ = '6';
value = 64; // (INT_MAX + 1) / 10
*buf++ = '4';
value = 4; // (INT_MAX + 1) % 10
*buf++ = '4';
*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: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
#include <string.h>
#include <ctype.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (cap == 0) return -1;
const char *p = line;
int field = 0;
int in_quotes = 0;
size_t out_pos = 0;
while (*p && field <= idx) {
if (in_quotes) {
if (*p == '"') {
const char *next = p + 1;
if (*next == '"') {
if (out_pos < cap - 1) out[out_pos++] = '"';
p = next + 1;
} else {
in_quotes = 0;
p++;
}
} else {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
} else {
if (*p == '"') {
in_quotes = 1;
p++;
} else if (*p == ',') {
field++;
p++;
if (field > idx) break;
} else {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
}
}
if (field != idx) return -1;
if (out_pos < cap) out[out_pos] = '\0';
return (int)out_pos;
}#include <string.h>
#include <ctype.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (cap == 0) return -1;
const char *p = line;
int field = 0;
int in_quotes = 0;
size_t out_pos = 0;
while (*p && field <= idx) {
if (in_quotes) {
if (*p == '"') {
const char *next = p + 1;
if (*next == '"') {
if (out_pos < cap - 1) out[out_pos++] = '"';
p = next + 1;
} else {
in_quotes = 0;
p++;
}
} else {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
} else {
if (*p == '"') {
in_quotes = 1;
p++;
} else if (*p == ',') {
field++;
p++;
if (field > idx) break;
} else {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
}
}
if (field != idx) return -1;
if (out_pos < cap) out[out_pos] = '\0';
return (int)out_pos;
}#include <string.h>
#include <ctype.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (cap == 0) return -1;
const char *p = line;
int field = 0;
int in_quotes = 0;
size_t out_pos = 0;
while (*p && field <= idx) {
if (in_quotes) {
if (*p == '"') {
const char *next = p + 1;
if (*next == '"') {
if (out_pos < cap - 1) out[out_pos++] = '"';
p = next + 1;
} else {
in_quotes = 0;
p++;
}
} else {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
} else {
if (*p == '"') {
in_quotes = 1;
p++;
} else if (*p == ',') {
field++;
p++;
if (field > idx) break;
} else {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
}
}
if (field != idx) return -1;
if (out_pos < cap) out[out_pos] = '\0';
return (int)out_pos;
}Served via llama.cpp Metal · 48.4 tok/s · 17.9 GB RAM · coding total 94/126
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.
#include <string.h>
const char *fizzbuzz(int n) {
static char buffer[32];
if (n % 15 == 0) {
strcpy(buffer, "FizzBuzz");
} else if (n % 3 == 0) {
strcpy(buffer, "Fizz");
} else if (n % 5 == 0) {
strcpy(buffer, "Buzz");
} else {
snprintf(buffer, sizeof(buffer), "%d", n);
}
return buffer;
}#include <string.h>
const char *fizzbuzz(int n) {
static char buffer[32];
if (n % 15 == 0) {
strcpy(buffer, "FizzBuzz");
} else if (n % 3 == 0) {
strcpy(buffer, "Fizz");
} else if (n % 5 == 0) {
strcpy(buffer, "Buzz");
} else {
snprintf(buffer, sizeof(buffer), "%d", n);
}
return buffer;
}#include <string.h>
const char *fizzbuzz(int n) {
static char buffer[32];
if (n % 15 == 0) {
strcpy(buffer, "FizzBuzz");
} else if (n % 3 == 0) {
strcpy(buffer, "Fizz");
} else if (n % 5 == 0) {
strcpy(buffer, "Buzz");
} else {
snprintf(buffer, sizeof(buffer), "%d", n);
}
return buffer;
}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' && *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;
}#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 glob_match(const char *pat, const char *str)`. Returns 1 if str matches the shell-style pattern pat, else 0. '*' matches any sequence (including empty), '?' matches exactly one character. All other characters match literally. The whole string must match. Reply with only a C code block. No main function, no tests, no explanation.
#include <stddef.h>
static int match_star(const char *s, const char *p, const char **ss, const char **sp);
int glob_match(const char *pat, const char *str) {
const char *s = str;
const char *p = pat;
while (*s) {
if (*p == '*') {
if (match_star(s, p, &s, &p)) {
return 1;
}
return 0;
} else if (*p == '?' || *p == *s) {
s++;
p++;
} else {
return 0;
}
}
while (*p == '*') {
p++;
}
return *p == '\0';
}
static int match_star(const char *s, const char *p, const char **ss, const char **sp) {
const char *star = *sp;
const char *match = *ss;
const char *tmp;
while (*match) {
if (*p == '?' || *p == *match) {
p++;
match++;
} else {
if (*p != '*') {
return 0;
}
p++;
tmp = match;
if (match_star(match, p, &match, &p)) {
return 1;
}
match = tmp + 1;
}
}
while (*p == '*') {
p++;
}
if (*p == '\0') {
*ss = match;
*sp = p;
return 1;
}
return 0;
}#include <stddef.h>
static int match_star(const char *s, const char *p, const char **ss, const char **sp);
int glob_match(const char *pat, const char *str) {
const char *s = str;
const char *p = pat;
while (*s) {
if (*p == '*') {
if (match_star(s, p, &s, &p)) {
return 1;
}
return 0;
} else if (*p == '?' || *p == *s) {
s++;
p++;
} else {
return 0;
}
}
while (*p == '*') {
p++;
}
return *p == '\0';
}
static int match_star(const char *s, const char *p, const char **ss, const char **sp) {
const char *star = *sp;
const char *match = *ss;
const char *tmp;
while (*match) {
if (*p == '?' || *p == *match) {
p++;
match++;
} else {
if (*p != '*') {
return 0;
}
p++;
tmp = match;
if (match_star(match, p, &match, &p)) {
return 1;
}
match = tmp + 1;
}
}
if (*p == '*') {
p++;
}
*ss = match;
*sp = p;
return 1;
}#include <stddef.h>
static int match_star(const char *s, const char *p, const char **ss, const char **sp);
int glob_match(const char *pat, const char *str) {
const char *s = str;
const char *p = pat;
while (*s) {
if (*p == '*') {
if (match_star(s, p, &s, &p)) {
return 1;
}
return 0;
} else if (*p == '?' || *p == *s) {
s++;
p++;
} else {
return 0;
}
}
while (*p == '*') {
p++;
}
return *p == '\0';
}
static int match_star(const char *s, const char *p, const char **ss, const char **sp) {
const char *star = *sp;
const char *match = *ss;
const char *tmp;
while (*match) {
if (*p == '?' || *p == *match) {
p++;
match++;
} else {
if (*p != '*') {
return 0;
}
p++;
tmp = match;
if (match_star(match, p, &match, &p)) {
return 1;
}
match = tmp + 1;
}
}
if (*p == '*') {
p++;
}
*ss = match;
*sp = p;
return 1;
}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) {
p++;
while (*p != '\0' && *p != ',' && *p != '\n') {
if (*p == '"') {
if (*(p + 1) == '"') {
p++;
} else {
break;
}
}
p++;
}
end = p;
}
if (start == NULL || end == NULL) {
return -1;
}
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) {
p++;
while (*p != '\0' && *p != ',' && *p != '\n') {
if (*p == '"') {
if (*(p + 1) == '"') {
p++;
} else {
break;
}
}
p++;
}
end = p;
}
if (start == NULL || end == NULL) {
return -1;
}
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) {
p++;
while (*p != '\0' && *p != ',' && *p != '\n') {
if (*p == '"') {
if (*(p + 1) == '"') {
p++;
} else {
break;
}
}
p++;
}
end = p;
}
if (start == NULL || end == NULL) {
return -1;
}
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 Metal · 52.2 tok/s · 59.7 GB RAM · coding total 91/126
Implement in C11: `void itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdbool.h>
#include <limits.h>
void itoa(int value, char *buf) {
if (buf == NULL) return;
bool is_negative = false;
if (value < 0) {
is_negative = true;
if (value == INT_MIN) {
value = -(value / 10);
buf[0] = '-';
buf[1] = '2';
buf += 2;
} else {
value = -value;
}
}
char *start = buf;
do {
*buf++ = '0' - value % 10;
value /= 10;
} while (value != 0);
if (is_negative) {
*buf++ = '-';
}
int len = buf - start;
for (int i = 0, j = len - 1; i < j; i++, j--) {
char tmp = start[i];
start[i] = start[j];
start[j] = tmp;
}
*buf = '\0';
}#include <stdbool.h>
#include <limits.h>
void itoa(int value, char *buf) {
if (buf == NULL) return;
bool is_negative = false;
if (value < 0) {
is_negative = true;
if (value == INT_MIN) {
value = -(value / 10);
buf[0] = '-';
buf[1] = '2';
buf += 2;
} else {
value = -value;
}
}
char *start = buf;
do {
*buf++ = '0' - value % 10;
value /= 10;
} while (value != 0);
if (is_negative) {
*buf++ = '-';
}
int len = buf - start;
for (int i = 0, j = len - 1; i < j; i++, j--) {
char tmp = start[i];
start[i] = start[j];
start[j] = tmp;
}
*buf = '\0';
}#include <stdbool.h>
#include <limits.h>
void itoa(int value, char *buf) {
if (buf == NULL) return;
bool is_negative = false;
if (value < 0) {
is_negative = true;
if (value == INT_MIN) {
value = -(value / 10);
buf[0] = '-';
buf[1] = '2';
buf += 2;
} else {
value = -value;
}
}
char *start = buf;
do {
*buf++ = '0' - value % 10;
value /= 10;
} while (value != 0);
if (is_negative) {
*buf++ = '-';
}
int len = buf - start;
for (int i = 0, j = len - 1; i < j; i++, j--) {
char tmp = start[i];
start[i] = start[j];
start[j] = tmp;
}
*buf = '\0';
}Implement in C11: `void rot13(char *s)`. Applies ROT13 to s in place: letters rotate by 13, all other characters unchanged. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
void rot13(char *s) {
if (s == NULL) return;
for (int i = 0; s[i] != '\0'; i++) {
char c = s[i];
if (isalpha(c)) {
char base = islower(c) ? 'a' : 'A';
c = (c - base + 13) % 26 + base;
}
s[i] = c;
}
}#include <ctype.h>
void rot13(char *s) {
if (s == NULL) return;
for (int i = 0; s[i] != '\0'; i++) {
char c = s[i];
if (isalpha(c)) {
char base = islower(c) ? 'a' : 'A';
c = (c - base + 13) % 26 + base;
}
s[i] = c;
}
}#include <ctype.h>
void rot13(char *s) {
if (s == NULL) return;
for (int i = 0; s[i] != '\0'; i++) {
char c = s[i];
if (isalpha(c)) {
char base = islower(c) ? 'a' : 'A';
c = (c - base + 13) % 26 + base;
}
s[i] = c;
}
}Implement in C11: `int parse_csv_ints(const char *s, int *out, int max)`. Parses comma-separated integers from s into out (at most max). Returns the count stored. Input is well-formed (digits, optional leading minus, commas). Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
#include <stdbool.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
bool negative = false;
int value = 0;
while (*s && count < max) {
if (*s == '-') {
negative = true;
s++;
} else if (*s == ',') {
if (negative) value = -value;
out[count++] = value;
value = 0;
negative = false;
s++;
} else {
value = value * 10 + (*s - '0');
s++;
}
}
if (count < max) {
if (negative) value = -value;
out[count++] = value;
}
return count;
}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) {
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;
}Served via llama.cpp Metal · 27.2 tok/s · 68.8 GB RAM · coding total 90/126
Implement in C11: `int count_words(const char *s)`. Counts words in s. Words are separated by one or more spaces; leading/trailing spaces possible. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
int count_words(const char *s) {
int count = 0;
int in_word = 0;
if (s == NULL) {
return 0;
}
while (*s != '\0') {
if (isspace((unsigned char)*s)) {
in_word = 0;
} else {
if (!in_word) {
count++;
in_word = 1;
}
}
s++;
}
return count;
}#include <ctype.h>
int count_words(const char *s) {
int count = 0;
int in_word = 0;
if (s == NULL) {
return 0;
}
while (*s != '\0') {
if (isspace((unsigned char)*s)) {
in_word = 0;
} else {
if (!in_word) {
count++;
in_word = 1;
}
}
s++;
}
return count;
}#include <ctype.h>
int count_words(const char *s) {
int count = 0;
int in_word = 0;
if (s == NULL) {
return 0;
}
while (*s != '\0') {
if (isspace((unsigned char)*s)) {
in_word = 0;
} else {
if (!in_word) {
count++;
in_word = 1;
}
}
s++;
}
return count;
}Implement in C11: `void rot13(char *s)`. Applies ROT13 to s in place: letters rotate by 13, all other characters unchanged. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
void rot13(char *s) {
if (s == NULL) return;
for (; *s != '\0'; ++s) {
if (isalpha((unsigned char)*s)) {
char base = isupper((unsigned char)*s) ? 'A' : 'a';
*s = (char)(((*s - base + 13) % 26) + base);
}
}
}#include <ctype.h>
void rot13(char *s) {
if (s == NULL) return;
for (; *s != '\0'; ++s) {
if (isalpha((unsigned char)*s)) {
char base = isupper((unsigned char)*s) ? 'A' : 'a';
*s = (char)(((*s - base + 13) % 26) + base);
}
}
}#include <ctype.h>
void rot13(char *s) {
if (s == NULL) return;
for (; *s != '\0'; ++s) {
if (isalpha((unsigned char)*s)) {
char base = isupper((unsigned char)*s) ? 'A' : 'a';
*s = (char)(((*s - base + 13) % 26) + base);
}
}
}Implement in C11: `int parse_csv_ints(const char *s, int *out, int max)`. Parses comma-separated integers from s into out (at most max). Returns the count stored. Input is well-formed (digits, optional leading minus, commas). Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
#include <limits.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
if (max <= 0 || s == NULL) {
return 0;
}
while (*s != '\0' && count < max) {
int sign = 1;
int val = 0;
int overflow = 0;
// Skip leading whitespace if any (though input is well-formed, robustness helps)
while (*s == ' ' || *s == '\t') {
s++;
}
if (*s == '\0') break;
// Handle sign
if (*s == '-') {
sign = -1;
s++;
} else if (*s == '+') {
s++;
}
// Parse digits
if (!isdigit((unsigned char)*s)) {
// If no digits found after optional sign, stop or skip?
// Problem says well-formed, so we assume digits follow sign if present.
// If we hit a comma or end immediately without digits, it's malformed per spec,
// but we'll just break to be safe.
break;
}
while (isdigit((unsigned char)*s)) {
int digit = *s - '0';
// Check for overflow before multiplying
if (val > (INT_MAX - digit) / 10) {
overflow = 1;
break;
}
val = val * 10 + digit;
s++;
}
if (overflow) {
// Handle overflow by clamping or breaking?
// Spec says well-formed, so overflow shouldn't happen on valid int input.
// We'll break to avoid undefined behavior.
break;
}
if (sign == -1) {
val = -val;
}
out[count++] = val;
// Skip comma if present
if (*s == ',') {
s++;
}
}
return count;
}#include <ctype.h>
#include <limits.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
if (max <= 0 || s == NULL) {
return 0;
}
while (*s != '\0' && count < max) {
int sign = 1;
int val = 0;
int overflow = 0;
// Skip leading whitespace if any (though input is well-formed, robustness helps)
while (*s == ' ' || *s == '\t') {
s++;
}
if (*s == '\0') break;
// Handle sign
if (*s == '-') {
sign = -1;
s++;
} else if (*s == '+') {
s++;
}
// Parse digits
if (!isdigit((unsigned char)*s)) {
// If no digits found after optional sign, stop or skip?
// Problem says well-formed, so we assume digits follow sign if present.
// If we hit a comma or end immediately without digits, it's malformed per spec,
// but we'll just break to be safe.
break;
}
while (isdigit((unsigned char)*s)) {
int digit = *s - '0';
// Check for overflow before multiplying
if (val > (INT_MAX - digit) / 10) {
overflow = 1;
break;
}
val = val * 10 + digit;
s++;
}
if (overflow) {
// Handle overflow by clamping or breaking?
// Spec says well-formed, so overflow shouldn't happen on valid int input.
// We'll break to avoid undefined behavior.
break;
}
if (sign == -1) {
val = -val;
}
out[count++] = val;
// Skip comma if present
if (*s == ',') {
s++;
}
}
return count;
}#include <ctype.h>
#include <limits.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
if (max <= 0 || s == NULL) {
return 0;
}
while (*s != '\0' && count < max) {
int sign = 1;
int val = 0;
int overflow = 0;
// Skip leading whitespace if any (though input is well-formed, robustness helps)
while (*s == ' ' || *s == '\t') {
s++;
}
if (*s == '\0') break;
// Handle sign
if (*s == '-') {
sign = -1;
s++;
} else if (*s == '+') {
s++;
}
// Parse digits
if (!isdigit((unsigned char)*s)) {
// If no digits found after optional sign, stop or skip?
// Problem says well-formed, so we assume digits follow sign if present.
// If we hit a comma or end immediately without digits, it's malformed per spec,
// but we'll just break to be safe.
break;
}
while (isdigit((unsigned char)*s)) {
int digit = *s - '0';
// Check for overflow before multiplying
if (val > (INT_MAX - digit) / 10) {
overflow = 1;
break;
}
val = val * 10 + digit;
s++;
}
if (overflow) {
// Handle overflow by clamping or breaking?
// Spec says well-formed, so overflow shouldn't happen on valid int input.
// We'll break to avoid undefined behavior.
break;
}
if (sign == -1) {
val = -val;
}
out[count++] = val;
// Skip comma if present
if (*s == ',') {
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 <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] = '\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>
#include <limits.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 number.
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;
}
// Apply sign
if (sign == -1) {
val = -val;
}
// No overflow tests required per spec, but we must ensure it fits in int if we were to check.
// Spec says "No overflow tests", so we just cast.
*out = (int)val;
return 0;
}#include <ctype.h>
#include <limits.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 number.
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;
}
// Apply sign
if (sign == -1) {
val = -val;
}
// No overflow tests required per spec, but we must ensure it fits in int if we were to check.
// Spec says "No overflow tests", so we just cast.
*out = (int)val;
return 0;
}#include <ctype.h>
#include <limits.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 number.
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;
}
// Apply sign
if (sign == -1) {
val = -val;
}
// No overflow tests required per spec, but we must ensure it fits in int if we were to check.
// Spec says "No overflow tests", so we just cast.
*out = (int)val;
return 0;
}Implement in C11: `int glob_match(const char *pat, const char *str)`. Returns 1 if str matches the shell-style pattern pat, else 0. '*' matches any sequence (including empty), '?' matches exactly one character. All other characters match literally. The whole string must match. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdbool.h>
int glob_match(const char *pat, const char *str) {
const char *star_pat = NULL;
const char *star_str = NULL;
while (*str != '\0') {
if (*pat == '*') {
star_pat = pat;
star_str = str;
pat++;
} else if (*pat == '?' || *pat == *str) {
pat++;
str++;
} else if (star_pat != NULL) {
pat = star_pat + 1;
str = star_str + 1;
star_str = str;
} else {
return 0;
}
}
while (*pat == '*') {
pat++;
}
return (*pat == '\0');
}#include <stdbool.h>
int glob_match(const char *pat, const char *str) {
const char *star_pat = NULL;
const char *star_str = NULL;
while (*str != '\0') {
if (*pat == '*') {
star_pat = pat;
star_str = str;
pat++;
} else if (*pat == '?' || *pat == *str) {
pat++;
str++;
} else if (star_pat != NULL) {
pat = star_pat + 1;
str = star_str + 1;
star_str = str;
} else {
return 0;
}
}
while (*pat == '*') {
pat++;
}
return (*pat == '\0');
}#include <stdbool.h>
int glob_match(const char *pat, const char *str) {
const char *star_pat = NULL;
const char *star_str = NULL;
while (*str != '\0') {
if (*pat == '*') {
star_pat = pat;
star_str = str;
pat++;
} else if (*pat == '?' || *pat == *str) {
pat++;
str++;
} else if (star_pat != NULL) {
pat = star_pat + 1;
str = star_str + 1;
star_str = str;
} else {
return 0;
}
}
while (*pat == '*') {
pat++;
}
return (*pat == '\0');
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
```c
#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (cap == 0) return -1;
int current_idx = 0;
const char *p = line;
size_t out_len = 0;
int in_quotes = 0;
while (*p != '\0') {
if (current_idx == idx) {
// We are inside the target field
if (*p == '"') {
if (!in_quotes) {
// Start of quoted field
in_quotes = 1;
p++;
continue;
} else {
// Inside quoted field
if (*(p + 1) == '"') {
// Escaped quote
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = 0;
p++;
continue;
}
}
} else if (*p == ',' && !in_quotes) {
// End of field
break;
} else {
// Regular character
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
} else {
// Skipping previous fields
if (*p == '"') {
if (!in_quotes) {
in_quotes = 1;
} else {
if (*(p + 1) == '"') {
p += 2;
continue;
} else {
in_quotes = 0;
}
}
} else if (*p == ',' && !in_quotes) {
current_idx++;
}
p++;
}
}
// Check if we found the field
if (current_idx != idx) {
// If we reached end of string and current_idx matches, it's the last field
if (current_idx == idx && out_len > 0) {
// Already processed
} else if (current_idx == idx && out_len == 0 && *p == '\0') {
// Empty last field
} else {
return -1;
}
}
// Re-scan to get exact length if we were in the target field
// Actually, the logic above processes the target field.
// But we need to handle the case where the field is empty or we just finished it.
// Let's refine the logic to be more robust.
// Reset and do a cleaner pass
p = line;
current_idx = 0;
out_len = 0;
in_quotes = 0;
int found = 0;
while (*p != '\0') {
if (current_idx == idx) {
found = 1;
if (*p == '"') {
if (!in_quotes) {
in_quotes = 1;
p++;
continue;
} else {
if (*(p + 1) == '"') {
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
in_quotes = 0;
p++;
continue;
}
}
} else if (*p == ',' && !in_quotes) {
break;
} else {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
} else {
if (*p == '"') {
if (!in_quotes) {
in_quotes = 1;
} else {
if (*(p + 1) == '"') {
p += 2;
continue;
} else {
in_quotes = 0;
}
}
} else if (*p == ',' && !in_quotes) {
current_idx++;
}
p++;
}
}
if (!found) {
// Check if it's the last field (empty or not)
if (current_idx == idx) {
found = 1;
}
}
if (!found) {
return -1```c
#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (cap == 0) return -1;
int current_idx = 0;
const char *p = line;
size_t out_len = 0;
int in_quotes = 0;
while (*p != '\0') {
if (current_idx == idx) {
// We are inside the target field
if (*p == '"') {
if (!in_quotes) {
// Start of quoted field
in_quotes = 1;
p++;
continue;
} else {
// Inside quoted field
if (*(p + 1) == '"') {
// Escaped quote
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = 0;
p++;
continue;
}
}
} else if (*p == ',' && !in_quotes) {
// End of field
break;
} else {
// Regular character
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
} else {
// Skipping previous fields
if (*p == '"') {
if (!in_quotes) {
in_quotes = 1;
} else {
if (*(p + 1) == '"') {
p += 2;
continue;
} else {
in_quotes = 0;
}
}
} else if (*p == ',' && !in_quotes) {
current_idx++;
}
p++;
}
}
// Check if we found the field
if (current_idx != idx) {
// If we reached end of string and current_idx matches, it's the last field
if (current_idx == idx && out_len > 0) {
// Already processed
} else if (current_idx == idx && out_len == 0 && *p == '\0') {
// Empty last field
} else {
return -1;
}
}
// Re-scan to get exact length if we were in the target field
// Actually, the logic above processes the target field.
// But we need to handle the case where the field is empty or we just finished it.
// Let's refine the logic to be more robust.
// Reset and do a cleaner pass
p = line;
current_idx = 0;
out_len = 0;
in_quotes = 0;
int found = 0;
while (*p != '\0') {
if (current_idx == idx) {
found = 1;
if (*p == '"') {
if (!in_quotes) {
in_quotes = 1;
p++;
continue;
} else {
if (*(p + 1) == '"') {
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
in_quotes = 0;
p++;
continue;
}
}
} else if (*p == ',' && !in_quotes) {
break;
} else {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
} else {
if (*p == '"') {
if (!in_quotes) {
in_quotes = 1;
} else {
if (*(p + 1) == '"') {
p += 2;
continue;
} else {
in_quotes = 0;
}
}
} else if (*p == ',' && !in_quotes) {
current_idx++;
}
p++;
}
}
if (!found) {
// Check if it's the last field (empty or not)
if (current_idx == idx) {
found = 1;
}
}
if (!found) {
return -1```c
#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (cap == 0) return -1;
int current_idx = 0;
const char *p = line;
size_t out_len = 0;
int in_quotes = 0;
while (*p != '\0') {
if (current_idx == idx) {
// We are inside the target field
if (*p == '"') {
if (!in_quotes) {
// Start of quoted field
in_quotes = 1;
p++;
continue;
} else {
// Inside quoted field
if (*(p + 1) == '"') {
// Escaped quote
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = 0;
p++;
continue;
}
}
} else if (*p == ',' && !in_quotes) {
// End of field
break;
} else {
// Regular character
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
} else {
// Skipping previous fields
if (*p == '"') {
if (!in_quotes) {
in_quotes = 1;
} else {
if (*(p + 1) == '"') {
p += 2;
continue;
} else {
in_quotes = 0;
}
}
} else if (*p == ',' && !in_quotes) {
current_idx++;
}
p++;
}
}
// Check if we found the field
if (current_idx != idx) {
// If we reached end of string and current_idx matches, it's the last field
if (current_idx == idx && out_len > 0) {
// Already processed
} else if (current_idx == idx && out_len == 0 && *p == '\0') {
// Empty last field
} else {
return -1;
}
}
// Re-scan to get exact length if we were in the target field
// Actually, the logic above processes the target field.
// But we need to handle the case where the field is empty or we just finished it.
// Let's refine the logic to be more robust.
// Reset and do a cleaner pass
p = line;
current_idx = 0;
out_len = 0;
in_quotes = 0;
int found = 0;
while (*p != '\0') {
if (current_idx == idx) {
found = 1;
if (*p == '"') {
if (!in_quotes) {
in_quotes = 1;
p++;
continue;
} else {
if (*(p + 1) == '"') {
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
in_quotes = 0;
p++;
continue;
}
}
} else if (*p == ',' && !in_quotes) {
break;
} else {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
} else {
if (*p == '"') {
if (!in_quotes) {
in_quotes = 1;
} else {
if (*(p + 1) == '"') {
p += 2;
continue;
} else {
in_quotes = 0;
}
}
} else if (*p == ',' && !in_quotes) {
current_idx++;
}
p++;
}
}
if (!found) {
// Check if it's the last field (empty or not)
if (current_idx == idx) {
found = 1;
}
}
if (!found) {
return -1Served via llama.cpp Metal · 13.9 tok/s · 18.7 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.
#include <stddef.h>
void reverse_string(char *s) {
if (s == NULL) {
return;
}
size_t len = 0;
while (s[len] != '\0') {
len++;
}
size_t left = 0;
size_t right = len - 1;
while (left < right) {
char temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--;
}
}#include <stddef.h>
void reverse_string(char *s) {
if (s == NULL) {
return;
}
size_t len = 0;
while (s[len] != '\0') {
len++;
}
size_t left = 0;
size_t right = len - 1;
while (left < right) {
char temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--;
}
}#include <stddef.h>
void reverse_string(char *s) {
if (s == NULL) {
return;
}
size_t len = 0;
while (s[len] != '\0') {
len++;
}
size_t left = 0;
size_t right = len - 1;
while (left < right) {
char temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--;
}
}Implement in C11: `int count_words(const char *s)`. Counts words in s. Words are separated by one or more spaces; leading/trailing spaces possible. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdbool.h>
int count_words(const char *s) {
int count = 0;
bool in_word = false;
if (s == NULL) {
return 0;
}
while (*s != '\0') {
if (*s != ' ') {
if (!in_word) {
count++;
in_word = true;
}
} else {
in_word = false;
}
s++;
}
return count;
}#include <stdbool.h>
int count_words(const char *s) {
int count = 0;
bool in_word = false;
if (s == NULL) {
return 0;
}
while (*s != '\0') {
if (*s != ' ') {
if (!in_word) {
count++;
in_word = true;
}
} else {
in_word = false;
}
s++;
}
return count;
}#include <stdbool.h>
int count_words(const char *s) {
int count = 0;
bool in_word = false;
if (s == NULL) {
return 0;
}
while (*s != '\0') {
if (*s != ' ') {
if (!in_word) {
count++;
in_word = true;
}
} else {
in_word = false;
}
s++;
}
return count;
}Implement in C11: `void itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
#include <limits.h>
#include <stdio.h>
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
char *p = buf;
int is_negative = 0;
if (value < 0) {
is_negative = 1;
if (value == INT_MIN) {
// Handle INT_MIN specifically to avoid overflow on negation
*p++ = '-';
value = -INT_MAX - 1; // This is still negative, but we process digits directly
// We will process the absolute value logic manually for INT_MIN
// INT_MIN is -2147483648. Digits: 2,1,4,7,4,8,3,6,4,8
// Since we can't negate it safely, we treat it as a special case or use unsigned math.
// A robust way: convert to unsigned, but handle the sign separately.
// Let's restart the logic for INT_MIN to be cleaner.
// Actually, simpler: use unsigned arithmetic for the magnitude.
unsigned int u_val = (unsigned int)INT_MIN;
// But wait, (unsigned int)INT_MIN is 2147483648.
// We need to print "2147483648".
// Let's just use a generic loop with unsigned conversion.
} else {
value = -value;
}
}
// Re-evaluating for a cleaner, unified approach without special casing INT_MIN in the flow
// Reset pointer
p = buf;
if (value == 0) {
*p++ = '0';
*p = '\0';
return;
}
if (value < 0) {
*p++ = '-';
// Convert to unsigned to handle INT_MIN safely
// Note: (unsigned int)INT_MIN is the correct magnitude for printing
unsigned int u_val = (unsigned int)value;
// However, if value was not INT_MIN, we negated it above? No, let's do it cleanly.
// Let's restart the function logic for maximum clarity and correctness.
}
}
// Corrected implementation below
void itoa(int value, char *buf) {
char *p = buf;
if (value == 0) {
*p++ = '0';
*p = '\0';
return;
}
if (value < 0) {
*p++ = '-';
// Cast to unsigned to handle INT_MIN without overflow
// INT_MIN is -2147483648. (unsigned int)INT_MIN is 2147483648.
// This is the correct magnitude to print.
unsigned int u_val = (unsigned int)value;
// Buffer for digits (max 10 digits + null)
char temp[12];
char *tp = temp;
while (u_val > 0) {
*tp++ = (u_val % 10) + '0';
u_val /= 10;
}
// Copy reversed digits to buffer
while (tp > temp) {
*p++ = *--tp;
}
} else {
unsigned int u_val = (unsigned int)value;
char temp[12];
char *tp = temp;
while (u_val > 0) {
*tp++ = (u_val % 10) + '0';
u_val /= 10;
}
while (tp > temp) {
*p++ = *--tp;
}
}
*p = '\0';
}#include <limits.h>
#include <stdio.h>
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
char *p = buf;
int is_negative = 0;
if (value < 0) {
is_negative = 1;
if (value == INT_MIN) {
// Handle INT_MIN specifically to avoid overflow on negation
*p++ = '-';
value = -INT_MAX - 1; // This is still negative, but we process digits directly
// We will process the absolute value logic manually for INT_MIN
// INT_MIN is -2147483648. Digits: 2,1,4,7,4,8,3,6,4,8
// Since we can't negate it safely, we treat it as a special case or use unsigned math.
// A robust way: convert to unsigned, but handle the sign separately.
// Let's restart the logic for INT_MIN to be cleaner.
// Actually, simpler: use unsigned arithmetic for the magnitude.
unsigned int u_val = (unsigned int)INT_MIN;
// But wait, (unsigned int)INT_MIN is 2147483648.
// We need to print "2147483648".
// Let's just use a generic loop with unsigned conversion.
} else {
value = -value;
}
}
// Re-evaluating for a cleaner, unified approach without special casing INT_MIN in the flow
// Reset pointer
p = buf;
if (value == 0) {
*p++ = '0';
*p = '\0';
return;
}
if (value < 0) {
*p++ = '-';
// Convert to unsigned to handle INT_MIN safely
// Note: (unsigned int)INT_MIN is the correct magnitude for printing
unsigned int u_val = (unsigned int)value;
// However, if value was not INT_MIN, we negated it above? No, let's do it cleanly.
// Let's restart the function logic for maximum clarity and correctness.
}
}
// Corrected implementation below
void itoa(int value, char *buf) {
char *p = buf;
if (value == 0) {
*p++ = '0';
*p = '\0';
return;
}
if (value < 0) {
*p++ = '-';
// Cast to unsigned to handle INT_MIN without overflow
// INT_MIN is -2147483648. (unsigned int)INT_MIN is 2147483648.
// This is the correct magnitude to print.
unsigned int u_val = (unsigned int)value;
// Buffer for digits (max 10 digits + null)
char temp[12];
char *tp = temp;
while (u_val > 0) {
*tp++ = (u_val % 10) + '0';
u_val /= 10;
}
// Copy reversed digits to buffer
while (tp > temp) {
*p++ = *--tp;
}
} else {
unsigned int u_val = (unsigned int)value;
char temp[12];
char *tp = temp;
while (u_val > 0) {
*tp++ = (u_val % 10) + '0';
u_val /= 10;
}
while (tp > temp) {
*p++ = *--tp;
}
}
*p = '\0';
}#include <limits.h>
#include <stdio.h>
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
char *p = buf;
int is_negative = 0;
if (value < 0) {
is_negative = 1;
if (value == INT_MIN) {
// Handle INT_MIN specifically to avoid overflow on negation
*p++ = '-';
value = -INT_MAX - 1; // This is still negative, but we process digits directly
// We will process the absolute value logic manually for INT_MIN
// INT_MIN is -2147483648. Digits: 2,1,4,7,4,8,3,6,4,8
// Since we can't negate it safely, we treat it as a special case or use unsigned math.
// A robust way: convert to unsigned, but handle the sign separately.
// Let's restart the logic for INT_MIN to be cleaner.
// Actually, simpler: use unsigned arithmetic for the magnitude.
unsigned int u_val = (unsigned int)INT_MIN;
// But wait, (unsigned int)INT_MIN is 2147483648.
// We need to print "2147483648".
// Let's just use a generic loop with unsigned conversion.
} else {
value = -value;
}
}
// Re-evaluating for a cleaner, unified approach without special casing INT_MIN in the flow
// Reset pointer
p = buf;
if (value == 0) {
*p++ = '0';
*p = '\0';
return;
}
if (value < 0) {
*p++ = '-';
// Convert to unsigned to handle INT_MIN safely
// Note: (unsigned int)INT_MIN is the correct magnitude for printing
unsigned int u_val = (unsigned int)value;
// However, if value was not INT_MIN, we negated it above? No, let's do it cleanly.
// Let's restart the function logic for maximum clarity and correctness.
}
}
// Corrected implementation below
void itoa(int value, char *buf) {
char *p = buf;
if (value == 0) {
*p++ = '0';
*p = '\0';
return;
}
if (value < 0) {
*p++ = '-';
// Cast to unsigned to handle INT_MIN without overflow
// INT_MIN is -2147483648. (unsigned int)INT_MIN is 2147483648.
// This is the correct magnitude to print.
unsigned int u_val = (unsigned int)value;
// Buffer for digits (max 10 digits + null)
char temp[12];
char *tp = temp;
while (u_val > 0) {
*tp++ = (u_val % 10) + '0';
u_val /= 10;
}
// Copy reversed digits to buffer
while (tp > temp) {
*p++ = *--tp;
}
} else {
unsigned int u_val = (unsigned int)value;
char temp[12];
char *tp = temp;
while (u_val > 0) {
*tp++ = (u_val % 10) + '0';
u_val /= 10;
}
while (tp > temp) {
*p++ = *--tp;
}
}
*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.
#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: `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>
double rpn_eval(const char *expr) {
double stack[1024];
int top = 0;
char *token = strtok((char *)expr, " ");
while (token != NULL) {
if (isdigit(token[0]) || (token[0] == '-' && isdigit(token[1]))) {
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[0];
}#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
double rpn_eval(const char *expr) {
double stack[1024];
int top = 0;
char *token = strtok((char *)expr, " ");
while (token != NULL) {
if (isdigit(token[0]) || (token[0] == '-' && isdigit(token[1]))) {
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[0];
}#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
double rpn_eval(const char *expr) {
double stack[1024];
int top = 0;
char *token = strtok((char *)expr, " ");
while (token != NULL) {
if (isdigit(token[0]) || (token[0] == '-' && isdigit(token[1]))) {
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[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;
bool in_quotes = false;
bool field_started = false;
size_t out_len = 0;
while (*p != '\0') {
char c = *p;
if (!in_quotes) {
if (c == '"') {
in_quotes = true;
field_started = true;
p++;
continue;
} else if (c == ',') {
if (field_started) {
current_idx++;
field_started = false;
}
if (current_idx == idx) {
// Start of target field
break;
}
p++;
continue;
} else {
field_started = true;
if (current_idx == idx) {
// Start of target field (unquoted)
break;
}
p++;
continue;
}
} else {
// Inside quotes
if (c == '"') {
if (*(p + 1) == '"') {
// Escaped quote
if (current_idx == idx && out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = false;
p++;
break;
}
} else {
if (current_idx == idx && out_len < cap - 1) {
out[out_len++] = c;
}
p++;
}
}
}
// If we broke out because we found the start of the target field
if (current_idx == idx) {
// If we broke out at a comma or quote, we need to process the rest
// Re-evaluate logic: The break above happens when we identify the start.
// We need to continue parsing from the current position 'p'
// Reset out_len for the actual extraction loop
out_len = 0;
// If we broke out at a quote, we are inside quotes now
// If we broke out at a comma, we are outside quotes
// The logic above was slightly flawed in the break condition.
// Let's restart the parsing logic cleanly.
}
// Clean implementation restart
p = line;
current_idx = 0;
in_quotes = false;
out_len = 0;
bool found = false;
while (*p != '\0') {
char c = *p;
if (!in_quotes) {
if (c == '"') {
in_quotes = true;
p++;
continue;
} else if (c == ',') {
current_idx++;
p++;
continue;
} else {
// Unquoted field start
if (current_idx == idx) {
found = true;
// Process this char
if (out_len < cap - 1) {
out[out_len++] = c;
}
p++;
continue;
}
// Skip chars for non-target fields
p++;
continue;
}
} else {
// Inside quotes
if (c == '"') {
if (*(p + 1) == '"') {
// Escaped quote
if (current_idx == idx && out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = false;
p++;
// Check if this was the end of the target field
if (current_idx == idx) {
found = true;
break;
}
continue;
}
} else {
if (current_idx == idx && out_len < cap - 1) {
out[out_len++] = c;
}
p++;
continue;
}
}
}
// Handle case where the last field ends at EOF
if (current_idx == idx && !found) {```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;
bool in_quotes = false;
bool field_started = false;
size_t out_len = 0;
while (*p != '\0') {
char c = *p;
if (!in_quotes) {
if (c == '"') {
in_quotes = true;
field_started = true;
p++;
continue;
} else if (c == ',') {
if (field_started) {
current_idx++;
field_started = false;
}
if (current_idx == idx) {
// Start of target field
break;
}
p++;
continue;
} else {
field_started = true;
if (current_idx == idx) {
// Start of target field (unquoted)
break;
}
p++;
continue;
}
} else {
// Inside quotes
if (c == '"') {
if (*(p + 1) == '"') {
// Escaped quote
if (current_idx == idx && out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = false;
p++;
break;
}
} else {
if (current_idx == idx && out_len < cap - 1) {
out[out_len++] = c;
}
p++;
}
}
}
// If we broke out because we found the start of the target field
if (current_idx == idx) {
// If we broke out at a comma or quote, we need to process the rest
// Re-evaluate logic: The break above happens when we identify the start.
// We need to continue parsing from the current position 'p'
// Reset out_len for the actual extraction loop
out_len = 0;
// If we broke out at a quote, we are inside quotes now
// If we broke out at a comma, we are outside quotes
// The logic above was slightly flawed in the break condition.
// Let's restart the parsing logic cleanly.
}
// Clean implementation restart
p = line;
current_idx = 0;
in_quotes = false;
out_len = 0;
bool found = false;
while (*p != '\0') {
char c = *p;
if (!in_quotes) {
if (c == '"') {
in_quotes = true;
p++;
continue;
} else if (c == ',') {
current_idx++;
p++;
continue;
} else {
// Unquoted field start
if (current_idx == idx) {
found = true;
// Process this char
if (out_len < cap - 1) {
out[out_len++] = c;
}
p++;
continue;
}
// Skip chars for non-target fields
p++;
continue;
}
} else {
// Inside quotes
if (c == '"') {
if (*(p + 1) == '"') {
// Escaped quote
if (current_idx == idx && out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = false;
p++;
// Check if this was the end of the target field
if (current_idx == idx) {
found = true;
break;
}
continue;
}
} else {
if (current_idx == idx && out_len < cap - 1) {
out[out_len++] = c;
}
p++;
continue;
}
}
}
// Handle case where the last field ends at EOF
if (current_idx == idx && !found) {```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;
bool in_quotes = false;
bool field_started = false;
size_t out_len = 0;
while (*p != '\0') {
char c = *p;
if (!in_quotes) {
if (c == '"') {
in_quotes = true;
field_started = true;
p++;
continue;
} else if (c == ',') {
if (field_started) {
current_idx++;
field_started = false;
}
if (current_idx == idx) {
// Start of target field
break;
}
p++;
continue;
} else {
field_started = true;
if (current_idx == idx) {
// Start of target field (unquoted)
break;
}
p++;
continue;
}
} else {
// Inside quotes
if (c == '"') {
if (*(p + 1) == '"') {
// Escaped quote
if (current_idx == idx && out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = false;
p++;
break;
}
} else {
if (current_idx == idx && out_len < cap - 1) {
out[out_len++] = c;
}
p++;
}
}
}
// If we broke out because we found the start of the target field
if (current_idx == idx) {
// If we broke out at a comma or quote, we need to process the rest
// Re-evaluate logic: The break above happens when we identify the start.
// We need to continue parsing from the current position 'p'
// Reset out_len for the actual extraction loop
out_len = 0;
// If we broke out at a quote, we are inside quotes now
// If we broke out at a comma, we are outside quotes
// The logic above was slightly flawed in the break condition.
// Let's restart the parsing logic cleanly.
}
// Clean implementation restart
p = line;
current_idx = 0;
in_quotes = false;
out_len = 0;
bool found = false;
while (*p != '\0') {
char c = *p;
if (!in_quotes) {
if (c == '"') {
in_quotes = true;
p++;
continue;
} else if (c == ',') {
current_idx++;
p++;
continue;
} else {
// Unquoted field start
if (current_idx == idx) {
found = true;
// Process this char
if (out_len < cap - 1) {
out[out_len++] = c;
}
p++;
continue;
}
// Skip chars for non-target fields
p++;
continue;
}
} else {
// Inside quotes
if (c == '"') {
if (*(p + 1) == '"') {
// Escaped quote
if (current_idx == idx && out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = false;
p++;
// Check if this was the end of the target field
if (current_idx == idx) {
found = true;
break;
}
continue;
}
} else {
if (current_idx == idx && out_len < cap - 1) {
out[out_len++] = c;
}
p++;
continue;
}
}
}
// Handle case where the last field ends at EOF
if (current_idx == idx && !found) {Served via llama.cpp Metal · 80.5 tok/s · 18.4 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 *start = s;
char *end = s;
// Find the end of the string
while (*end != '\0') {
end++;
}
end--; // Move back to the last character
// Reverse the string in place
while (start < end) {
char temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}void reverse_string(char *s) {
if (s == NULL) return;
char *start = s;
char *end = s;
// Find the end of the string
while (*end != '\0') {
end++;
}
end--; // Move back to the last character
// Reverse the string in place
while (start < end) {
char temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}void reverse_string(char *s) {
if (s == NULL) return;
char *start = s;
char *end = s;
// Find the end of the string
while (*end != '\0') {
end++;
}
end--; // Move back to the last character
// Reverse the string in place
while (start < end) {
char temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}Implement in C11: `const char *fizzbuzz(int n)`. Returns "FizzBuzz" if n divisible by 15, "Fizz" by 3, "Buzz" by 5, else the decimal string of n. A static buffer is fine. Reply with only a C code block. No main function, no tests, no explanation.
const char *fizzbuzz(int n) {
static char buffer[16];
if (n % 15 == 0) {
return "FizzBuzz";
} else if (n % 3 == 0) {
return "Fizz";
} else if (n % 5 == 0) {
return "Buzz";
} else {
snprintf(buffer, sizeof(buffer), "%d", n);
return buffer;
}
}const char *fizzbuzz(int n) {
static char buffer[16];
if (n % 15 == 0) {
return "FizzBuzz";
} else if (n % 3 == 0) {
return "Fizz";
} else if (n % 5 == 0) {
return "Buzz";
} else {
snprintf(buffer, sizeof(buffer), "%d", n);
return buffer;
}
}const char *fizzbuzz(int n) {
static char buffer[16];
if (n % 15 == 0) {
return "FizzBuzz";
} else if (n % 3 == 0) {
return "Fizz";
} else if (n % 5 == 0) {
return "Buzz";
} else {
snprintf(buffer, sizeof(buffer), "%d", n);
return buffer;
}
}Implement in C11: `int count_words(const char *s)`. Counts words in s. Words are separated by one or more spaces; leading/trailing spaces possible. Reply with only a C code block. No main function, no tests, no explanation.
int count_words(const char *s) {
if (s == NULL) return 0;
int count = 0;
int in_word = 0;
for (int i = 0; s[i] != '\0'; i++) {
if (s[i] == ' ') {
if (in_word) {
count++;
in_word = 0;
}
} else {
in_word = 1;
}
}
if (in_word) {
count++;
}
return count;
}int count_words(const char *s) {
if (s == NULL) return 0;
int count = 0;
int in_word = 0;
for (int i = 0; s[i] != '\0'; i++) {
if (s[i] == ' ') {
if (in_word) {
count++;
in_word = 0;
}
} else {
in_word = 1;
}
}
if (in_word) {
count++;
}
return count;
}int count_words(const char *s) {
if (s == NULL) return 0;
int count = 0;
int in_word = 0;
for (int i = 0; s[i] != '\0'; i++) {
if (s[i] == ' ') {
if (in_word) {
count++;
in_word = 0;
}
} else {
in_word = 1;
}
}
if (in_word) {
count++;
}
return count;
}Implement in C11: `void itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdint.h>
#include <stdbool.h>
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
bool negative = value < 0;
if (negative && value == INT_MIN) {
// Special case for INT_MIN to avoid overflow when negating
int32_t temp = value;
int i = 0;
while (temp != 0) {
buf[i++] = '0' - (temp % 10);
temp /= 10;
}
buf[i] = '\0';
// Reverse the string
for (int j = 0; j < i/2; j++) {
char c = buf[j];
buf[j] = buf[i-1-j];
buf[i-1-j] = c;
}
buf[i] = '\0';
return;
}
int32_t abs_value = negative ? -value : value;
int i = 0;
while (abs_value > 0) {
buf[i++] = '0' + (abs_value % 10);
abs_value /= 10;
}
if (negative) {
buf[i++] = '-';
}
// Reverse the string
for (int j = 0; j < i/2; j++) {
char c = buf[j];
buf[j] = buf[i-1-j];
buf[i-1-j] = c;
}
buf[i] = '\0';
}#include <stdint.h>
#include <stdbool.h>
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
bool negative = value < 0;
if (negative && value == INT_MIN) {
// Special case for INT_MIN to avoid overflow when negating
int32_t temp = value;
int i = 0;
while (temp != 0) {
buf[i++] = '0' - (temp % 10);
temp /= 10;
}
buf[i] = '\0';
// Reverse the string
for (int j = 0; j < i/2; j++) {
char c = buf[j];
buf[j] = buf[i-1-j];
buf[i-1-j] = c;
}
buf[i] = '\0';
return;
}
int32_t abs_value = negative ? -value : value;
int i = 0;
while (abs_value > 0) {
buf[i++] = '0' + (abs_value % 10);
abs_value /= 10;
}
if (negative) {
buf[i++] = '-';
}
// Reverse the string
for (int j = 0; j < i/2; j++) {
char c = buf[j];
buf[j] = buf[i-1-j];
buf[i-1-j] = c;
}
buf[i] = '\0';
}#include <stdint.h>
#include <stdbool.h>
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
bool negative = value < 0;
if (negative && value == INT_MIN) {
// Special case for INT_MIN to avoid overflow when negating
int32_t temp = value;
int i = 0;
while (temp != 0) {
buf[i++] = '0' - (temp % 10);
temp /= 10;
}
buf[i] = '\0';
// Reverse the string
for (int j = 0; j < i/2; j++) {
char c = buf[j];
buf[j] = buf[i-1-j];
buf[i-1-j] = c;
}
buf[i] = '\0';
return;
}
int32_t abs_value = negative ? -value : value;
int i = 0;
while (abs_value > 0) {
buf[i++] = '0' + (abs_value % 10);
abs_value /= 10;
}
if (negative) {
buf[i++] = '-';
}
// Reverse the string
for (int j = 0; j < i/2; j++) {
char c = buf[j];
buf[j] = buf[i-1-j];
buf[i-1-j] = c;
}
buf[i] = '\0';
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
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;
int field_len = 0;
while (*p) {
// Skip to the start of the desired field
while (current_idx < idx) {
if (*p == '"') {
// Quoted field
p++;
while (*p) {
if (*p == '"') {
if (*(p + 1) == '"') {
p += 2; // Skip escaped quote
} else {
p++; // End of quoted field
break;
}
} else {
p++;
}
}
} else {
// Unquoted field
while (*p && *p != ',') p++;
}
if (*p == ',') p++;
current_idx++;
}
// Now p points to the start of our target field
if (current_idx != idx) return -1; // Field index out of range
// Extract the field
const char *start = p;
int in_quotes = 0;
int escaped = 0;
if (*p == '"') {
in_quotes = 1;
p++;
}
const char *field_start = p;
while (*p) {
if (in_quotes) {
if (*p == '"') {
if (*(p + 1) == '"') {
p += 2; // Skip escaped quote
} else {
p++; // End of quoted field
break;
}
} else {
p++;
}
} else {
if (*p == ',') break;
p++;
}
}
// Copy the field to output
size_t field_len = p - field_start;
if (field_len >= cap) field_len = cap - 1;
memcpy(out, field_start, field_len);
out[field_len] = '\0';
return field_len;
}
return -1; // Field index out of range
}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;
int field_len = 0;
while (*p) {
// Skip to the start of the desired field
while (current_idx < idx) {
// Skip to next comma or end of line
while (*p && *p != ',') {
if (*p == '"' && *(p+1) == '"') p += 2; // Skip escaped quote
else if (*p == '"') {
// Skip quoted field
p++;
while (*p && *p != '"') {
if (*p == '"' && *(p+1) == '"') p += 2;
else p++;
}
if (*p == '"') p++;
} else p++;
}
if (*p == ',') p++;
current_idx++;
}
// Now we're at the start of the desired field
if (current_idx != idx) return -1; // Field index out of range
// Handle quoted field
if (*p == '"') {
p++; // Skip opening quote
while (*p) {
if (*p == '"') {
if (*(p+1) == '"') {
// Escaped quote
if (field_len < (int)(cap - 1)) {
out[field_len++] = '"';
}
p += 2;
} else {
// Closing quote
p++;
break;
}
} else {
if (field_len < (int)(cap - 1)) {
out[field_len++] = *p;
}
p++;
}
}
} else {
// Unquoted field
while (*p && *p != ',') {
if (field_len < (int)(cap - 1)) {
out[field_len++] = *p;
}
p++;
}
}
// Terminate the output string
out[field_len] = '\0';
return field_len;
}
// If we reach here, the field index was out of range
return -1;
}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;
int field_len = 0;
while (*p) {
// Skip to the start of the desired field
while (current_idx < idx) {
// Skip to next comma or end of line
while (*p && *p != ',') {
if (*p == '"' && *(p+1) == '"') p += 2; // Skip escaped quote
else if (*p == '"') {
// Skip quoted field
p++;
while (*p && *p != '"') {
if (*p == '"' && *(p+1) == '"') p += 2;
else p++;
}
if (*p == '"') p++;
} else p++;
}
if (*p == ',') p++;
current_idx++;
}
// Now we're at the start of the desired field
if (current_idx != idx) return -1; // Field index out of range
// Handle quoted field
if (*p == '"') {
p++; // Skip opening quote
while (*p) {
if (*p == '"') {
if (*(p+1) == '"') {
// Escaped quote
if (field_len < (int)(cap - 1)) {
out[field_len++] = '"';
}
p += 2;
} else {
// Closing quote
p++;
break;
}
} else {
if (field_len < (int)(cap - 1)) {
out[field_len++] = *p;
}
p++;
}
}
} else {
// Unquoted field
while (*p && *p != ',') {
if (field_len < (int)(cap - 1)) {
out[field_len++] = *p;
}
p++;
}
}
// Terminate the output string
out[field_len] = '\0';
return field_len;
}
// If we reach here, the field index was out of range
return -1;
}Served via llama.cpp Metal · 47.1 tok/s · 24.0 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 *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 *start = buf;
int negative = value < 0;
if (negative) {
*buf++ = '-';
if (value == INT_MIN) {
value = INT_MAX;
} else {
value = -value;
}
}
char *digits = buf;
if (value == 0) {
*digits++ = '0';
} else {
while (value > 0) {
*digits++ = (char)('0' + (value % 10));
value /= 10;
}
}
*digits = '\0';
for (char *p = start, *q = digits - 1; p < q; p++, q--) {
char tmp = *p;
*p = *q;
*q = tmp;
}
}#include <limits.h>
void itoa(int value, char *buf) {
char *start = buf;
int negative = value < 0;
if (negative) {
*buf++ = '-';
if (value == INT_MIN) {
value = INT_MAX;
} else {
value = -value;
}
}
char *digits = buf;
if (value == 0) {
*digits++ = '0';
} else {
while (value > 0) {
*digits++ = (char)('0' + (value % 10));
value /= 10;
}
}
*digits = '\0';
for (char *p = start, *q = digits - 1; p < q; p++, q--) {
char tmp = *p;
*p = *q;
*q = tmp;
}
}#include <limits.h>
void itoa(int value, char *buf) {
char *start = buf;
int negative = value < 0;
if (negative) {
*buf++ = '-';
if (value == INT_MIN) {
value = INT_MAX;
} else {
value = -value;
}
}
char *digits = buf;
if (value == 0) {
*digits++ = '0';
} else {
while (value > 0) {
*digits++ = (char)('0' + (value % 10));
value /= 10;
}
}
*digits = '\0';
for (char *p = start, *q = digits - 1; p < q; p++, q--) {
char tmp = *p;
*p = *q;
*q = tmp;
}
}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';
else {
end = s + strlen(s) - 1;
while (end > s && (*end == ' ' || *end == '\t' || *end == '\n'))
end--;
end[1] = '\0';
}
}void trim(char *s) {
char *end;
while (*s == ' ' || *s == '\t' || *s == '\n')
s++;
if (*s == '\0')
*s = '\0';
else {
end = s + strlen(s) - 1;
while (end > s && (*end == ' ' || *end == '\t' || *end == '\n'))
end--;
end[1] = '\0';
}
}void trim(char *s) {
char *end;
while (*s == ' ' || *s == '\t' || *s == '\n')
s++;
if (*s == '\0')
*s = '\0';
else {
end = s + strlen(s) - 1;
while (end > s && (*end == ' ' || *end == '\t' || *end == '\n'))
end--;
end[1] = '\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) {
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 field = 0;
int in_quotes = 0;
size_t len = 0;
for (const char *p = line; ; ++p) {
if (*p == '\0') {
if (field == 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 (field == idx) {
if (len >= cap - 1)
len = cap - 1;
out[len] = '\0';
return (int)len;
}
++field;
} 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 field = 0;
int in_quotes = 0;
size_t len = 0;
for (const char *p = line; ; ++p) {
if (*p == '\0') {
if (field == 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 (field == idx) {
if (len >= cap - 1)
len = cap - 1;
out[len] = '\0';
return (int)len;
}
++field;
} else {
if (len < cap - 1)
out[len++] = *p;
}
}
}
}Served via llama.cpp Metal · 11.2 tok/s · 24.5 GB RAM · coding total 79/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;
while (*end != '\0') end++;
end--;
char *start = s;
while (start < end) {
char temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}void reverse_string(char *s) {
if (s == NULL) return;
char *end = s;
while (*end != '\0') end++;
end--;
char *start = s;
while (start < end) {
char temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}void reverse_string(char *s) {
if (s == NULL) return;
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 {
in_word = 0;
}
s++;
}
return count;
}Implement in C11: `void itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdint.h>
void itoa(int value, char *buf) {
char tmp[12];
int i = 0, neg = 0;
unsigned int uval;
if (value < 0) {
neg = 1;
if (value == INT_MIN) {
uval = (unsigned int)(INT_MIN);
} else {
uval = (unsigned int)(-value);
}
} else {
uval = (unsigned int)value;
}
do {
tmp[i++] = "0123456789"[uval % 10];
uval /= 10;
} while (uval);
if (neg) {
tmp[i++] = '-';
}
tmp[i] = '\0';
for (int j = 0; j < i; j++) {
buf[j] = tmp[i - j - 1];
}
buf[i] = '\0';
}#include <stdint.h>
void itoa(int value, char *buf) {
char tmp[12];
int i = 0, neg = 0;
unsigned int uval = (value == INT_MIN) ? (uint32_t)INT_MIN : (unsigned int)(value < 0 ? -value : value);
if (value < 0 && value != INT_MIN) neg = 1;
do {
tmp[i++] = uval % 10 + '0';
} while ((uval /= 10) > 0);
if (neg) tmp[i++] = '-';
int j = 0;
do {
buf[j++] = tmp[--i];
} while (i > 0);
buf[j] = '\0';
}(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.
#include <stdlib.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
int i = 0;
while (s[i] != '\0' && count < max) {
int j = i;
while (s[j] != '\0' && s[j] != ',') {
j++;
}
char *endptr;
int num = (int)strtol(s + i, &endptr, 10);
out[count++] = num;
i = j + 1;
}
return count;
}#include <stdlib.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
int i = 0;
while (s[i] != '\0' && count < max) {
int j = i;
while (s[j] != '\0' && s[j] != ',') {
j++;
}
char *endptr;
int num = (int)strtol(s + i, &endptr, 10);
out[count++] = num;
i = j + 1;
}
return count;
}#include <stdlib.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
int i = 0;
while (s[i] != '\0' && count < max) {
int j = i;
while (s[j] != '\0' && s[j] != ',') {
j++;
}
char *endptr;
int num = (int)strtol(s + i, &endptr, 10);
out[count++] = num;
i = j + 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) return;
char *start = s;
while (*start && (*start == ' ' || *start == '\t' || *start == '\n'))
start++;
if (!*start) {
*s = '\0';
return;
}
char *end = start;
while (*(end + 1))
end++;
while (end > start && (*end == ' ' || *end == '\t' || *end == '\n'))
end--;
size_t len = end - start + 1;
for (size_t i = 0; i < len; i++)
s[i] = start[i];
s[len] = '\0';
}void trim(char *s) {
if (!s) return;
char *start = s;
while (*start && (*start == ' ' || *start == '\t' || *start == '\n'))
start++;
if (!*start) {
*s = '\0';
return;
}
char *end = start;
while (*(end + 1))
end++;
while (end > start && (*end == ' ' || *end == '\t' || *end == '\n'))
end--;
size_t len = end - start + 1;
for (size_t i = 0; i < len; i++)
s[i] = start[i];
s[len] = '\0';
}void trim(char *s) {
if (!s) return;
char *start = s;
while (*start && (*start == ' ' || *start == '\t' || *start == '\n'))
start++;
if (!*start) {
*s = '\0';
return;
}
char *end = start;
while (*(end + 1))
end++;
while (end > start && (*end == ' ' || *end == '\t' || *end == '\n'))
end--;
size_t len = end - start + 1;
for (size_t i = 0; i < len; i++)
s[i] = start[i];
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.
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL || *s == '\0') {
return -1;
}
const char *p = s;
int sign = 1;
if (*p == '+' || *p == '-') {
sign = (*p == '-') ? -1 : 1;
p++;
if (*p == '\0') {
return -1;
}
}
int result = 0;
for (; *p != '\0'; p++) {
if (*p < '0' || *p > '9') {
return -1;
}
result = result * 10 + (*p - '0');
}
*out = sign * result;
return 0;
}#include <stdbool.h>
int atoi_strict(const char *s, int *out) {
if (!s || !out) return -1;
const char *p = s;
bool neg = false;
if (*p == '-') {
neg = true;
p++;
} else if (*p == '+') {
p++;
}
if (*p == '\0') return -1;
int val = 0;
while (*p != '\0') {
if (*p < '0' || *p > '9') return -1;
val = val * 10 + (*p - '0');
p++;
}
*out = neg ? -val : val;
return *s != '\0' && p == s + (neg || *s == '+' ? 2 : 1) + (p - (neg || *s == '+' ? s + 1 : s)) - !(*s == '-' || *s == '+' || (*s >= '0' && *s <= '9')) ? 0 : -1;
}(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.
#include <stdbool.h>
int glob_match(const char *pat, const char *str) {
bool (*match_here)(const char *, const char *) = NULL;
bool (*match_star)(const char *, const char *) = NULL;
match_here = [=](const char *p, const char *s) {
for (;; ++p, ++s) {
if (*p == '\0') return *s == '\0';
if (*p == '*') return match_star(p + 1, s);
if (*s == '\0') return false;
if (*p != '?' && *p != *s) return false;
if (*s != '\0') continue;
return *p == '\0';
}
};
match_star = [=](const char *p, const char *s) {
do {
if (match_here(p, s)) return true;
} while (*s++ != '\0');
return false;
};
return match_here(pat, str) ? 1 : 0;
}(no code extracted)#include <stdbool.h>
int glob_match(const char *pat, const char *str) {
bool (*match_here)(const char*, const char*);
bool (*match_star)(const char*, const char*);
match_here = (bool (*)(const char*, const char*))[](const char *p, const char *s) {
for (;; p++, s++) {
if (!*p) return !*s;
if (*p == '*') return match_star(p + 1, s);
if (*p != '?' && *p != *s) return false;
if (!*s) return false;
}
};
match_star = (bool (*)(const char*, const char*))[](const char *p, const char *s) {
do {
if (match_here(p, s)) return true;
} while (*s++);
return false;
};
return match_here(pat, str) ? 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 || !out || cap < 1 || idx < 0) return -1;
size_t i = 0, field_idx = 0, out_pos = 0;
char c;
while ((c = line[i++]) != '\0') {
if (field_idx > idx) break;
if (field_idx == idx) {
if (c == '"') {
for (; (c = line[i++]) != '\0';) {
if (c == '"') {
if (line[i] == '"') { i++; continue; }
break;
}
if (out_pos < cap - 1) out[out_pos++] = c;
}
if (c != '\0') field_idx++;
} else {
if (c == ',') { field_idx++; continue; }
if (out_pos < cap - 1) out[out_pos++] = c;
}
} else if (c == ',') field_idx++;
}
if (field_idx <= idx) return -1;
out[out_pos] = '\0';
return (int)out_pos;
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (!line || !out || cap < 1 || idx < 0) return -1;
size_t i = 0, field_start = 0, field_len = 0;
int in_quotes = 0, current_field = 0;
while (line[i] != '\0') {
char c = line[i];
if (c == '"') {
in_quotes = !in_quotes;
i++;
if (in_quotes) continue;
if (line[i] == '"') { out[field_len++] = '"'; i++; }
else { out[field_len] = '\0'; return (int)field_len; }
}
if (!in_quotes) {
if (c == ',') {
if (current_field == idx) { out[field_len] = '\0'; return (int)field_len; }
current_field++; field_start = i + 1; field_len = 0;
} else {
if (current_field == idx) {
if (field_len < cap - 1) out[field_len] = c;
field_len++;
}
}
}
i++;
}
if (current_field == idx) { out[field_len] = '\0'; return (int)field_len; }
return -1;
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (!line || !out || cap < 1 || idx < 0) return -1;
size_t i = 0, field_idx = 0, out_pos = 0;
int inquotes = 0, escaped = 0;
while (line[i]) {
char c = line[i++];
if (escaped) {
out[out_pos++] = c;
escaped = 0;
if (out_pos == cap - 1) break;
} else if (c == '"') {
if (line[i] == '"') {
escaped = 1;
i++;
} else {
inquotes = !inquotes;
}
} else if (c == ',' && !inquotes) {
if (field_idx == idx) break;
field_idx++;
out_pos = 0;
} else {
if (field_idx == idx) {
out[out_pos++] = c;
if (out_pos == cap - 1) break;
}
}
}
if (field_idx < idx) return -1;
out[out_pos] = '\0';
return (int)out_pos;
}Served via llama.cpp Metal · 10.8 tok/s · 37.9 GB RAM · coding total 66/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_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_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;
}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])) {
int val = 0;
while (isdigit(s[i])) {
val = val * 10 + (s[i++] - '0');
}
if (j < max) {
out[j++] = neg ? -val : val;
}
}
if (s[i] == ',') {
i++;
} else {
break;
}
}
return j;
}#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])) {
int val = 0;
while (isdigit(s[i])) {
val = val * 10 + (s[i++] - '0');
}
if (j < max) {
out[j++] = neg ? -val : val;
}
}
if (s[i] == ',') {
i++;
} else {
break;
}
}
return j;
}#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])) {
int val = 0;
while (isdigit(s[i])) {
val = val * 10 + (s[i++] - '0');
}
if (j < max) {
out[j++] = neg ? -val : val;
}
}
if (s[i] == ',') {
i++;
} else {
break;
}
}
return j;
}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;
const char *p = line;
if (idx < 0 || idx >= strcspn(line, ","))
return -1;
for (i = 0; i < idx; i++) {
while (*p && *p != ',' && *p != '"')
p++;
if (*p == ',')
p++;
else if (*p == '"') {
in_quotes = 1;
while (*p && *p != '"')
p++;
if (*p == '"')
p++;
}
}
while (*p && *p != ',' && *p != '\r' && *p != '\n') {
if (in_quotes && *p == '\\') {
if (p[1] == '\"' || p[1] == '\\') {
out[len++] = p[1];
p += 2;
}
} else {
out[len++] = *p;
p++;
}
}
out[len] = '\0';
return len;
}#include <stdio.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
int i, j, in_quotes, len = 0;
const char *p = line;
if (idx < 0 || idx >= strcspn(line, ","))
return -1;
for (i = 0; i < idx; i++) {
while (*p && *p != ',' && *p != '"')
p++;
if (*p == ',')
p++;
else if (*p == '"') {
in_quotes = 1;
while (*p && *p != '"')
p++;
if (*p == '"')
p++;
}
}
while (*p && *p != ',' && *p != '\r' && *p != '\n') {
if (in_quotes && *p == '\\') {
if (p[1] == '\"' || p[1] == '\\') {
out[len++] = p[1];
p += 2;
}
} else {
out[len++] = *p;
p++;
}
}
out[len] = '\0';
return len;
}#include <stdio.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
int i, j, in_quotes, len = 0;
const char *p = line;
if (idx < 0 || idx >= strcspn(line, ","))
return -1;
for (i = 0; i < idx; i++) {
while (*p && *p != ',' && *p != '"')
p++;
if (*p == ',')
p++;
else if (*p == '"') {
in_quotes = 1;
while (*p && *p != '"')
p++;
if (*p == '"')
p++;
}
}
while (*p && *p != ',' && *p != '\r' && *p != '\n') {
if (in_quotes && *p == '\\') {
if (p[1] == '\"' || p[1] == '\\') {
out[len++] = p[1];
p += 2;
}
} else {
out[len++] = *p;
p++;
}
}
out[len] = '\0';
return len;
}