Every failing sample below is the real generated C, with the compiler or test error that rejected it. Trial 0 runs at temperature 0, trials 1 and 2 at 0.7 — so a task passing 1/3 is a sampling-luck pass, not a reliable one. Models are ordered best first. Other technologies: C · Python · Bash · research · how to reproduce.
Setup and model serving are on the models index — including a script-free version using nothing but curl and a compiler; with a server running, this is the exact command that produced the samples below:
# C suite (16 easy + 3 hard tasks, 3 trials each; trial 0 at temp 0, then 0.7)
python scripts/eval_code.py --target <model> --set all --trials 3 \
--dump-failures results/failures
# brutal set: 6 tasks where the textbook answer is wrong
python scripts/eval_code.py --target <model> --set brutal --trials 3 \
--dump-failures results/failures --json > results/<model>-brutal-c.json
# grading is deterministic: cc -std=c11 -O2 -Wall must compile it, then hidden
# asserts run. A referee re-graded every failure by hand.
Served via llama.cpp CUDA · 28.1 tok/s · 29.8 GB RAM · coding total 111/126
Implement in C11: `int parse_csv_ints(const char *s, int *out, int max)`. Parses comma-separated integers from s into out (at most max). Returns the count stored. Input is well-formed (digits, optional leading minus, commas). Reply with only a C code block. No main function, no tests, no explanation.
#include <stdlib.h>
#include <string.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
const char *p = s;
while (*p && count < max) {
/* Skip whitespace if any (though spec says well-formed, be safe) */
while (*p == ' ' || *p == '\t') {
p++;
}
if (*p == '\0') {
break;
}
/* Parse integer */
char *endptr;
long val = strtol(p, &endptr, 10);
/* Check if we actually parsed something */
if (endptr == p) {
/* No digits parsed, skip to next comma or end */
while (*p && *p != ',') {
p++;
}
} else {
/* Store the value, clamping to int range if needed */
if (val > INT_MAX) {
out[count] = INT_MAX;
} else if (val < INT_MIN) {
out[count] = INT_MIN;
} else {
out[count] = (int)val;
}
count++;
p = endptr;
}
/* Skip comma if present */
if (*p == ',') {
p++;
}
}
return count;
}#include <stdlib.h>
#include <string.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
const char *p = s;
while (*p && count < max) {
/* Skip whitespace if any (though spec says well-formed, be safe) */
while (*p == ' ' || *p == '\t') {
p++;
}
if (*p == '\0') {
break;
}
/* Parse integer */
char *endptr;
long val = strtol(p, &endptr, 10);
/* Check if we actually parsed something */
if (endptr == p) {
/* No digits parsed, skip to next comma or end */
while (*p && *p != ',') {
p++;
}
} else {
/* Store the value, clamping to int range if needed */
if (val > INT_MAX) {
out[count] = INT_MAX;
} else if (val < INT_MIN) {
out[count] = INT_MIN;
} else {
out[count] = (int)val;
}
count++;
p = endptr;
}
/* Skip comma if present */
if (*p == ',') {
p++;
}
}
return count;
}#include <stdlib.h>
#include <string.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
const char *p = s;
while (*p && count < max) {
/* Skip whitespace if any (though spec says well-formed, be safe) */
while (*p == ' ' || *p == '\t') {
p++;
}
if (*p == '\0') {
break;
}
/* Parse integer */
char *endptr;
long val = strtol(p, &endptr, 10);
/* Check if we actually parsed something */
if (endptr == p) {
/* No digits parsed, skip to next comma or end */
while (*p && *p != ',') {
p++;
}
} else {
/* Store the value, clamping to int range if needed */
if (val > INT_MAX) {
out[count] = INT_MAX;
} else if (val < INT_MIN) {
out[count] = INT_MIN;
} else {
out[count] = (int)val;
}
count++;
p = endptr;
}
/* Skip comma if present */
if (*p == ',') {
p++;
}
}
return count;
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
Looking at this problem, I need to parse a single CSV line according to RFC-4180 rules:
1. Fields are separated by commas
2. Quoted fields start and end with double quotes
3. Inside quoted fields, doubled quotes `""` represent a single quote `"`
4. Quoted fields can contain commas and newlines (but since we're parsing one line, newlines aren't an issue here)
5. Unquoted fields cannot contain commas or quotes (per RFC-4180, though some implementations are lenient)
The function needs to:
- Extract field at index `idx` (0-based)
- Write the unescaped content to `out`, NUL-terminated, truncated to `cap-1` chars
- Return the unescaped length of the field, or -1 if idx is out of range
Let me think through the parsing logic:
1. Start at the beginning of the line
2. For each field, determine if it's quoted or unquoted
3. If quoted: skip opening quote, read until closing quote (handling `""` as escaped quote), then expect comma or end of line
4. If unquoted: read until comma or end of line
5. Count fields and when we reach idx, extract the content
I need to be careful about:
- The return value is the unescaped length, not the number of characters written to `out`
- Truncation: if the unescaped field is longer than `cap-1`, we write only `cap-1` chars but still return the full unescaped length
- Edge cases: empty fields, fields at start/end, consecutive commas
Let me trace through an example: `a,"b,c",d`
- Field 0: `a` (unquoted, length 1)
- Field 1: `b,c` (quoted, length 3)
- Field 2: `d` (unquoted, length 1)
Another example: `"a""b",c`
- Field 0: `a"b` (quoted with escaped quote, length 3)
- Field 1: `c` (unquoted, length 1)
Implementation approach:
1. Iterate through the line, tracking current field index
2. For each field, parse it and if it's the target idx, extract the unescaped content
3. Keep counting fields to know if idx is valid
Let me code this step by step:
```c
#include <stddef.h>
#include <string.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;
int found = 0;
int unescaped_len = 0;
while (*p != '\0' || field_idx == idx) {
// Parse one field
const char *field_start = p;
int is_quoted = 0;
int field_unescaped_len = 0;
if (*p == '"') {
// Quoted field
is_quoted = 1;
p++; // skip opening quote
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
// Escaped quote
field_unescaped_len++;
p += 2;
} else {
// Closing quote
p++;
break;
}
} else {
field_unescaped_len++;
p++;
}
}
// After closing quote, expect comma or end of line
if (*p == ',') {
p++;
} else if (*p != '\0') {
// Invalid format, but let's continue
// Actually per RFC, after closing quote must be comma or EOL
// For robustness, treat as end of field
}
} else {
// Unquoted field
while (*p != '\0' && *p != ',') {
field_unescaped_len++;
p++;
}
if (*p == ',') {
p++;
}
}
// Check if this is the target field
if (field_idx == idx) {
found = 1;
unescaped_len = field_unescaped_len;
// Extract the unescaped content
const char *src = field_start;
size_t written = 0;
if (is_quoted) {
src++; //Looking at this problem, I need to parse a single CSV line according to RFC-4180 rules:
1. Fields are separated by commas
2. Quoted fields start and end with double quotes
3. Inside quoted fields, doubled quotes `""` represent a single quote `"`
4. Quoted fields can contain commas and newlines (but since we're parsing one line, newlines aren't an issue here)
5. Unquoted fields cannot contain commas or quotes (per RFC-4180, though some implementations are lenient)
The function needs to:
- Extract field at index `idx` (0-based)
- Write the unescaped content to `out`, NUL-terminated, truncated to `cap-1` chars
- Return the unescaped length of the field, or -1 if idx is out of range
Let me think through the parsing logic:
1. Start at the beginning of the line
2. For each field, determine if it's quoted or unquoted
3. If quoted: skip opening quote, read until closing quote (handling `""` as escaped quote), then expect comma or end of line
4. If unquoted: read until comma or end of line
5. Count fields and when we reach idx, extract the content
I need to be careful about:
- The return value is the unescaped length, not the number of characters written to `out`
- Truncation: if the unescaped field is longer than `cap-1`, we write only `cap-1` chars but still return the full unescaped length
- Edge cases: empty fields, fields at start/end, consecutive commas
Let me trace through an example: `a,"b,c",d`
- Field 0: `a` (unquoted, length 1)
- Field 1: `b,c` (quoted, length 3)
- Field 2: `d` (unquoted, length 1)
Another example: `"a""b",c`
- Field 0: `a"b` (quoted with escaped quote, length 3)
- Field 1: `c` (unquoted, length 1)
Implementation approach:
1. Iterate through the line, tracking current field index
2. For each field, parse it and if it's the target idx, extract the unescaped content
3. Keep counting fields to know if idx is valid
Let me code this step by step:
```c
#include <stddef.h>
#include <string.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;
int found = 0;
int unescaped_len = 0;
while (*p != '\0' || field_idx == idx) {
// Parse one field
const char *field_start = p;
int is_quoted = 0;
int field_unescaped_len = 0;
if (*p == '"') {
// Quoted field
is_quoted = 1;
p++; // skip opening quote
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
// Escaped quote
field_unescaped_len++;
p += 2;
} else {
// Closing quote
p++;
break;
}
} else {
field_unescaped_len++;
p++;
}
}
// After closing quote, expect comma or end of line
if (*p == ',') {
p++;
} else if (*p != '\0') {
// Invalid format, but let's continue
// Actually per RFC, after closing quote must be comma or EOL
// For robustness, treat as end of field
}
} else {
// Unquoted field
while (*p != '\0' && *p != ',') {
field_unescaped_len++;
p++;
}
if (*p == ',') {
p++;
}
}
// Check if this is the target field
if (field_idx == idx) {
found = 1;
unescaped_len = field_unescaped_len;
// Extract the unescaped content
const char *src = field_start;
size_t written = 0;
if (is_quoted) {
src++; //Looking at this problem, I need to parse a single CSV line according to RFC-4180 rules:
1. Fields are separated by commas
2. Quoted fields start and end with double quotes
3. Inside quoted fields, doubled quotes `""` represent a single quote `"`
4. Quoted fields can contain commas and newlines (but since we're parsing one line, newlines aren't an issue here)
5. Unquoted fields cannot contain commas or quotes (per RFC-4180, though some implementations are lenient)
The function needs to:
- Extract field at index `idx` (0-based)
- Write the unescaped content to `out`, NUL-terminated, truncated to `cap-1` chars
- Return the unescaped length of the field, or -1 if idx is out of range
Let me think through the parsing logic:
1. Start at the beginning of the line
2. For each field, determine if it's quoted or unquoted
3. If quoted: skip opening quote, read until closing quote (handling `""` as escaped quote), then expect comma or end of line
4. If unquoted: read until comma or end of line
5. Count fields and when we reach idx, extract the content
I need to be careful about:
- The return value is the unescaped length, not the number of characters written to `out`
- Truncation: if the unescaped field is longer than `cap-1`, we write only `cap-1` chars but still return the full unescaped length
- Edge cases: empty fields, fields at start/end, consecutive commas
Let me trace through an example: `a,"b,c",d`
- Field 0: `a` (unquoted, length 1)
- Field 1: `b,c` (quoted, length 3)
- Field 2: `d` (unquoted, length 1)
Another example: `"a""b",c`
- Field 0: `a"b` (quoted with escaped quote, length 3)
- Field 1: `c` (unquoted, length 1)
Implementation approach:
1. Iterate through the line, tracking current field index
2. For each field, parse it and if it's the target idx, extract the unescaped content
3. Keep counting fields to know if idx is valid
Let me code this step by step:
```c
#include <stddef.h>
#include <string.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;
int found = 0;
int unescaped_len = 0;
while (*p != '\0' || field_idx == idx) {
// Parse one field
const char *field_start = p;
int is_quoted = 0;
int field_unescaped_len = 0;
if (*p == '"') {
// Quoted field
is_quoted = 1;
p++; // skip opening quote
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
// Escaped quote
field_unescaped_len++;
p += 2;
} else {
// Closing quote
p++;
break;
}
} else {
field_unescaped_len++;
p++;
}
}
// After closing quote, expect comma or end of line
if (*p == ',') {
p++;
} else if (*p != '\0') {
// Invalid format, but let's continue
// Actually per RFC, after closing quote must be comma or EOL
// For robustness, treat as end of field
}
} else {
// Unquoted field
while (*p != '\0' && *p != ',') {
field_unescaped_len++;
p++;
}
if (*p == ',') {
p++;
}
}
// Check if this is the target field
if (field_idx == idx) {
found = 1;
unescaped_len = field_unescaped_len;
// Extract the unescaped content
const char *src = field_start;
size_t written = 0;
if (is_quoted) {
src++; //Served via llama.cpp CUDA · 51.7 tok/s · 2.0 GB RAM · coding total 108/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;
// Skip leading whitespace
char *start = s;
while (*start && isspace((unsigned char)*start)) {
start++;
}
// If string is all whitespace
if (*start == '\0') {
*s = '\0';
return;
}
// Find end of non-whitespace content
char *end = start;
while (*end && !isspace((unsigned char)*end)) {
end++;
}
// Move content to beginning and null-terminate
memmove(s, start, end - start);
s[end - start] = '\0';
}#include <ctype.h>
void trim(char *s) {
if (s == NULL || *s == '\0') return;
// Skip leading whitespace
char *start = s;
while (*start && isspace((unsigned char)*start)) {
start++;
}
// If string is all whitespace
if (*start == '\0') {
*s = '\0';
return;
}
// Find end of non-whitespace content
char *end = start;
while (*end && !isspace((unsigned char)*end)) {
end++;
}
// Move content to beginning and null-terminate
memmove(s, start, end - start);
s[end - start] = '\0';
}#include <ctype.h>
void trim(char *s) {
if (s == NULL || *s == '\0') return;
// Skip leading whitespace
char *start = s;
while (*start && isspace((unsigned char)*start)) {
start++;
}
// If string is all whitespace
if (*start == '\0') {
*s = '\0';
return;
}
// Find end of non-whitespace content
char *end = start;
while (*end && !isspace((unsigned char)*end)) {
end++;
}
// Move content to beginning and null-terminate
memmove(s, start, end - start);
s[end - start] = '\0';
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
#include <string.h>
#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int field = 0;
size_t out_len = 0;
while (field <= idx) {
// Skip leading whitespace (not required by RFC 4180, but common practice)
while (*p == ' ' || *p == '\t') {
p++;
}
if (*p == '\0') {
return -1; // End of line reached before finding field
}
if (field == idx) {
// Process the target field
size_t remaining_cap = cap - 1; // Leave room for NUL
size_t unescaped_len = 0;
if (*p == '"') {
// Quoted field
p++; // Skip opening quote
while (*p != '\0' && *p != '"') {
if (*p == '"' && *(p + 1) == '"') {
// Escaped quote
if (out_len < remaining_cap) {
out[out_len++] = '"';
}
unescaped_len++;
p += 2;
} else {
if (out_len < remaining_cap) {
out[out_len++] = *p;
}
unescaped_len++;
p++;
}
}
if (*p == '"') {
p++; // Skip closing quote
}
} else {
// Unquoted field
while (*p != '\0' && *p != ',') {
if (out_len < remaining_cap) {
out[out_len++] = *p;
}
unescaped_len++;
p++;
}
}
// NUL-terminate
out[out_len] = '\0';
// Skip trailing whitespace after field (not required by RFC 4180)
while (*p == ' ' || *p == '\t') {
p++;
}
// Skip comma if present
if (*p == ',') {
p++;
}
return (int)unescaped_len;
}
// Skip current field
if (*p == '"') {
p++; // Skip opening quote
while (*p != '\0' && *p != '"') {
if (*p == '"' && *(p + 1) == '"') {
p += 2;
} else {
p++;
}
}
if (*p == '"') {
p++; // Skip closing quote
}
} else {
while (*p != '\0' && *p != ',') {
p++;
}
}
// Skip comma
if (*p == ',') {
p++;
}
field++;
}
return -1;
}#include <string.h>
#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int field = 0;
size_t out_len = 0;
while (field <= idx) {
// Skip leading whitespace (not required by RFC 4180, but common practice)
while (*p == ' ' || *p == '\t') {
p++;
}
if (*p == '\0') {
return -1; // End of line reached before finding field
}
if (field == idx) {
// Process the target field
size_t remaining_cap = cap - 1; // Leave room for NUL
size_t unescaped_len = 0;
if (*p == '"') {
// Quoted field
p++; // Skip opening quote
while (*p != '\0' && *p != '"') {
if (*p == '"' && *(p + 1) == '"') {
// Escaped quote
if (out_len < remaining_cap) {
out[out_len++] = '"';
}
unescaped_len++;
p += 2;
} else {
if (out_len < remaining_cap) {
out[out_len++] = *p;
}
unescaped_len++;
p++;
}
}
if (*p == '"') {
p++; // Skip closing quote
}
} else {
// Unquoted field
while (*p != '\0' && *p != ',') {
if (out_len < remaining_cap) {
out[out_len++] = *p;
}
unescaped_len++;
p++;
}
}
// NUL-terminate
out[out_len] = '\0';
// Skip trailing whitespace after field (not required by RFC 4180)
while (*p == ' ' || *p == '\t') {
p++;
}
// Skip comma if present
if (*p == ',') {
p++;
}
return (int)unescaped_len;
}
// Skip current field
if (*p == '"') {
p++; // Skip opening quote
while (*p != '\0' && *p != '"') {
if (*p == '"' && *(p + 1) == '"') {
p += 2;
} else {
p++;
}
}
if (*p == '"') {
p++; // Skip closing quote
}
} else {
while (*p != '\0' && *p != ',') {
p++;
}
}
// Skip comma
if (*p == ',') {
p++;
}
field++;
}
return -1;
}#include <string.h>
#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int field = 0;
size_t out_len = 0;
while (field <= idx) {
// Skip leading whitespace (not required by RFC 4180, but common practice)
while (*p == ' ' || *p == '\t') {
p++;
}
if (*p == '\0') {
return -1; // End of line reached before finding field
}
if (field == idx) {
// Process the target field
size_t remaining_cap = cap - 1; // Leave room for NUL
size_t unescaped_len = 0;
if (*p == '"') {
// Quoted field
p++; // Skip opening quote
while (*p != '\0' && *p != '"') {
if (*p == '"' && *(p + 1) == '"') {
// Escaped quote
if (out_len < remaining_cap) {
out[out_len++] = '"';
}
unescaped_len++;
p += 2;
} else {
if (out_len < remaining_cap) {
out[out_len++] = *p;
}
unescaped_len++;
p++;
}
}
if (*p == '"') {
p++; // Skip closing quote
}
} else {
// Unquoted field
while (*p != '\0' && *p != ',') {
if (out_len < remaining_cap) {
out[out_len++] = *p;
}
unescaped_len++;
p++;
}
}
// NUL-terminate
out[out_len] = '\0';
// Skip trailing whitespace after field (not required by RFC 4180)
while (*p == ' ' || *p == '\t') {
p++;
}
// Skip comma if present
if (*p == ',') {
p++;
}
return (int)unescaped_len;
}
// Skip current field
if (*p == '"') {
p++; // Skip opening quote
while (*p != '\0' && *p != '"') {
if (*p == '"' && *(p + 1) == '"') {
p += 2;
} else {
p++;
}
}
if (*p == '"') {
p++; // Skip closing quote
}
} else {
while (*p != '\0' && *p != ',') {
p++;
}
}
// Skip comma
if (*p == ',') {
p++;
}
field++;
}
return -1;
}Served via llama.cpp CUDA · 27.8 tok/s · 1.0 GB RAM · coding total 108/126
No failures.
Implement in C11: `double rpn_eval(const char *expr)`. Evaluates a space-separated Reverse Polish Notation expression of non-negative integers and operators + - * /. Returns the result as a double. Input is always valid and non-empty. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_STACK 1000
double rpn_eval(const char *expr) {
double stack[MAX_STACK];
int top = -1;
char *token = strtok((char *)expr, " ");
while (token != NULL) {
if (isdigit(token[0])) {
stack[++top] = atof(token);
} else {
double b = stack[top--];
double a = stack[top--];
switch (token[0]) {
case '+': stack[++top] = a + b; break;
case '-': stack[++top] = a - b; break;
case '*': stack[++top] = a * b; break;
case '/': stack[++top] = a / b; break;
}
}
token = strtok(NULL, " ");
}
return stack[top];
}#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_STACK 1000
double rpn_eval(const char *expr) {
double stack[MAX_STACK];
int top = -1;
char *token = strtok((char *)expr, " ");
while (token != NULL) {
if (isdigit(token[0])) {
stack[++top] = atof(token);
} else {
double b = stack[top--];
double a = stack[top--];
switch (token[0]) {
case '+': stack[++top] = a + b; break;
case '-': stack[++top] = a - b; break;
case '*': stack[++top] = a * b; break;
case '/': stack[++top] = a / b; break;
}
}
token = strtok(NULL, " ");
}
return stack[top];
}#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_STACK 1000
double rpn_eval(const char *expr) {
double stack[MAX_STACK];
int top = -1;
char *token = strtok((char *)expr, " ");
while (token != NULL) {
if (isdigit(token[0])) {
stack[++top] = atof(token);
} else {
double b = stack[top--];
double a = stack[top--];
switch (token[0]) {
case '+': stack[++top] = a + b; break;
case '-': stack[++top] = a - b; break;
case '*': stack[++top] = a * b; break;
case '/': stack[++top] = a / b; break;
}
}
token = strtok(NULL, " ");
}
return stack[top];
}Served via llama.cpp CUDA · 48.9 tok/s · 1.3 GB RAM · coding total 107/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)
{
unsigned count = 0;
while (x) {
x &= x - 1; /* clear the least‑significant set bit */
++count;
}
return count;
}/* Count the number of set bits in an unsigned integer (C11) */
static inline unsigned popcount(unsigned x)
{
unsigned count = 0;
while (x) {
x &= x - 1; /* clear the least‑significant set bit */
++count;
}
return count;
}/* Count the number of set bits in an unsigned integer (C11) */
static inline unsigned popcount(unsigned x)
{
unsigned count = 0;
while (x) {
x &= x - 1; /* clear the least‑significant set bit */
++count;
}
return count;
}Implement in C11: `uint32_t bswap32(uint32_t x)`. Returns x with its four bytes reversed (endian swap). Include <stdint.h> yourself. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdint.h>
static inline uint32_t bswap32(uint32_t x)
{
return ((x & 0x000000FFU) << 24) |
((x & 0x0000FF00U) << 8) |
((x & 0x00FF0000U) >> 8) |
((x & 0xFF000000U) >> 24);
}#include <stdint.h>
static inline uint32_t bswap32(uint32_t x)
{
return ((x & 0x000000FFU) << 24) |
((x & 0x0000FF00U) << 8) |
((x & 0x00FF0000U) >> 8) |
((x & 0xFF000000U) >> 24);
}#include <stdint.h>
static inline uint32_t bswap32(uint32_t x)
{
return ((x & 0x000000FFU) << 24) |
((x & 0x0000FF00U) << 8) |
((x & 0x00FF0000U) >> 8) |
((x & 0xFF000000U) >> 24);
}Implement in C11: `void 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 <stddef.h>
typedef struct {
int data[8];
int head;
int count;
} Ring;
static inline void ring_init(Ring *r) {
r->head = 0;
r->count = 0;
}
static inline int ring_push(Ring *r, int v) {
if (r->count == 8) {
return -1; // buffer full
}
int tail = (r->head + r->count) % 8;
r->data[tail] = v;
r->count++;
return 0; // success
}
static inline int ring_pop(Ring *r, int *out) {
if (r->count == 0) {
return -1; // buffer empty
}
*out = r->data[r->head];
r->head = (r->head + 1) % 8;
r->count--;
return 0; // success
}#include <stddef.h>
typedef struct {
int data[8];
int head;
int count;
} Ring;
static inline void ring_init(Ring *r) {
r->head = 0;
r->count = 0;
}
static inline int ring_push(Ring *r, int v) {
if (r->count == 8) {
return -1; // buffer full
}
int tail = (r->head + r->count) % 8;
r->data[tail] = v;
r->count++;
return 0; // success
}
static inline int ring_pop(Ring *r, int *out) {
if (r->count == 0) {
return -1; // buffer empty
}
*out = r->data[r->head];
r->head = (r->head + 1) % 8;
r->count--;
return 0; // success
}No failures.
Served via llama.cpp CUDA · 16.1 tok/s · 3.8 GB RAM · coding total 105/126
No failures.
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';
}Served via llama.cpp CUDA · 81.8 tok/s · 1.3 GB RAM · coding total 102/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.
#include <stdint.h>
static unsigned popcount(unsigned x)
{
unsigned count = 0;
while (x)
{
x &= x - 1; // clear the least significant set bit
++count;
}
return count;
}#include <stdint.h>
static unsigned popcount(unsigned x)
{
unsigned count = 0;
while (x)
{
x &= x - 1; // clear the least significant set bit
++count;
}
return count;
}Implement in C11: `int glob_match(const char *pat, const char *str)`. Returns 1 if str matches the shell-style pattern pat, else 0. '*' matches any sequence (including empty), '?' matches exactly one character. All other characters match literally. The whole string must match. Reply with only a C code block. No main function, no tests, no explanation.
(no code extracted)(no code extracted)Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
(no code extracted)(no code extracted)(no code extracted)Served via llama.cpp CUDA · 55.7 tok/s · 1.7 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) {
in_word = true;
count++;
}
s++;
}
return count;
}#include <ctype.h>
#include <stdbool.h>
int count_words(const char *s) {
if (s == NULL) {
return 0;
}
int count = 0;
bool in_word = false;
while (*s != '\0') {
if (isspace((unsigned char)*s)) {
in_word = false;
} else {
if (!in_word) {
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 va = *(const int *)a;
int vb = *(const int *)b;
if (va > vb) {
return 1;
} else if (va < vb) {
return -1;
} else {
return 0;
}
}int cmp_desc(const void *a, const void *b) {
int va = *(const int *)a;
int vb = *(const int *)b;
if (va > vb) {
return 1;
} else if (va < vb) {
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.
```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 || idx < 0) return -1;
int current_idx = 0;
const char *p = line;
while (true) {
if (current_idx == idx) {
size_t written = 0;
bool in_quotes = false;
// Check if field is quoted
if (*p == '"') {
in_quotes = true;
p++;
}
while (*p != '\0') {
if (in_quotes) {
if (*p == '"') {
if (*(p + 1) == '"') {
// Escaped quote
if (written < cap - 1) {
out[written++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = false;
p++;
break;
}
}
} else {
// If not in quotes, a comma or end of string marks field end
if (*p == ',' || *p == '\0') break;
}
if (written < cap - 1) {
out[written] = *p;
}
written++;
p++;
}
// Skip to the end of this field in the source string to allow iteration
// if we were looping, but since we return, we just finalize.
// However, we must handle the case where the field ended but the line continues.
// The logic above handles the extraction. We need to return the unescaped length.
// Re-calculating unescaped length correctly requires a separate pass or tracking.
// Let's refine the loop to track actual unescaped length.
return (int)written;
}
// Skip current field to move to next idx
bool field_quoted = false;
if (*p == '"') {
field_quoted = true;
p++;
}
if (field_quoted) {
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') p += 2;
else { p++; break; }
} else {
p++;
}
}
if (*p == '"') p++; // skip closing quote
} else {
while (*p != '\0' && *p != ',') p++;
}
if (*p == ',') {
p++;
current_idx++;
} else {
// End of line reached before idx
break;
}
}
return -1;
}
// Refined implementation to ensure correct unescaped length and truncation logic
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (!line || !out || idx < 0) return -1;
const char *p = line;
int current_idx = 0;
while (current_idx <= idx) {
bool is_quoted = (*p == '"');
const char *field_start = p;
if (is_quoted) p++;
const char *field_end = NULL;
if (is_quoted) {
while (*p) {
if (*p == '"' && *(p + 1) == '"') {
p += 2;
} else if (*p == '"') {
p++;
field_end = p;
break;
} else {
p++;
}
}
} else {
while (*p && *p != ',') p++;
field_end = p;
}
if (current_idx == idx) {
// We found the field. Now unescape it.
size_t unescaped_len = 0;
const char *scan = is_quoted ? field_start + 1 : field_start;
const char *end_scan = field_end;
// First pass: calculate true unescaped length
while (scan < end_scan) {
if (is_quoted#include <stddef.h>
#include <stdbool.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) return -1;
int current_idx = 0;
const char *p = line;
while (true) {
if (current_idx == idx) {
size_t written = 0;
bool in_quotes = false;
if (*p == '"') {
in_quotes = true;
p++;
}
while (*p != '\0') {
if (in_quotes) {
if (*p == '"') {
if (*(p + 1) == '"') {
// Escaped quote
if (written < cap - 1) {
out[written++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = false;
p++;
break;
}
} else {
if (written < cap - 1) {
out[written++] = *p;
}
p++;
}
} else {
// We are outside quotes, but this shouldn't happen
// if the CSV is well-formed and we are in the target field.
break;
}
}
// If we were in quotes and didn't find the closing quote,
// we treat the rest of the string as the field.
out[written] = '\0';
return (int)written;
}
// Skip the current field to move to the next one
bool field_in_quotes = false;
if (*p == '"') {
field_in_quotes = true;
p++;
}
while (*p != '\0') {
if (field_in_quotes) {
if (*p == '"') {
if (*(p + 1) == '"') {
p += 2; // Skip escaped quote
} else {
field_in_quotes = false;
p++;
break;
}
} else {
p++;
}
} else {
if (*p == ',') {
break;
}
p++;
}
}
if (*p == ',') {
p++; // Skip the comma
current_idx++;
} else {
// End of line reached
break;
}
}
return -1;
}#include <stddef.h>
#include <stdbool.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) return -1;
int current_idx = 0;
const char *p = line;
while (true) {
if (current_idx == idx) {
size_t written = 0;
bool in_quotes = false;
if (*p == '"') {
in_quotes = true;
p++;
}
while (*p != '\0') {
if (in_quotes) {
if (*p == '"') {
if (*(p + 1) == '"') {
// Escaped quote
if (written < cap - 1) {
out[written++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = false;
p++;
break;
}
} else {
if (written < cap - 1) {
out[written++] = *p;
}
p++;
}
} else {
// We are outside quotes, but this shouldn't happen
// if the CSV is well-formed and we are in the target field.
break;
}
}
// If we were in quotes and didn't find the closing quote,
// we treat the rest of the string as the field.
out[written] = '\0';
return (int)written;
}
// Skip the current field to move to the next one
bool field_in_quotes = false;
if (*p == '"') {
field_in_quotes = true;
p++;
}
while (*p != '\0') {
if (field_in_quotes) {
if (*p == '"') {
if (*(p + 1) == '"') {
p += 2; // Skip escaped quote
} else {
field_in_quotes = false;
p++;
break;
}
} else {
p++;
}
} else {
if (*p == ',') {
break;
}
p++;
}
}
if (*p == ',') {
p++; // Skip the comma
current_idx++;
} else {
// End of line reached
break;
}
}
return -1;
}Served via llama.cpp CUDA · 12.3 tok/s · 3.7 GB RAM · coding total 102/126
Implement in C11: `void itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
#include <limits.h>
void itoa(int value, char *buf) {
char *p = buf;
int is_negative = 0;
unsigned int uvalue;
if (value < 0) {
is_negative = 1;
/* Handle INT_MIN specially since -INT_MIN overflows */
if (value == INT_MIN) {
uvalue = (unsigned int)(-(value + 1)) + 1u;
} else {
uvalue = (unsigned int)(-value);
}
} else {
uvalue = (unsigned int)value;
}
if (uvalue == 0) {
*p++ = '0';
} else {
char digits[10];
int i = 0;
while (uvalue > 0) {
digits[i++] = (char)('0' + (uvalue % 10));
uvalue /= 10;
}
/* Write digits in reverse order */
while (i > 0) {
*p++ = digits[--i];
}
}
if (is_negative) {
*p++ = '-';
}
*p = '\0';
}#include <limits.h>
void itoa(int value, char *buf) {
char *p = buf;
int is_negative = 0;
unsigned int uvalue;
if (value < 0) {
is_negative = 1;
/* Handle INT_MIN specially since -INT_MIN overflows */
if (value == INT_MIN) {
uvalue = (unsigned int)(-(value + 1)) + 1u;
} else {
uvalue = (unsigned int)(-value);
}
} else {
uvalue = (unsigned int)value;
}
if (uvalue == 0) {
*p++ = '0';
} else {
char digits[10];
int i = 0;
while (uvalue > 0) {
digits[i++] = (char)('0' + (uvalue % 10));
uvalue /= 10;
}
/* Write digits in reverse order */
while (i > 0) {
*p++ = digits[--i];
}
}
if (is_negative) {
*p++ = '-';
}
*p = '\0';
}#include <limits.h>
void itoa(int value, char *buf) {
char *p = buf;
int is_negative = 0;
unsigned int uvalue;
if (value < 0) {
is_negative = 1;
/* Handle INT_MIN specially since -INT_MIN overflows */
if (value == INT_MIN) {
uvalue = (unsigned int)(-(value + 1)) + 1u;
} else {
uvalue = (unsigned int)(-value);
}
} else {
uvalue = (unsigned int)value;
}
if (uvalue == 0) {
*p++ = '0';
} else {
char digits[10];
int i = 0;
while (uvalue > 0) {
digits[i++] = (char)('0' + (uvalue % 10));
uvalue /= 10;
}
/* Write digits in reverse order */
while (i > 0) {
*p++ = digits[--i];
}
}
if (is_negative) {
*p++ = '-';
}
*p = '\0';
}Implement in C11: `void trim(char *s)`. Strips leading and trailing whitespace (space, tab, newline) from s in place. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
void trim(char *s) {
if (s == NULL) return;
char *start = s;
while (*start && isspace((unsigned char)*start)) {
start++;
}
char *end = start + (start - s);
/* Find the last non-whitespace character */
char *p = s;
while (*p) p++;
while (p > start && isspace((unsigned char)*(p - 1))) {
p--;
}
/* Move the trimmed content to the beginning */
size_t len = (size_t)(p - start);
for (size_t i = 0; i < len; i++) {
s[i] = start[i];
}
s[len] = '\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 + (start - s);
/* Find the last non-whitespace character */
char *p = s;
while (*p) p++;
while (p > start && isspace((unsigned char)*(p - 1))) {
p--;
}
/* Move the trimmed content to the beginning */
size_t len = (size_t)(p - start);
for (size_t i = 0; i < len; i++) {
s[i] = start[i];
}
s[len] = '\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 + (start - s);
/* Find the last non-whitespace character */
char *p = s;
while (*p) p++;
while (p > start && isspace((unsigned char)*(p - 1))) {
p--;
}
/* Move the trimmed content to the beginning */
size_t len = (size_t)(p - start);
for (size_t i = 0; i < len; i++) {
s[i] = start[i];
}
s[len] = '\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 (cap == 0) return -1;
const char *p = line;
int field_idx = 0;
size_t out_len = 0;
while (1) {
/* Skip leading whitespace? RFC 4180 does not allow leading whitespace,
but we handle the standard case. */
if (*p == '\0') {
/* End of line */
if (field_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
return -1;
}
if (*p == '"') {
/* Quoted field */
p++;
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
/* Doubled quote -> single quote */
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
} else {
/* Closing quote */
p++;
break;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
}
/* After closing quote, expect comma or end of line */
if (*p == ',') {
p++;
field_idx++;
continue;
} else if (*p == '\0') {
if (field_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
return -1;
} else {
/* Malformed: non-comma, non-NUL after closing quote.
Treat as end of field. */
if (field_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
return -1;
}
} else {
/* Unquoted field */
while (*p != '\0' && *p != ',') {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
if (*p == ',') {
p++;
field_idx++;
continue;
} else {
/* End of line */
if (field_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
return -1;
}
}
}
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (cap == 0) return -1;
const char *p = line;
int field_idx = 0;
size_t out_len = 0;
while (1) {
/* Skip leading whitespace? RFC 4180 does not allow leading whitespace,
but we handle the standard case. */
if (*p == '\0') {
/* End of line */
if (field_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
return -1;
}
if (*p == '"') {
/* Quoted field */
p++;
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
/* Doubled quote -> single quote */
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
} else {
/* Closing quote */
p++;
break;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
}
/* After closing quote, expect comma or end of line */
if (*p == ',') {
p++;
field_idx++;
continue;
} else if (*p == '\0') {
if (field_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
return -1;
} else {
/* Malformed: non-comma, non-NUL after closing quote.
Treat as end of field. */
if (field_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
return -1;
}
} else {
/* Unquoted field */
while (*p != '\0' && *p != ',') {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
if (*p == ',') {
p++;
field_idx++;
continue;
} else {
/* End of line */
if (field_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
return -1;
}
}
}
}#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (cap == 0) return -1;
const char *p = line;
int field_idx = 0;
size_t out_len = 0;
while (1) {
/* Skip leading whitespace? RFC 4180 does not allow leading whitespace,
but we handle the standard case. */
if (*p == '\0') {
/* End of line */
if (field_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
return -1;
}
if (*p == '"') {
/* Quoted field */
p++;
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
/* Doubled quote -> single quote */
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
} else {
/* Closing quote */
p++;
break;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
}
/* After closing quote, expect comma or end of line */
if (*p == ',') {
p++;
field_idx++;
continue;
} else if (*p == '\0') {
if (field_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
return -1;
} else {
/* Malformed: non-comma, non-NUL after closing quote.
Treat as end of field. */
if (field_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
return -1;
}
} else {
/* Unquoted field */
while (*p != '\0' && *p != ',') {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
if (*p == ',') {
p++;
field_idx++;
continue;
} else {
/* End of line */
if (field_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
return -1;
}
}
}
}Served via llama.cpp CUDA · 67.0 tok/s · 2.2 GB RAM · coding total 102/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: `void trim(char *s)`. Strips leading and trailing whitespace (space, tab, newline) from s in place. Reply with only a C code block. No main function, no tests, no explanation.
#include <string.h>
#include <ctype.h>
void trim(char *s) {
if (!s) return;
// Strip leading whitespace
char *start = s;
while (isspace((unsigned char)*start)) {
start++;
}
// Strip trailing whitespace
char *end = start + strlen(start) - 1;
while (end >= start && isspace((unsigned char)*end)) {
end--;
}
// Move trimmed string to the beginning if necessary
if (start != s) {
memmove(s, start, (size_t)(end - start + 2)); // +2 for null terminator
} else {
// If no leading whitespace, just truncate trailing
*(end + 1) = '\0';
}
}#include <string.h>
#include <ctype.h>
void trim(char *s) {
if (!s) return;
// Strip leading whitespace
char *start = s;
while (isspace((unsigned char)*start)) {
start++;
}
// Strip trailing whitespace
char *end = start + strlen(start) - 1;
while (end >= start && isspace((unsigned char)*end)) {
end--;
}
// Move trimmed string to the beginning if necessary
if (start != s) {
memmove(s, start, (size_t)(end - start + 2)); // +2 for null terminator
} else {
// If no leading whitespace, just truncate trailing
*(end + 1) = '\0';
}
}#include <string.h>
#include <ctype.h>
void trim(char *s) {
if (!s) return;
// Strip leading whitespace
char *start = s;
while (isspace((unsigned char)*start)) {
start++;
}
// Strip trailing whitespace
char *end = start + strlen(start) - 1;
while (end >= start && isspace((unsigned char)*end)) {
end--;
}
// Move trimmed string to the beginning if necessary
if (start != s) {
memmove(s, start, (size_t)(end - start + 2)); // +2 for null terminator
} else {
// If no leading whitespace, just truncate trailing
*(end + 1) = '\0';
}
}No failures.
Served via llama.cpp CUDA · 88.4 tok/s · 0.9 GB RAM · coding total 101/126
Implement in C11: `int is_prime(int n)`. Returns 1 if n is prime, 0 otherwise. n <= 1 is not prime. Reply with only a C code block. No main function, no tests, no explanation.
#include <math.h>
int is_prime(int n) {
if (n <= 1) return 0;
if (n == 2) return 1;
if (n % 2 == 0) return 0;
int limit = (int)sqrt(n);
for (int i = 3; i <= limit; i += 2) {
if (n % i == 0) return 0;
}
return 1;
}#include <math.h>
int is_prime(int n) {
if (n <= 1) return 0;
if (n == 2) return 1;
if (n % 2 == 0) return 0;
int limit = (int)sqrt(n);
for (int i = 3; i <= limit; i += 2) {
if (n % i == 0) return 0;
}
return 1;
}#include <math.h>
int is_prime(int n) {
if (n <= 1) return 0;
if (n == 2) return 1;
if (n % 2 == 0) return 0;
int limit = (int)sqrt(n);
for (int i = 3; i <= limit; i += 2) {
if (n % i == 0) return 0;
}
return 1;
}Implement in C11: `void itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
#include <limits.h>
void itoa(int value, char *buf) {
char temp[12];
int i = 0;
unsigned int uvalue;
int negative = 0;
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
if (value < 0) {
negative = 1;
uvalue = (unsigned int)(-(value + 1)) + 1;
} else {
uvalue = (unsigned int)value;
}
while (uvalue > 0) {
temp[i++] = '0' + (uvalue % 10);
uvalue /= 10;
}
if (negative) {
buf[i++] = '-';
}
while (i > 0) {
buf[--i] = temp[i];
}
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);
}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 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>
int glob_match(const char *pat, const char *str) {
const char *p = pat;
const char *s = str;
const char *star = NULL;
const char *ss = NULL;
while (*p) {
if (p[1] == '*') {
star = p;
ss = s;
p++;
} else if (*p == '?') {
if (!*s) return 0;
p++;
s++;
} else if (*p == *s) {
p++;
s++;
} else if (star) {
p = star + 1;
s = ss++;
} else {
return 0;
}
}
while (*p == '*') p++;
return *p == '\0';
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (idx < 0) return -1;
size_t out_idx = 0;
int field_idx = 0;
int in_quotes = 0;
int escaped = 0;
const char *p = line;
while (*p) {
if (field_idx == idx) {
if (out_idx < cap - 1) {
if (escaped) {
out[out_idx++] = '"';
escaped = 0;
} else if (in_quotes) {
out[out_idx++] = *p;
}
} else {
return -1;
}
}
if (!in_quotes && *p == ',') {
field_idx++;
if (field_idx == idx) out_idx = 0;
p++;
continue;
}
if (in_quotes) {
if (*p == '"') {
if (p[1] == '"') {
if (field_idx == idx && out_idx < cap - 1) {
if (!escaped) {
out[out_idx++] = '"';
} else {
out[out_idx++] = '"';
escaped = 0;
}
}
p += 2;
continue;
} else {
in_quotes = 0;
p++;
continue;
}
}
} else {
if (*p == '"') {
in_quotes = 1;
p++;
continue;
}
}
if (field_idx == idx && out_idx < cap - 1) {
out[out_idx++] = *p;
}
p++;
}
if (field_idx < idx) return -1;
if (out_idx < cap) out[out_idx] = '\0';
else if (cap > 0) out[cap - 1] = '\0';
return (int)out_idx;
}#include <stddef.h>
#include <string.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 field_idx = 0;
char *out_ptr = out;
size_t out_remaining = cap;
while (*p && field_idx <= idx) {
if (field_idx == idx) {
if (*p == '"') {
p++;
while (*p) {
if (*p == '"') {
if (*(p+1) == '"') {
if (out_remaining > 1) *out_ptr++ = '"';
out_remaining--;
p += 2;
} else {
p++;
break;
}
} else {
if (out_remaining > 1) *out_ptr++ = *p;
out_remaining--;
p++;
}
}
} else {
while (*p && *p != ',') {
if (out_remaining > 1) *out_ptr++ = *p;
out_remaining--;
p++;
}
}
if (out_remaining > 0) *out_ptr = '\0';
else if (cap > 0) out[cap-1] = '\0';
return out_ptr - out;
}
if (*p == '"') {
p++;
while (*p) {
if (*p == '"') {
if (*(p+1) == '"') p += 2;
else { p++; break; }
} else {
p++;
}
}
if (*p) p++;
} else {
while (*p && *p != ',') p++;
if (*p) p++;
}
field_idx++;
}
return -1;
}#include <stddef.h>
#include <string.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 field_idx = 0;
char *out_ptr = out;
size_t out_remaining = cap;
while (*p && field_idx <= idx) {
if (field_idx == idx) {
if (*p == '"') {
p++;
while (*p) {
if (*p == '"') {
if (*(p+1) == '"') {
if (out_remaining > 1) *out_ptr++ = '"';
out_remaining--;
p += 2;
} else {
p++;
break;
}
} else {
if (out_remaining > 1) *out_ptr++ = *p;
out_remaining--;
p++;
}
}
} else {
while (*p && *p != ',') {
if (out_remaining > 1) *out_ptr++ = *p;
out_remaining--;
p++;
}
}
if (out_remaining > 0) *out_ptr = '\0';
else if (cap > 0) out[cap-1] = '\0';
return out_ptr - out;
}
if (*p == '"') {
p++;
while (*p) {
if (*p == '"') {
if (*(p+1) == '"') p += 2;
else { p++; break; }
} else {
p++;
}
}
if (*p) p++;
} else {
while (*p && *p != ',') p++;
if (*p) p++;
}
field_idx++;
}
return -1;
}Served via llama.cpp CUDA · 12.4 tok/s · 3.6 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 itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
```c
#include <limits.h>
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
char temp[12]; // Enough for INT_MIN (-2147483648) + null terminator
int i = 0;
int is_negative = 0;
if (value < 0) {
is_negative = 1;
// Handle INT_MIN specially to avoid overflow when negating
if (value == INT_MIN) {
// INT_MIN is -2147483648
// We can process it by taking absolute value in parts or using unsigned
// A safe way: convert using unsigned arithmetic
unsigned int uval = (unsigned int)(-(value + 1)) + 1; // This is tricky, let's use a different approach
// Actually, simpler: just use unsigned conversion for the magnitude
// But value is int. Let's cast to unsigned after handling sign.
// For INT_MIN, -value overflows. So we handle it by converting the positive part.
// INT_MIN = -2147483648. Magnitude is 2147483648.
// We can write the digits directly or use unsigned long.
// Let's use a generic approach with unsigned long for the magnitude.
unsigned long mag = (unsigned long)(-(long)value);
// But -(long)INT_MIN might still be problematic if long is 32-bit.
// On most systems long is 64-bit, but to be strictly portable for 32-bit int:
// Let's just do digit extraction carefully.
// Alternative: process the number as unsigned by casting after negation? No.
// Best approach for INT_MIN:
// We know INT_MIN is -2147483648.
// Let's just use a loop that works for negative numbers by dividing by 10.
// In C11, division of negative numbers truncates towards zero.
// -123 / 10 = -12, -123 % 10 = -3.
// So we can work with negative numbers directly.
// Reset and use negative arithmetic
i = 0;
int n = value;
while (n != 0) {
int digit = n % 10;
n /= 10;
// digit is negative or zero
temp[i++] = (char)('0' - digit);
}
} else {
// General negative case
int n = value;
while (n != 0) {
int digit = n % 10;
n /= 10;
temp[i++] = (char)('0' - digit);
}
}
} else {
// Positive case
int n = value;
while (n != 0) {
int digit = n % 10;
n /= 10;
temp[i++] = (char)('0' + digit);
}
}
// If we handled INT_MIN or other negatives in the special block above, we need to ensure consistency.
// The above logic for negative numbers (including INT_MIN) uses the 'n' variable and extracts digits.
// However, the INT_MIN block and the else block are duplicated. Let's refactor.
// Actually, the logic `n % 10` for negative `n` yields negative remainder.
// e.g. -123 % 10 = -3. '0' - (-3) = '3'. Correct.
// -10 % 10 = 0. '0' - 0 = '0'. Correct.
// So the same loop works for all negative numbers, including INT_MIN, provided we don't negate INT_MIN.
// Let's rewrite cleanly:
// Reset
i = 0;
int n = value;
if (n == 0) {
temp[i++] = '0';
} else if (n < 0) {
while (n != 0) {
int digit = n % 10;
n /= 10;
temp[i++] = (char)('0' - digit);```c
#include <limits.h>
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
char temp[12]; // Enough for INT_MIN (-2147483648) + null terminator
int i = 0;
int is_negative = 0;
if (value < 0) {
is_negative = 1;
// Handle INT_MIN specially to avoid overflow when negating
if (value == INT_MIN) {
// INT_MIN is -2147483648
// We can process it by taking absolute value in parts or using unsigned
// A safe way: convert using unsigned arithmetic
unsigned int uval = (unsigned int)(-(value + 1)) + 1; // This is tricky, let's use a different approach
// Actually, simpler: just use unsigned conversion for the magnitude
// But value is int. Let's cast to unsigned after handling sign.
// For INT_MIN, -value overflows. So we handle it by converting the positive part.
// INT_MIN = -2147483648. Magnitude is 2147483648.
// We can write the digits directly or use unsigned long.
// Let's use a generic approach with unsigned long for the magnitude.
unsigned long mag = (unsigned long)(-(long)value);
// But -(long)INT_MIN might still be problematic if long is 32-bit.
// On most systems long is 64-bit, but to be strictly portable for 32-bit int:
// Let's just do digit extraction carefully.
// Alternative: process the number as unsigned by casting after negation? No.
// Best approach for INT_MIN:
// We know INT_MIN is -2147483648.
// Let's just use a loop that works for negative numbers by dividing by 10.
// In C11, division of negative numbers truncates towards zero.
// -123 / 10 = -12, -123 % 10 = -3.
// So we can work with negative numbers directly.
// Reset and use negative arithmetic
i = 0;
int n = value;
while (n != 0) {
int digit = n % 10;
n /= 10;
// digit is negative or zero
temp[i++] = (char)('0' - digit);
}
} else {
// General negative case
int n = value;
while (n != 0) {
int digit = n % 10;
n /= 10;
temp[i++] = (char)('0' - digit);
}
}
} else {
// Positive case
int n = value;
while (n != 0) {
int digit = n % 10;
n /= 10;
temp[i++] = (char)('0' + digit);
}
}
// If we handled INT_MIN or other negatives in the special block above, we need to ensure consistency.
// The above logic for negative numbers (including INT_MIN) uses the 'n' variable and extracts digits.
// However, the INT_MIN block and the else block are duplicated. Let's refactor.
// Actually, the logic `n % 10` for negative `n` yields negative remainder.
// e.g. -123 % 10 = -3. '0' - (-3) = '3'. Correct.
// -10 % 10 = 0. '0' - 0 = '0'. Correct.
// So the same loop works for all negative numbers, including INT_MIN, provided we don't negate INT_MIN.
// Let's rewrite cleanly:
// Reset
i = 0;
int n = value;
if (n == 0) {
temp[i++] = '0';
} else if (n < 0) {
while (n != 0) {
int digit = n % 10;
n /= 10;
temp[i++] = (char)('0' - digit);```c
#include <limits.h>
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
char temp[12]; // Enough for INT_MIN (-2147483648) + null terminator
int i = 0;
int is_negative = 0;
if (value < 0) {
is_negative = 1;
// Handle INT_MIN specially to avoid overflow when negating
if (value == INT_MIN) {
// INT_MIN is -2147483648
// We can process it by taking absolute value in parts or using unsigned
// A safe way: convert using unsigned arithmetic
unsigned int uval = (unsigned int)(-(value + 1)) + 1; // This is tricky, let's use a different approach
// Actually, simpler: just use unsigned conversion for the magnitude
// But value is int. Let's cast to unsigned after handling sign.
// For INT_MIN, -value overflows. So we handle it by converting the positive part.
// INT_MIN = -2147483648. Magnitude is 2147483648.
// We can write the digits directly or use unsigned long.
// Let's use a generic approach with unsigned long for the magnitude.
unsigned long mag = (unsigned long)(-(long)value);
// But -(long)INT_MIN might still be problematic if long is 32-bit.
// On most systems long is 64-bit, but to be strictly portable for 32-bit int:
// Let's just do digit extraction carefully.
// Alternative: process the number as unsigned by casting after negation? No.
// Best approach for INT_MIN:
// We know INT_MIN is -2147483648.
// Let's just use a loop that works for negative numbers by dividing by 10.
// In C11, division of negative numbers truncates towards zero.
// -123 / 10 = -12, -123 % 10 = -3.
// So we can work with negative numbers directly.
// Reset and use negative arithmetic
i = 0;
int n = value;
while (n != 0) {
int digit = n % 10;
n /= 10;
// digit is negative or zero
temp[i++] = (char)('0' - digit);
}
} else {
// General negative case
int n = value;
while (n != 0) {
int digit = n % 10;
n /= 10;
temp[i++] = (char)('0' - digit);
}
}
} else {
// Positive case
int n = value;
while (n != 0) {
int digit = n % 10;
n /= 10;
temp[i++] = (char)('0' + digit);
}
}
// If we handled INT_MIN or other negatives in the special block above, we need to ensure consistency.
// The above logic for negative numbers (including INT_MIN) uses the 'n' variable and extracts digits.
// However, the INT_MIN block and the else block are duplicated. Let's refactor.
// Actually, the logic `n % 10` for negative `n` yields negative remainder.
// e.g. -123 % 10 = -3. '0' - (-3) = '3'. Correct.
// -10 % 10 = 0. '0' - 0 = '0'. Correct.
// So the same loop works for all negative numbers, including INT_MIN, provided we don't negate INT_MIN.
// Let's rewrite cleanly:
// Reset
i = 0;
int n = value;
if (n == 0) {
temp[i++] = '0';
} else if (n < 0) {
while (n != 0) {
int digit = n % 10;
n /= 10;
temp[i++] = (char)('0' - digit);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;
// Strip leading whitespace
char *start = s;
while (*start == ' ' || *start == '\t' || *start == '\n') {
start++;
}
// If the string is all whitespace or empty, null-terminate immediately
if (*start == '\0') {
*s = '\0';
return;
}
// Strip trailing whitespace
char *end = s + strlen(s) - 1;
while (end > start && (*end == ' ' || *end == '\t' || *end == '\n')) {
end--;
}
*(end + 1) = '\0';
// Move the trimmed string to the beginning if necessary
if (start != s) {
memmove(s, start, (size_t)(end - start + 1));
}
}void trim(char *s) {
if (!s) return;
// Strip leading whitespace
char *start = s;
while (*start == ' ' || *start == '\t' || *start == '\n') {
start++;
}
// If the string is all whitespace or empty, null-terminate immediately
if (*start == '\0') {
*s = '\0';
return;
}
// Strip trailing whitespace
char *end = s + strlen(s) - 1;
while (end > start && (*end == ' ' || *end == '\t' || *end == '\n')) {
end--;
}
*(end + 1) = '\0';
// Move the trimmed string to the beginning if necessary
if (start != s) {
memmove(s, start, (size_t)(end - start + 1));
}
}void trim(char *s) {
if (!s) return;
// Strip leading whitespace
char *start = s;
while (*start == ' ' || *start == '\t' || *start == '\n') {
start++;
}
// If the string is all whitespace or empty, null-terminate immediately
if (*start == '\0') {
*s = '\0';
return;
}
// Strip trailing whitespace
char *end = s + strlen(s) - 1;
while (end > start && (*end == ' ' || *end == '\t' || *end == '\n')) {
end--;
}
*(end + 1) = '\0';
// Move the trimmed string to the beginning if necessary
if (start != s) {
memmove(s, start, (size_t)(end - start + 1));
}
}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;
// Skip to the desired field index
while (current_idx < idx) {
if (*p == '\0') {
return -1; // idx out of range
}
if (*p == '"') {
// Quoted field
p++; // skip opening quote
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
p += 2; // escaped quote
} else {
p++; // closing quote
break;
}
} else {
p++;
}
}
if (*p != '"') {
return -1; // malformed quoted field
}
// Skip comma after closing quote if present
if (*(p + 1) == ',') {
p += 2;
} else if (*(p + 1) == '\0') {
p++;
} else {
return -1; // malformed CSV
}
} else {
// Unquoted field
while (*p != '\0' && *p != ',') {
p++;
}
if (*p == ',') {
p++;
} else if (*p == '\0') {
p++;
}
}
current_idx++;
}
// Now p points to the start of the field at idx
// Check if we've reached end of string
if (*p == '\0') {
return -1; // idx out of range
}
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 {
// Closing quote
p++;
break;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
}
if (*p != '"') {
return -1; // malformed quoted field
}
// After closing quote, expect comma or end of string
if (*p == ',') {
// valid, field ends here
} else if (*p != '\0') {
return -1; // malformed CSV
}
} else {
// Unquoted field
while (*p != '\0' && *p != ',') {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
}
out[out_len] = '\0';
return (int)out_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;
// Skip to the desired field index
while (current_idx < idx) {
if (*p == '\0') {
return -1; // idx out of range
}
if (*p == '"') {
// Quoted field
p++; // skip opening quote
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
p += 2; // escaped quote
} else {
p++; // closing quote
break;
}
} else {
p++;
}
}
if (*p != '"') {
return -1; // malformed quoted field
}
// Skip comma after closing quote if present
if (*(p + 1) == ',') {
p += 2;
} else if (*(p + 1) == '\0') {
p++;
} else {
return -1; // malformed CSV
}
} else {
// Unquoted field
while (*p != '\0' && *p != ',') {
p++;
}
if (*p == ',') {
p++;
} else if (*p == '\0') {
p++;
}
}
current_idx++;
}
// Now p points to the start of the field at idx
// Check if we've reached end of string
if (*p == '\0') {
return -1; // idx out of range
}
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 {
// Closing quote
p++;
break;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
}
if (*p != '"') {
return -1; // malformed quoted field
}
// After closing quote, expect comma or end of string
if (*p == ',') {
// valid, field ends here
} else if (*p != '\0') {
return -1; // malformed CSV
}
} else {
// Unquoted field
while (*p != '\0' && *p != ',') {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
}
out[out_len] = '\0';
return (int)out_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;
// Skip to the desired field index
while (current_idx < idx) {
if (*p == '\0') {
return -1; // idx out of range
}
if (*p == '"') {
// Quoted field
p++; // skip opening quote
while (*p != '\0') {
if (*p == '"') {
if (*(p + 1) == '"') {
p += 2; // escaped quote
} else {
p++; // closing quote
break;
}
} else {
p++;
}
}
if (*p != '"') {
return -1; // malformed quoted field
}
// Skip comma after closing quote if present
if (*(p + 1) == ',') {
p += 2;
} else if (*(p + 1) == '\0') {
p++;
} else {
return -1; // malformed CSV
}
} else {
// Unquoted field
while (*p != '\0' && *p != ',') {
p++;
}
if (*p == ',') {
p++;
} else if (*p == '\0') {
p++;
}
}
current_idx++;
}
// Now p points to the start of the field at idx
// Check if we've reached end of string
if (*p == '\0') {
return -1; // idx out of range
}
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 {
// Closing quote
p++;
break;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
}
if (*p != '"') {
return -1; // malformed quoted field
}
// After closing quote, expect comma or end of string
if (*p == ',') {
// valid, field ends here
} else if (*p != '\0') {
return -1; // malformed CSV
}
} else {
// Unquoted field
while (*p != '\0' && *p != ',') {
if (out_len < cap - 1) {
out[out_len++] = *p;
}
p++;
}
}
out[out_len] = '\0';
return (int)out_len;
}Served via llama.cpp CUDA · 13.7 tok/s · 1.2 GB RAM · coding total 98/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;
}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 = 463648286; // (INT_MAX + 1) % 10
*buf++ = '4';
value = 746340269; // (INT_MAX + 1) / 10
*buf++ = '6';
value = 322384093; // (INT_MAX + 1) % 10
*buf++ = '3';
value = 644768186; // (INT_MAX + 1) / 10
*buf++ = '4';
value = 188495440; // (INT_MAX + 1) % 10
*buf++ = '8';
value = 37699088; // (INT_MAX + 1) / 10
*buf++ = '2';
value = 18849544; // (INT_MAX + 1) % 10
*buf++ = '8';
value = 1884954; // (INT_MAX + 1) / 10
*buf++ = '4';
value = 942477; // (INT_MAX + 1) % 10
*buf++ = '7';
value = 94247; // (INT_MAX + 1) / 10
*buf++ = '7';
value = 47123; // (INT_MAX + 1) % 10
*buf++ = '3';
value = 4712; // (INT_MAX + 1) / 10
*buf++ = '2';
value = 359; // (INT_MAX + 1) % 10
*buf++ = '5';
value = 35; // (INT_MAX + 1) / 10
*buf++ = '9';
value = 5; // (INT_MAX + 1) % 10
*buf++ = '5';
*buf = '\0';
return;
}
value = -value;
}
char temp[12];
int i = 0;
do {
temp[i++] = '0' + (value % 10);
value /= 10;
} while (value > 0);
if (is_negative) {
*buf++ = '-';
}
while (i > 0) {
*buf++ = temp[--i];
}
*buf = '\0';
}#include <limits.h>
#include <stdbool.h>
void itoa(int value, char *buf) {
bool is_negative = false;
if (value < 0) {
is_negative = true;
if (value == INT_MIN) {
// Handle INT_MIN separately to avoid overflow
*buf++ = '-';
value = -(value + 1);
*buf++ = '2';
value = 1431655765; // (INT_MAX + 1) / 10
*buf++ = '1';
value = 463648286; // (INT_MAX + 1) % 10
*buf++ = '4';
value = 746340269; // (INT_MAX + 1) / 10
*buf++ = '6';
value = 322384093; // (INT_MAX + 1) % 10
*buf++ = '3';
value = 644768186; // (INT_MAX + 1) / 10
*buf++ = '4';
value = 188495440; // (INT_MAX + 1) % 10
*buf++ = '8';
value = 37699088; // (INT_MAX + 1) / 10
*buf++ = '2';
value = 18849544; // (INT_MAX + 1) % 10
*buf++ = '8';
value = 1884954; // (INT_MAX + 1) / 10
*buf++ = '4';
value = 942477; // (INT_MAX + 1) % 10
*buf++ = '7';
value = 94247; // (INT_MAX + 1) / 10
*buf++ = '7';
value = 47123; // (INT_MAX + 1) % 10
*buf++ = '3';
value = 4712; // (INT_MAX + 1) / 10
*buf++ = '2';
value = 359; // (INT_MAX + 1) % 10
*buf++ = '5';
value = 35; // (INT_MAX + 1) / 10
*buf++ = '9';
value = 5; // (INT_MAX + 1) % 10
*buf++ = '5';
*buf = '\0';
return;
}
value = -value;
}
char temp[12];
int i = 0;
do {
temp[i++] = '0' + (value % 10);
value /= 10;
} while (value > 0);
if (is_negative) {
*buf++ = '-';
}
while (i > 0) {
*buf++ = temp[--i];
}
*buf = '\0';
}Implement in C11: `void trim(char *s)`. Strips leading and trailing whitespace (space, tab, newline) from s in place. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
void trim(char *s) {
if (s == NULL) return;
char *end = s + strlen(s) - 1;
while (end >= s && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
char *start = s;
while (*start && isspace((unsigned char)*start)) {
start++;
}
memmove(s, start, end - start + 2);
}#include <ctype.h>
void trim(char *s) {
if (!s) return;
// Trim leading whitespace
while (isspace((unsigned char)*s)) {
s++;
}
// Trim trailing whitespace
char *end = s + strlen(s) - 1;
while (end >= s && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
}#include <ctype.h>
void trim(char *s) {
if (!s) return;
// Trim leading whitespace
while (isspace((unsigned char)*s)) {
s++;
}
// Trim trailing whitespace
char *end = s + strlen(s) - 1;
while (end >= s && isspace((unsigned char)*end)) {
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.
#include <ctype.h>
#include <stddef.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) return -1;
// Skip leading whitespace
while (isspace((unsigned char)*s)) s++;
// Check for empty string
if (*s == '\0') return -1;
// Handle sign
int sign = 1;
if (*s == '-') {
sign = -1;
s++;
} else if (*s == '+') {
s++;
}
// Check if there's at least one digit
if (!isdigit((unsigned char)*s)) return -1;
// Parse digits
long long value = 0;
while (isdigit((unsigned char)*s)) {
value = value * 10 + (*s - '0');
s++;
}
// Check for trailing non-digit characters
while (isspace((unsigned char)*s)) s++;
if (*s != '\0') return -1;
*out = sign * value;
return 0;
}#include <ctype.h>
#include <stddef.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) return -1;
// Skip leading whitespace
while (isspace((unsigned char)*s)) s++;
// Check for empty string
if (*s == '\0') return -1;
// Handle sign
int sign = 1;
if (*s == '-') {
sign = -1;
s++;
} else if (*s == '+') {
s++;
}
// Check if there's at least one digit
if (!isdigit((unsigned char)*s)) return -1;
// Parse digits
long long value = 0;
while (isdigit((unsigned char)*s)) {
value = value * 10 + (*s - '0');
s++;
}
// Check for trailing non-digit characters
while (isspace((unsigned char)*s)) s++;
if (*s != '\0') return -1;
*out = sign * value;
return 0;
}Implement in C11: `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;
char *q = out;
size_t len = 0;
while (*p && field <= idx) {
if (field == idx) {
if (*p == '"') {
p++;
while (*p && len < cap - 1) {
if (*p == '"') {
p++;
if (*p == '"') {
if (q != out) *q++ = '"';
len++;
p++;
} else {
break;
}
} else {
*q++ = *p++;
len++;
}
}
} else {
while (*p != ',' && *p != '\0' && len < cap - 1) {
*q++ = *p++;
len++;
}
}
*q = '\0';
return len;
}
if (*p == '"') {
p++;
while (*p && *p != '"') {
if (*p == '"') p++;
p++;
}
if (*p == '"') p++;
} else {
while (*p && *p != ',') p++;
}
if (*p == ',') p++;
field++;
}
return -1;
}#include <string.h>
#include <ctype.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (cap == 0) return -1;
const char *p = line;
int field = 0;
int in_quotes = 0;
size_t out_pos = 0;
while (*p && field <= idx) {
if (in_quotes) {
if (*p == '"') {
const char *next = p + 1;
if (*next == '"') {
if (out_pos < cap - 1) out[out_pos++] = '"';
p = next + 1;
} else {
in_quotes = 0;
p++;
}
} else {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
} else {
if (*p == '"') {
in_quotes = 1;
p++;
} else if (*p == ',') {
field++;
p++;
if (field > idx) break;
} else {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
}
}
if (field != idx) return -1;
if (out_pos < cap) out[out_pos] = '\0';
return (int)out_pos;
}#include <string.h>
#include <ctype.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (cap == 0) return -1;
const char *p = line;
int field = 0;
int in_quotes = 0;
size_t out_pos = 0;
while (*p && field <= idx) {
if (in_quotes) {
if (*p == '"') {
const char *next = p + 1;
if (*next == '"') {
if (out_pos < cap - 1) out[out_pos++] = '"';
p = next + 1;
} else {
in_quotes = 0;
p++;
}
} else {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
} else {
if (*p == '"') {
in_quotes = 1;
p++;
} else if (*p == ',') {
field++;
p++;
if (field > idx) break;
} else {
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
}
}
}
if (field != idx) return -1;
if (out_pos < cap) out[out_pos] = '\0';
return (int)out_pos;
}Served via llama.cpp CUDA · 66.5 tok/s · 2.2 GB RAM · coding total 96/126
Implement in C11: `void itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
```c
#include <limits.h>
#include <stddef.h>
void itoa(int value, char *buf) {
char *p = buf;
int is_negative = 0;
if (value == INT_MIN) {
// Handle INT_MIN specially to avoid overflow when negating
// INT_MIN = -2147483648
// We can't do -value, so we subtract 1 and negate, then add 1 at the end
// Or simpler: manually handle the digits.
// Let's use a safe approach: subtract 1, negate, then add 1 to the last digit.
// Actually, simpler: just process digits by subtracting 10 repeatedly or using modulo.
// But modulo on negative numbers in C is implementation-defined before C99,
// and in C99/C11 it truncates towards zero.
// -2147483648 % 10 = -8.
// So we can use modulo and handle the sign.
// Let's stick to the standard logic:
// If value is INT_MIN, we can't do -value.
// We can do: value = value + 1; is_negative = 1; then process, then add 1 to the last digit.
// But that's messy.
// Better approach:
// Use a temporary buffer or just handle the digits carefully.
// Since we are writing to buf, we can just do:
// if (value == INT_MIN) {
// // -2147483648
// // We can't negate it.
// // Let's just subtract 10 repeatedly? No, too slow.
// // Let's use the property that -2147483648 % 10 = -8.
// // We can collect digits as negative numbers and then convert to positive chars.
// }
// Standard robust way:
// If value is INT_MIN, we can't negate it.
// We can do: value = value + 1; is_negative = 1; then process, then add 1 to the last digit.
// But let's try a different way:
// Just use the fact that we can handle negative numbers by taking modulo.
// In C11, -2147483648 % 10 is -8.
// So we can do:
// while (value != 0) {
// int digit = value % 10;
// value /= 10;
// *p++ = '0' + (digit < 0 ? -digit : digit);
// }
// But wait, -2147483648 / 10 is -214748364.
// -214748364 % 10 is -4.
// This works!
// The only issue is if value is 0, we need to handle it.
// Let's implement the general case:
// If value is INT_MIN, we can't do -value, but we can do value % 10 and value / 10.
// The only problem is if value is 0, we need to output "0".
// So:
// if (value == 0) {
// *p++ = '0';
// *p = '\0';
// return;
// }
// if (value < 0) {
// is_negative = 1;
// // We can't do -value if value is INT_MIN.
// // But we can process digits as negative.
// }
// Let's rewrite the loop to handle negative digits correctly.
// We will collect digits in reverse order.
// If value is INT_MIN, we can't negate it, but we can still do modulo and division.
// The digits will be negative.
// We can convert them to positive by taking absolute value.
// But abs(INT_MIN) is undefined behavior.
// So we must avoid abs(INT_MIN).
// Instead, we can do: digit = value % 10; value /= 10;
// Then digit is negative.
// We can convert digit to positive by: digit = -digit;
// But if digit is -```c
#include <limits.h>
#include <stddef.h>
void itoa(int value, char *buf) {
char *p = buf;
int is_negative = 0;
if (value == INT_MIN) {
// Handle INT_MIN specially to avoid overflow when negating
// INT_MIN = -2147483648
// We can't do -value, so we subtract 1 and negate, then add 1 at the end
// Or simpler: manually handle the digits.
// Let's use a safe approach: subtract 1, negate, then add 1 to the last digit.
// Actually, simpler: just process digits by subtracting 10 repeatedly or using modulo.
// But modulo on negative numbers in C is implementation-defined before C99,
// and in C99/C11 it truncates towards zero.
// -2147483648 % 10 = -8.
// So we can use modulo and handle the sign.
// Let's stick to the standard logic:
// If value is INT_MIN, we can't do -value.
// We can do: value = value + 1; is_negative = 1; then process, then add 1 to the last digit.
// But that's messy.
// Better approach:
// Use a temporary buffer or just handle the digits carefully.
// Since we are writing to buf, we can just do:
// if (value == INT_MIN) {
// // -2147483648
// // We can't negate it.
// // Let's just subtract 10 repeatedly? No, too slow.
// // Let's use the property that -2147483648 % 10 = -8.
// // We can collect digits as negative numbers and then convert to positive chars.
// }
// Standard robust way:
// If value is INT_MIN, we can't negate it.
// We can do: value = value + 1; is_negative = 1; then process, then add 1 to the last digit.
// But let's try a different way:
// Just use the fact that we can handle negative numbers by taking modulo.
// In C11, -2147483648 % 10 is -8.
// So we can do:
// while (value != 0) {
// int digit = value % 10;
// value /= 10;
// *p++ = '0' + (digit < 0 ? -digit : digit);
// }
// But wait, -2147483648 / 10 is -214748364.
// -214748364 % 10 is -4.
// This works!
// The only issue is if value is 0, we need to handle it.
// Let's implement the general case:
// If value is INT_MIN, we can't do -value, but we can do value % 10 and value / 10.
// The only problem is if value is 0, we need to output "0".
// So:
// if (value == 0) {
// *p++ = '0';
// *p = '\0';
// return;
// }
// if (value < 0) {
// is_negative = 1;
// // We can't do -value if value is INT_MIN.
// // But we can process digits as negative.
// }
// Let's rewrite the loop to handle negative digits correctly.
// We will collect digits in reverse order.
// If value is INT_MIN, we can't negate it, but we can still do modulo and division.
// The digits will be negative.
// We can convert them to positive by taking absolute value.
// But abs(INT_MIN) is undefined behavior.
// So we must avoid abs(INT_MIN).
// Instead, we can do: digit = value % 10; value /= 10;
// Then digit is negative.
// We can convert digit to positive by: digit = -digit;
// But if digit is -```c
#include <limits.h>
#include <stddef.h>
void itoa(int value, char *buf) {
char *p = buf;
int is_negative = 0;
if (value == INT_MIN) {
// Handle INT_MIN specially to avoid overflow when negating
// INT_MIN = -2147483648
// We can't do -value, so we subtract 1 and negate, then add 1 at the end
// Or simpler: manually handle the digits.
// Let's use a safe approach: subtract 1, negate, then add 1 to the last digit.
// Actually, simpler: just process digits by subtracting 10 repeatedly or using modulo.
// But modulo on negative numbers in C is implementation-defined before C99,
// and in C99/C11 it truncates towards zero.
// -2147483648 % 10 = -8.
// So we can use modulo and handle the sign.
// Let's stick to the standard logic:
// If value is INT_MIN, we can't do -value.
// We can do: value = value + 1; is_negative = 1; then process, then add 1 to the last digit.
// But that's messy.
// Better approach:
// Use a temporary buffer or just handle the digits carefully.
// Since we are writing to buf, we can just do:
// if (value == INT_MIN) {
// // -2147483648
// // We can't negate it.
// // Let's just subtract 10 repeatedly? No, too slow.
// // Let's use the property that -2147483648 % 10 = -8.
// // We can collect digits as negative numbers and then convert to positive chars.
// }
// Standard robust way:
// If value is INT_MIN, we can't negate it.
// We can do: value = value + 1; is_negative = 1; then process, then add 1 to the last digit.
// But let's try a different way:
// Just use the fact that we can handle negative numbers by taking modulo.
// In C11, -2147483648 % 10 is -8.
// So we can do:
// while (value != 0) {
// int digit = value % 10;
// value /= 10;
// *p++ = '0' + (digit < 0 ? -digit : digit);
// }
// But wait, -2147483648 / 10 is -214748364.
// -214748364 % 10 is -4.
// This works!
// The only issue is if value is 0, we need to handle it.
// Let's implement the general case:
// If value is INT_MIN, we can't do -value, but we can do value % 10 and value / 10.
// The only problem is if value is 0, we need to output "0".
// So:
// if (value == 0) {
// *p++ = '0';
// *p = '\0';
// return;
// }
// if (value < 0) {
// is_negative = 1;
// // We can't do -value if value is INT_MIN.
// // But we can process digits as negative.
// }
// Let's rewrite the loop to handle negative digits correctly.
// We will collect digits in reverse order.
// If value is INT_MIN, we can't negate it, but we can still do modulo and division.
// The digits will be negative.
// We can convert them to positive by taking absolute value.
// But abs(INT_MIN) is undefined behavior.
// So we must avoid abs(INT_MIN).
// Instead, we can do: digit = value % 10; value /= 10;
// Then digit is negative.
// We can convert digit to positive by: digit = -digit;
// But if digit is -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 val = 0;
int sign = 1;
int in_number = 0;
while (*p && count < max) {
if (isdigit((unsigned char)*p)) {
if (!in_number) {
in_number = 1;
val = 0;
sign = 1;
}
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++] = 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 val = 0;
int sign = 1;
int in_number = 0;
while (*p && count < max) {
if (isdigit((unsigned char)*p)) {
if (!in_number) {
in_number = 1;
val = 0;
sign = 1;
}
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++] = 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 val = 0;
int sign = 1;
int in_number = 0;
while (*p && count < max) {
if (isdigit((unsigned char)*p)) {
if (!in_number) {
in_number = 1;
val = 0;
sign = 1;
}
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++] = 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 <stddef.h>
int cmp_desc(const void *a, const void *b) {
const int *ia = (const int *)a;
const int *ib = (const int *)b;
if (*ia > *ib) return 1;
if (*ia < *ib) return -1;
return 0;
}#include <stddef.h>
int cmp_desc(const void *a, const void *b) {
const int *ia = (const int *)a;
const int *ib = (const int *)b;
if (*ia > *ib) return 1;
if (*ia < *ib) return -1;
return 0;
}#include <stddef.h>
int cmp_desc(const void *a, const void *b) {
const int *ia = (const int *)a;
const int *ib = (const int *)b;
if (*ia > *ib) return 1;
if (*ia < *ib) return -1;
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';
}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 == 0) return -1;
const char *p = line;
const char *field_start = NULL;
int current_idx = 0;
int in_quotes = 0;
int field_len = 0;
int i = 0;
while (*p) {
if (current_idx == idx) {
field_start = p;
field_len = 0;
}
if (current_idx == idx && *p == '"') {
if (in_quotes) {
if (*(p + 1) == '"') {
p++;
field_len++;
} else {
in_quotes = 0;
}
} else {
in_quotes = 1;
}
} else if (current_idx == idx && *p == ',' && !in_quotes) {
break;
} else if (current_idx == idx) {
field_len++;
}
if (current_idx == idx && *p == ',' && !in_quotes) {
break;
}
if (*p == ',' && !in_quotes) {
current_idx++;
if (current_idx > idx) break;
}
p++;
}
if (current_idx != idx) {
return -1;
}
if (field_start == NULL) {
return -1;
}
size_t copy_len = (size_t)field_len;
if (copy_len >= cap) {
copy_len = cap - 1;
}
const char *src = field_start;
char *dst = out;
int j = 0;
int k = 0;
while (k < copy_len && *src) {
if (*src == '"') {
if (*(src + 1) == '"') {
*dst++ = '"';
src++;
k++;
} else {
src++;
}
} else {
*dst++ = *src;
k++;
}
src++;
}
*dst = '\0';
return (int)copy_len;
}#include <stddef.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;
const char *field_start = NULL;
int current_idx = 0;
int in_quotes = 0;
int field_len = 0;
int i = 0;
while (*p) {
if (current_idx == idx) {
field_start = p;
field_len = 0;
}
if (current_idx == idx && *p == '"') {
if (in_quotes) {
if (*(p + 1) == '"') {
p++;
field_len++;
} else {
in_quotes = 0;
}
} else {
in_quotes = 1;
}
} else if (current_idx == idx && *p == ',' && !in_quotes) {
break;
} else if (current_idx == idx) {
field_len++;
}
if (current_idx == idx && *p == ',' && !in_quotes) {
break;
}
if (*p == ',' && !in_quotes) {
current_idx++;
if (current_idx > idx) break;
}
p++;
}
if (current_idx != idx) {
return -1;
}
if (field_start == NULL) {
return -1;
}
size_t copy_len = (size_t)field_len;
if (copy_len >= cap) {
copy_len = cap - 1;
}
const char *src = field_start;
char *dst = out;
int j = 0;
int k = 0;
while (k < copy_len && *src) {
if (*src == '"') {
if (*(src + 1) == '"') {
*dst++ = '"';
src++;
k++;
} else {
src++;
}
} else {
*dst++ = *src;
k++;
}
src++;
}
*dst = '\0';
return (int)copy_len;
}#include <stddef.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;
const char *field_start = NULL;
int current_idx = 0;
int in_quotes = 0;
int field_len = 0;
int i = 0;
while (*p) {
if (current_idx == idx) {
field_start = p;
field_len = 0;
}
if (current_idx == idx && *p == '"') {
if (in_quotes) {
if (*(p + 1) == '"') {
p++;
field_len++;
} else {
in_quotes = 0;
}
} else {
in_quotes = 1;
}
} else if (current_idx == idx && *p == ',' && !in_quotes) {
break;
} else if (current_idx == idx) {
field_len++;
}
if (current_idx == idx && *p == ',' && !in_quotes) {
break;
}
if (*p == ',' && !in_quotes) {
current_idx++;
if (current_idx > idx) break;
}
p++;
}
if (current_idx != idx) {
return -1;
}
if (field_start == NULL) {
return -1;
}
size_t copy_len = (size_t)field_len;
if (copy_len >= cap) {
copy_len = cap - 1;
}
const char *src = field_start;
char *dst = out;
int j = 0;
int k = 0;
while (k < copy_len && *src) {
if (*src == '"') {
if (*(src + 1) == '"') {
*dst++ = '"';
src++;
k++;
} else {
src++;
}
} else {
*dst++ = *src;
k++;
}
src++;
}
*dst = '\0';
return (int)copy_len;
}Served via llama.cpp CUDA · 67.4 tok/s · 0.9 GB RAM · coding total 96/126
Implement in C11: `int atoi_strict(const char *s, int *out)`. Parses an optionally negative decimal integer from s into out. Returns 0 on success, -1 if s is not exactly one valid integer (empty, junk, trailing chars). No overflow tests. Reply with only a C code block. No main function, no tests, no explanation.
#include <limits.h>
#include <stdbool.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
bool negative = false;
// Check for empty string
if (*p == '\0') {
return -1;
}
// Handle optional sign
if (*p == '-') {
negative = true;
p++;
} else if (*p == '+') {
p++;
}
// Check for empty string after sign
if (*p == '\0') {
return -1;
}
// Parse digits
long result = 0;
while (*p >= '0' && *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>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
int sign = 1;
long value = 0;
int valid = 0;
// Skip leading whitespace
while (*p == ' ') {
p++;
}
// Handle optional sign
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
// Parse digits
while (*p >= '0' && *p <= '9') {
valid = 1;
value = value * 10 + (*p - '0');
p++;
}
// If no digits were found, it's invalid
if (!valid) {
return -1;
}
// Apply sign
value *= sign;
// Check for overflow (since we used long)
if (value < INT_MIN || value > INT_MAX) {
return -1;
}
// Check for trailing characters
while (*p != '\0') {
if (*p != ' ') {
return -1;
}
p++;
}
*out = (int)value;
return 0;
}#include <limits.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
int sign = 1;
long value = 0;
int valid = 0;
// Skip leading whitespace
while (*p == ' ') {
p++;
}
// Handle optional sign
if (*p == '-') {
sign = -1;
p++;
} else if (*p == '+') {
p++;
}
// Parse digits
while (*p >= '0' && *p <= '9') {
valid = 1;
value = value * 10 + (*p - '0');
p++;
}
// If no digits were found, it's invalid
if (!valid) {
return -1;
}
// Apply sign
value *= sign;
// Check for overflow (since we used long)
if (value < INT_MIN || value > INT_MAX) {
return -1;
}
// Check for trailing characters
while (*p != '\0') {
if (*p != ' ') {
return -1;
}
p++;
}
*out = (int)value;
return 0;
}Implement in C11: `int glob_match(const char *pat, const char *str)`. Returns 1 if str matches the shell-style pattern pat, else 0. '*' matches any sequence (including empty), '?' matches exactly one character. All other characters match literally. The whole string must match. Reply with only a C code block. No main function, no tests, no explanation.
#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 *pat, const char *str, const char **next_pat);
static int match_question(const char *pat, const char *str);
int glob_match(const char *pat, const char *str) {
if (*pat == '*') {
return match_star(pat, str, &pat);
}
return match_question(pat, str);
}
static int match_star(const char *pat, const char *str, const char **next_pat) {
const char *s = str;
const char *p = pat;
const char *star = NULL;
const char *match = NULL;
while (*s != '\0') {
if (*p == '*') {
star = p;
match = s;
p++;
} else if (*p == *s || *p == '?') {
p++;
s++;
} else {
if (star != NULL) {
p = star + 1;
match++;
s = match;
} else {
return 0;
}
}
}
while (*p == '*') {
p++;
}
if (*p == '\0') {
*next_pat = p;
return 1;
}
return 0;
}
static int match_question(const char *pat, const char *str) {
if (*pat == '\0' && *str == '\0') {
return 1;
}
if (*pat == '?' && *str != '\0') {
return match_question(pat + 1, str + 1);
}
if (*pat == *str) {
return match_question(pat + 1, str + 1);
}
return 0;
}#include <stddef.h>
static int match_star(const char *pat, const char *str, const char **next_pat);
static int match_question(const char *pat, const char *str);
int glob_match(const char *pat, const char *str) {
if (*pat == '*') {
return match_star(pat, str, &pat);
}
return match_question(pat, str);
}
static int match_star(const char *pat, const char *str, const char **next_pat) {
const char *s = str;
const char *p = pat;
const char *star = NULL;
const char *match = NULL;
while (*s != '\0') {
if (*p == '*') {
star = p;
match = s;
p++;
} else if (*p == *s || *p == '?') {
p++;
s++;
} else {
if (star != NULL) {
p = star + 1;
match++;
s = match;
} else {
return 0;
}
}
}
while (*p == '*') {
p++;
}
if (*p == '\0') {
*next_pat = p;
return 1;
}
return 0;
}
static int match_question(const char *pat, const char *str) {
if (*pat == '\0' && *str == '\0') {
return 1;
}
if (*pat == '?' && *str != '\0') {
return match_question(pat + 1, str + 1);
}
if (*pat == *str) {
return match_question(pat + 1, str + 1);
}
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 <math.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_STACK_SIZE 256
typedef struct {
double data[MAX_STACK_SIZE];
int top;
} Stack;
void stack_init(Stack *s) {
s->top = -1;
}
int stack_push(Stack *s, double val) {
if (s->top >= MAX_STACK_SIZE - 1) return 0;
s->data[++(s->top)] = val;
return 1;
}
int stack_pop(Stack *s, double *val) {
if (s->top < 0) return 0;
*val = s->data[(s->top)--];
return 1;
}
int stack_is_empty(Stack *s) {
return s->top < 0;
}
double rpn_eval(const char *expr) {
Stack stack;
stack_init(&stack);
const char *p = expr;
char *endptr;
char token[32];
while (*p != '\0') {
// Skip whitespace
while (isspace((unsigned char)*p)) p++;
if (*p == '\0') break;
// Check for operator
int is_operator = 0;
if (*p == '+' || *p == '-' || *p == '*' || *p == '/') {
is_operator = 1;
token[0] = *p;
token[1] = '\0';
p++;
} else {
// Parse number
char *t = token;
while (*p != '\0' && !isspace((unsigned char)*p)) {
*t++ = *p++;
}
*t = '\0';
}
if (is_operator) {
double b, a;
if (stack_pop(&stack, &b) && stack_pop(&stack, &a)) {
switch (token[0]) {
case '+': stack_push(&stack, a + b); break;
case '-': stack_push(&stack, a - b); break;
case '*': stack_push(&stack, a * b); break;
case '/': stack_push(&stack, a / b); break;
}
} else {
// Stack underflow (invalid expression)
return NAN;
}
} else {
// Parse number to double
errno = 0;
double val = strtod(token, &endptr);
if (endptr == token || errno == ERANGE) {
return NAN;
}
stack_push(&stack, val);
}
}
double result;
if (stack_pop(&stack, &result) && stack_is_empty(&stack)) {
return result;
} else {
return NAN;
}
}#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_STACK_SIZE 256
typedef struct {
double data[MAX_STACK_SIZE];
int top;
} Stack;
void stack_init(Stack *s) {
s->top = -1;
}
int stack_push(Stack *s, double val) {
if (s->top >= MAX_STACK_SIZE - 1) return 0;
s->data[++(s->top)] = val;
return 1;
}
int stack_pop(Stack *s, double *val) {
if (s->top < 0) return 0;
*val = s->data[(s->top)--];
return 1;
}
int stack_is_empty(Stack *s) {
return s->top < 0;
}
double rpn_eval(const char *expr) {
Stack stack;
stack_init(&stack);
const char *p = expr;
char *endptr;
char token[32];
while (*p != '\0') {
// Skip whitespace
while (isspace((unsigned char)*p)) p++;
if (*p == '\0') break;
// Check for operator
int is_operator = 0;
if (*p == '+' || *p == '-' || *p == '*' || *p == '/') {
is_operator = 1;
token[0] = *p;
token[1] = '\0';
p++;
} else {
// Parse number
char *t = token;
while (*p != '\0' && !isspace((unsigned char)*p)) {
*t++ = *p++;
}
*t = '\0';
}
if (is_operator) {
double b, a;
if (stack_pop(&stack, &b) && stack_pop(&stack, &a)) {
switch (token[0]) {
case '+': stack_push(&stack, a + b); break;
case '-': stack_push(&stack, a - b); break;
case '*': stack_push(&stack, a * b); break;
case '/': stack_push(&stack, a / b); break;
}
} else {
// Stack underflow (invalid expression)
return NAN;
}
} else {
// Parse number to double
errno = 0;
double val = strtod(token, &endptr);
if (endptr == token || errno == ERANGE) {
return NAN;
}
stack_push(&stack, val);
}
}
double result;
if (stack_pop(&stack, &result) && stack_is_empty(&stack)) {
return result;
} else {
return NAN;
}
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
#include <stddef.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
const char *p = line;
const char *start = NULL;
const char *end = NULL;
int current = 0;
int in_quotes = 0;
size_t out_len = 0;
if (idx < 0) {
return -1;
}
while (*p != '\0') {
if (current == idx) {
if (in_quotes) {
start = p;
} else {
start = p;
while (*p != ',' && *p != '\0') {
p++;
}
end = p;
break;
}
}
if (*p == '"') {
if (in_quotes) {
if (*(p + 1) == '"') {
p++;
} else {
in_quotes = 0;
}
} else {
in_quotes = 1;
}
} else if (*p == ',' && !in_quotes) {
current++;
}
p++;
}
if (current != idx) {
return -1;
}
if (in_quotes) {
return -1;
}
if (start == NULL) {
return -1;
}
if (end == NULL) {
end = p;
}
size_t copy_len = end - start;
if (copy_len > cap - 1) {
copy_len = cap - 1;
}
memcpy(out, start, copy_len);
out[copy_len] = '\0';
return (int)copy_len;
}#include <stddef.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
const char *p = line;
const char *start = NULL;
const char *end = NULL;
int current = 0;
int in_quotes = 0;
size_t out_len = 0;
if (idx < 0) {
return -1;
}
while (*p != '\0') {
if (current == idx) {
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 CUDA · 11.8 tok/s · 3.5 GB RAM · coding total 96/126
Implement in C11: `void reverse_string(char *s)`. Reverses s in place. Reply with only a C code block. No main function, no tests, no explanation.
#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: `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) {
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;
}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;
}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 = 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;
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;
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;
s = ++match;
} else {
return 0;
}
}
}
while (*p == '*') {
p++;
}
return *p == '\0';
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
#include <stddef.h>
#include <stdbool.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int current_idx = 0;
size_t out_len = 0;
bool in_quotes = false;
bool field_started = false;
while (*p != '\0') {
char c = *p;
if (!in_quotes) {
if (c == '"') {
in_quotes = true;
field_started = true;
p++;
continue;
} else if (c == ',') {
if (field_started) {
current_idx++;
field_started = false;
}
if (current_idx == idx) {
// Found the target field, but it's empty
if (cap > 0) out[0] = '\0';
return 0;
}
p++;
continue;
} else {
field_started = true;
}
}
if (current_idx == idx) {
if (in_quotes) {
if (c == '"') {
// Check for escaped quote ""
if (*(p + 1) == '"') {
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = false;
p++;
continue;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = c;
}
p++;
continue;
}
} else {
if (c == ',') {
// End of unquoted field
break;
}
if (out_len < cap - 1) {
out[out_len++] = c;
}
p++;
continue;
}
} else {
// Skipping fields before the target index
if (in_quotes) {
if (c == '"') {
if (*(p + 1) == '"') {
p += 2;
} else {
in_quotes = false;
p++;
}
} else {
p++;
}
} else {
if (c == ',') {
current_idx++;
field_started = false;
p++;
} else {
p++;
}
}
}
}
// Handle end of string
if (current_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
return -1;
}#include <stddef.h>
#include <stdbool.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int current_idx = 0;
size_t out_len = 0;
bool in_quotes = false;
bool field_started = false;
while (*p != '\0') {
char c = *p;
if (!in_quotes) {
if (c == '"') {
in_quotes = true;
field_started = true;
p++;
continue;
} else if (c == ',') {
if (field_started) {
current_idx++;
field_started = false;
}
if (current_idx == idx) {
// Found the target field, but it's empty
if (cap > 0) out[0] = '\0';
return 0;
}
p++;
continue;
} else {
field_started = true;
}
}
if (current_idx == idx) {
if (in_quotes) {
if (c == '"') {
// Check for escaped quote ""
if (*(p + 1) == '"') {
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = false;
p++;
continue;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = c;
}
p++;
continue;
}
} else {
if (c == ',') {
// End of unquoted field
break;
}
if (out_len < cap - 1) {
out[out_len++] = c;
}
p++;
continue;
}
} else {
// Skipping fields before the target index
if (in_quotes) {
if (c == '"') {
if (*(p + 1) == '"') {
p += 2;
} else {
in_quotes = false;
p++;
}
} else {
p++;
}
} else {
if (c == ',') {
current_idx++;
field_started = false;
p++;
} else {
p++;
}
}
}
}
// Handle end of string
if (current_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
return -1;
}#include <stddef.h>
#include <stdbool.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) {
return -1;
}
const char *p = line;
int current_idx = 0;
size_t out_len = 0;
bool in_quotes = false;
bool field_started = false;
while (*p != '\0') {
char c = *p;
if (!in_quotes) {
if (c == '"') {
in_quotes = true;
field_started = true;
p++;
continue;
} else if (c == ',') {
if (field_started) {
current_idx++;
field_started = false;
}
if (current_idx == idx) {
// Found the target field, but it's empty
if (cap > 0) out[0] = '\0';
return 0;
}
p++;
continue;
} else {
field_started = true;
}
}
if (current_idx == idx) {
if (in_quotes) {
if (c == '"') {
// Check for escaped quote ""
if (*(p + 1) == '"') {
if (out_len < cap - 1) {
out[out_len++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = false;
p++;
continue;
}
} else {
if (out_len < cap - 1) {
out[out_len++] = c;
}
p++;
continue;
}
} else {
if (c == ',') {
// End of unquoted field
break;
}
if (out_len < cap - 1) {
out[out_len++] = c;
}
p++;
continue;
}
} else {
// Skipping fields before the target index
if (in_quotes) {
if (c == '"') {
if (*(p + 1) == '"') {
p += 2;
} else {
in_quotes = false;
p++;
}
} else {
p++;
}
} else {
if (c == ',') {
current_idx++;
field_started = false;
p++;
} else {
p++;
}
}
}
}
// Handle end of string
if (current_idx == idx) {
out[out_len] = '\0';
return (int)out_len;
}
return -1;
}Served via llama.cpp CUDA · 22.3 tok/s · 3.7 GB RAM · coding total 93/126
Implement in C11: `int count_words(const char *s)`. Counts words in s. Words are separated by one or more spaces; leading/trailing spaces possible. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
int count_words(const char *s) {
if (s == NULL) {
return 0;
}
int count = 0;
int in_word = 0;
while (*s != '\0') {
if (isspace((unsigned char)*s)) {
in_word = 0;
} else {
if (!in_word) {
count++;
in_word = 1;
}
}
s++;
}
return count;
}#include <ctype.h>
int count_words(const char *s) {
if (s == NULL) {
return 0;
}
int count = 0;
int in_word = 0;
while (*s != '\0') {
if (isspace((unsigned char)*s)) {
in_word = 0;
} else {
if (!in_word) {
count++;
in_word = 1;
}
}
s++;
}
return count;
}#include <ctype.h>
int count_words(const char *s) {
if (s == NULL) {
return 0;
}
int count = 0;
int in_word = 0;
while (*s != '\0') {
if (isspace((unsigned char)*s)) {
in_word = 0;
} else {
if (!in_word) {
count++;
in_word = 1;
}
}
s++;
}
return count;
}Implement in C11: `void rot13(char *s)`. Applies ROT13 to s in place: letters rotate by 13, all other characters unchanged. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
void rot13(char *s) {
if (s == NULL) return;
for (; *s != '\0'; ++s) {
if (isalpha((unsigned char)*s)) {
char base = isupper((unsigned char)*s) ? 'A' : 'a';
*s = (char)(((*s - base + 13) % 26) + base);
}
}
}#include <ctype.h>
void rot13(char *s) {
if (s == NULL) return;
for (; *s != '\0'; ++s) {
if (isalpha((unsigned char)*s)) {
char base = isupper((unsigned char)*s) ? 'A' : 'a';
*s = (char)(((*s - base + 13) % 26) + base);
}
}
}#include <ctype.h>
void rot13(char *s) {
if (s == NULL) return;
for (; *s != '\0'; ++s) {
if (isalpha((unsigned char)*s)) {
char base = isupper((unsigned char)*s) ? 'A' : 'a';
*s = (char)(((*s - base + 13) % 26) + base);
}
}
}Implement in C11: `void trim(char *s)`. Strips leading and trailing whitespace (space, tab, newline) from s in place. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
void trim(char *s) {
if (s == NULL) return;
char *start = s;
char *end;
while (*start && isspace((unsigned char)*start)) {
start++;
}
if (*start == '\0') {
s[0] = '\0';
return;
}
end = start + strlen(start) - 1;
while (end > start && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
if (start != s) {
char *dst = s;
while (start <= end) {
*dst++ = *start++;
}
*dst = '\0';
}
}#include <ctype.h>
void trim(char *s) {
if (s == NULL) return;
char *start = s;
char *end;
while (*start && isspace((unsigned char)*start)) {
start++;
}
if (*start == '\0') {
s[0] = '\0';
return;
}
end = start + strlen(start) - 1;
while (end > start && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
if (start != s) {
char *dst = s;
while (start <= end) {
*dst++ = *start++;
}
*dst = '\0';
}
}#include <ctype.h>
void trim(char *s) {
if (s == NULL) return;
char *start = s;
char *end;
while (*start && isspace((unsigned char)*start)) {
start++;
}
if (*start == '\0') {
s[0] = '\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 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 <stdint.h>
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;
if (val_a < val_b) return -1;
return 0;
}#include <stdint.h>
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;
if (val_a < val_b) return -1;
return 0;
}#include <stdint.h>
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;
if (val_a < val_b) 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;
int sign = 1;
int has_digit = 0;
int result = 0;
// Skip leading whitespace
while (*p != '\0' && 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++;
}
// Parse digits
while (*p != '\0' && isdigit((unsigned char)*p)) {
has_digit = 1;
result = result * 10 + (*p - '0');
p++;
}
// Must have at least one digit
if (!has_digit) {
return -1;
}
// Must end immediately after the number (no trailing junk)
if (*p != '\0') {
return -1;
}
*out = sign * result;
return 0;
}#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
int sign = 1;
int has_digit = 0;
int result = 0;
// Skip leading whitespace
while (*p != '\0' && 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++;
}
// Parse digits
while (*p != '\0' && isdigit((unsigned char)*p)) {
has_digit = 1;
result = result * 10 + (*p - '0');
p++;
}
// Must have at least one digit
if (!has_digit) {
return -1;
}
// Must end immediately after the number (no trailing junk)
if (*p != '\0') {
return -1;
}
*out = sign * result;
return 0;
}#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || out == NULL) {
return -1;
}
const char *p = s;
int sign = 1;
int has_digit = 0;
int result = 0;
// Skip leading whitespace
while (*p != '\0' && 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++;
}
// Parse digits
while (*p != '\0' && isdigit((unsigned char)*p)) {
has_digit = 1;
result = result * 10 + (*p - '0');
p++;
}
// Must have at least one digit
if (!has_digit) {
return -1;
}
// Must end immediately after the number (no trailing junk)
if (*p != '\0') {
return -1;
}
*out = sign * result;
return 0;
}Implement in C11: `int glob_match(const char *pat, const char *str)`. Returns 1 if str matches the shell-style pattern pat, else 0. '*' matches any sequence (including empty), '?' matches exactly one character. All other characters match literally. The whole string must match. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdbool.h>
int glob_match(const char *pat, const char *str) {
const char *star_pat = NULL;
const char *star_str = NULL;
while (*str != '\0') {
if (*pat == '*') {
star_pat = pat;
star_str = str;
pat++;
} else if (*pat == '?' || *pat == *str) {
pat++;
str++;
} else if (star_pat != NULL) {
pat = star_pat + 1;
str = star_str + 1;
star_str = str;
} else {
return 0;
}
}
while (*pat == '*') {
pat++;
}
return (*pat == '\0');
}#include <stdbool.h>
int glob_match(const char *pat, const char *str) {
const char *star_pat = NULL;
const char *star_str = NULL;
while (*str != '\0') {
if (*pat == '*') {
star_pat = pat;
star_str = str;
pat++;
} else if (*pat == '?' || *pat == *str) {
pat++;
str++;
} else if (star_pat != NULL) {
pat = star_pat + 1;
str = star_str + 1;
star_str = str;
} else {
return 0;
}
}
while (*pat == '*') {
pat++;
}
return (*pat == '\0');
}#include <stdbool.h>
int glob_match(const char *pat, const char *str) {
const char *star_pat = NULL;
const char *star_str = NULL;
while (*str != '\0') {
if (*pat == '*') {
star_pat = pat;
star_str = str;
pat++;
} else if (*pat == '?' || *pat == *str) {
pat++;
str++;
} else if (star_pat != NULL) {
pat = star_pat + 1;
str = star_str + 1;
star_str = str;
} else {
return 0;
}
}
while (*pat == '*') {
pat++;
}
return (*pat == '\0');
}Served via llama.cpp CUDA · 68.1 tok/s · 0.9 GB RAM · coding total 90/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 (not null terminator)
// 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 (not null terminator)
// Reverse the string in place
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;
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[0] = '-';
return;
}
int32_t abs_value = negative ? -value : value;
int i = 0;
while (abs_value != 0) {
buf[i++] = '0' + (abs_value % 10);
abs_value /= 10;
}
if (negative) {
buf[i++] = '-';
}
// Reverse the string
for (int j = 0; j < i/2; j++) {
char c = buf[j];
buf[j] = buf[i-1-j];
buf[i-1-j] = c;
}
buf[i] = '\0';
}#include <stdint.h>
#include <stdbool.h>
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
bool negative = value < 0;
if (negative && value == INT_MIN) {
// Special case for INT_MIN to avoid overflow when negating
int32_t temp = value;
int i = 0;
while (temp != 0) {
buf[i++] = '0' - (temp % 10);
temp /= 10;
}
buf[i] = '\0';
// Reverse the string
for (int j = 0; j < i/2; j++) {
char c = buf[j];
buf[j] = buf[i-1-j];
buf[i-1-j] = c;
}
buf[i] = '\0';
return;
}
if (negative) {
value = -value;
}
int i = 0;
while (value > 0) {
buf[i++] = '0' + (value % 10);
value /= 10;
}
if (negative) {
buf[i++] = '-';
}
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;
}
}#include <stdint.h>
#include <stdbool.h>
void itoa(int value, char *buf) {
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
bool negative = value < 0;
if (negative && value == INT_MIN) {
// Special case for INT_MIN to avoid overflow when negating
int32_t temp = value;
int i = 0;
while (temp != 0) {
buf[i++] = '0' - (temp % 10);
temp /= 10;
}
buf[i] = '\0';
// Reverse the string
for (int j = 0; j < i/2; j++) {
char c = buf[j];
buf[j] = buf[i-1-j];
buf[i-1-j] = c;
}
buf[i] = '\0';
return;
}
if (negative) {
value = -value;
}
int i = 0;
while (value > 0) {
buf[i++] = '0' + (value % 10);
value /= 10;
}
if (negative) {
buf[i++] = '-';
}
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;
}
}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 || idx < 0 || !out || cap == 0) return -1;
const char *p = line;
int current_idx = 0;
size_t out_pos = 0;
while (*p) {
// Skip leading whitespace
while (*p == ' ' || *p == '\t') p++;
// Check if we're at the right field
if (current_idx == idx) {
const char *start = p;
int in_quotes = 0;
int escaped = 0;
// Parse the field
while (*p) {
if (escaped) {
escaped = 0;
if (*p == '"' && *(p + 1) == '"') {
// Doubled quote
if (out_pos < cap - 1) out[out_pos++] = '"';
p += 2;
continue;
} else {
// Escaped character (treat as literal)
if (out_pos < cap - 1) out[out_pos++] = *p;
p++;
continue;
}
}
if (*p == '"') {
if (in_quotes) {
// Check if this is a closing quote
if (*(p + 1) == '"') {
// Doubled quote, continue
p++;
} else {
// End of quoted field
in_quotes = 0;
}
} else {
// Start of quoted field
in_quotes = 1;
}
} else if (*p == ',' && !in_quotes) {
// End of field
break;
} else if (*p == '\\' && in_quotes) {
// Escape character in quoted field
escaped = 1;
} else {
// Regular character
if (out_pos < cap - 1) out[out_pos++] = *p;
}
p++;
}
// Null terminate
out[out_pos] = '\0';
return (int)out_pos;
}
// Skip to next field
int in_quotes = 0;
while (*p) {
if (*p == '"') {
if (*(p + 1) == '"') {
p += 2;
continue;
} else {
in_quotes = !in_quotes;
}
} else if (*p == ',' && !in_quotes) {
break;
}
p++;
}
// Skip the comma
if (*p == ',') p++;
current_idx++;
}
return -1;
}#include <string.h>
#include <stdbool.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (!line || idx < 0 || !out || cap == 0) return -1;
const char *p = line;
int current_idx = 0;
bool in_quotes = false;
// Skip to the desired field
while (current_idx < idx) {
if (*p == '"') {
in_quotes = !in_quotes;
p++;
if (in_quotes && *p == '"') {
p++; // Skip the second quote in a quoted field
}
} else if (*p == ',' && !in_quotes) {
current_idx++;
} else if (*p == '\0') {
return -1; // Field not found
} else {
p++;
}
}
// Skip leading commas and whitespace
while (*p == ',' || *p == ' ' || *p == '\t') {
p++;
}
// Check if we're at the end of the line
if (*p == '\0') return -1;
// Extract the field
size_t out_pos = 0;
bool escaped = false;
while (*p != '\0' && (in_quotes || *p != ',')) {
if (escaped) {
if (*p == '"') {
if (out_pos < cap - 1) {
out[out_pos++] = '"';
}
} else {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
}
escaped = false;
} else if (*p == '"') {
if (in_quotes) {
// Check if this is a double quote
if (*(p + 1) == '"') {
if (out_pos < cap - 1) {
out[out_pos++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = false;
}
} else {
// Start of quoted field
in_quotes = true;
}
} else if (*p == '\\') {
escaped = true;
} else {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
}
p++;
}
out[out_pos] = '\0';
return (int)out_pos;
}#include <string.h>
#include <stdbool.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (!line || idx < 0 || !out || cap == 0) return -1;
const char *p = line;
int current_idx = 0;
bool in_quotes = false;
// Skip to the desired field
while (current_idx < idx) {
if (*p == '"') {
in_quotes = !in_quotes;
p++;
if (in_quotes && *p == '"') {
p++; // Skip the second quote in a quoted field
}
} else if (*p == ',' && !in_quotes) {
current_idx++;
} else if (*p == '\0') {
return -1; // Field not found
} else {
p++;
}
}
// Skip leading commas and whitespace
while (*p == ',' || *p == ' ' || *p == '\t') {
p++;
}
// Check if we're at the end of the line
if (*p == '\0') return -1;
// Extract the field
size_t out_pos = 0;
bool escaped = false;
while (*p != '\0' && (in_quotes || *p != ',')) {
if (escaped) {
if (*p == '"') {
if (out_pos < cap - 1) {
out[out_pos++] = '"';
}
} else {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
}
escaped = false;
} else if (*p == '"') {
if (in_quotes) {
// Check if this is a double quote
if (*(p + 1) == '"') {
if (out_pos < cap - 1) {
out[out_pos++] = '"';
}
p += 2;
continue;
} else {
// End of quoted field
in_quotes = false;
}
} else {
// Start of quoted field
in_quotes = true;
}
} else if (*p == '\\') {
escaped = true;
} else {
if (out_pos < cap - 1) {
out[out_pos++] = *p;
}
}
p++;
}
out[out_pos] = '\0';
return (int)out_pos;
}Served via llama.cpp CUDA · 48.3 tok/s · 1.2 GB RAM · coding total 88/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--;
while (s < end) {
char tmp = *s;
*s = *end;
*end = tmp;
s++;
end--;
}
}void reverse_string(char *s) {
if (s == NULL) return;
char *end = s;
while (*end) ++end;
--end;
while (s < end) {
char tmp = *s;
*s++ = *end;
*end-- = tmp;
}
}void reverse_string(char *s) {
if (s == NULL) return;
char *end = s;
while (*end) ++end;
--end;
while (s < end) {
char tmp = *s;
*s++ = *end;
*end-- = tmp;
}
}Implement in C11: `void itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
#include <limits.h>
void itoa(int value, char *buf) {
char *p = buf;
int negative = value < 0;
if (negative) {
*p++ = '-';
if (value == INT_MIN) {
value = INT_MAX;
} else {
value = -value;
}
}
if (value == 0) {
*p++ = '0';
} else {
char *start = p;
while (value > 0) {
*p++ = (char)('0' + (value % 10));
value /= 10;
}
while (p > start) {
*--p = *p;
}
}
*p = '\0';
}#include <limits.h>
void itoa(int value, char *buf) {
char *p = buf;
int negative = value < 0;
if (negative) {
*p++ = '-';
if (value == INT_MIN) {
value = INT_MAX;
} else {
value = -value;
}
}
if (value == 0) {
*p++ = '0';
} else {
char *start = p;
while (value > 0) {
*p++ = (char)('0' + (value % 10));
value /= 10;
}
while (p > start) {
*--p = *p;
}
}
*p = '\0';
}#include <limits.h>
void itoa(int value, char *buf) {
char *p = buf;
int negative = value < 0;
if (negative) {
*p++ = '-';
if (value == INT_MIN) {
value = INT_MAX;
} else {
value = -value;
}
}
if (value == 0) {
*p++ = '0';
} else {
char *start = p;
while (value > 0) {
*p++ = (char)('0' + (value % 10));
value /= 10;
}
while (p > start) {
*--p = *p;
}
}
*p = '\0';
}Implement in C11: `void trim(char *s)`. Strips leading and trailing whitespace (space, tab, newline) from s in place. Reply with only a C code block. No main function, no tests, no explanation.
void trim(char *s) {
char *end;
while (*s == ' ' || *s == '\t' || *s == '\n')
s++;
if (*s == '\0')
*s = '\0';
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';
return;
}
end = s + strlen(s) - 1;
while (end > s && (*end == ' ' || *end == '\t' || *end == '\n'))
*end-- = '\0';
}void trim(char *s) {
char *end;
while (*s == ' ' || *s == '\t' || *s == '\n')
s++;
if (*s == '\0') {
*s = '\0';
return;
}
end = s + strlen(s) - 1;
while (end > s && (*end == ' ' || *end == '\t' || *end == '\n'))
*end-- = '\0';
}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) return -1;
if (idx < 0) return -1;
const char *p = line;
int cur = 0;
while (*p != '\0') {
if (*p == '"') {
/* quoted field */
++p;
size_t len = 0;
while (*p != '\0' && *p != '"') {
if (*p == '"') {
if (p[1] == '"') {
if (len + 1 < cap) out[len++] = '"';
p += 2;
} else {
++p; /* closing quote */
break;
}
} else {
if (len + 1 < cap) out[len++] = *p;
++p;
}
}
/* skip to end of field */
while (*p != '\0' && *p != ',') ++p;
if (*p == ',') ++p;
if (cur == idx) {
if (len + 1 < cap) out[len] = '\0';
else out[cap - 1] = '\0';
return (int)len;
}
++cur;
} else {
/* unquoted field */
const char *start = p;
while (*p != '\0' && *p != ',') ++p;
size_t len = (size_t)(p - start);
if (cur == idx) {
if (len >= cap) len = cap - 1;
for (size_t i = 0; i < len; ++i) out[i] = start[i];
out[len] = '\0';
return (int)len;
}
if (*p == ',') ++p;
++cur;
}
}
return -1;
}```c
#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) return -1;
if (idx < 0) return -1;
const char *p = line;
int field = 0;
size_t len = 0;
while (*p != '\0') {
if (field == idx) break;
if (*p == '"') {
/* quoted field */
p++;
while (*p != '\0' && *p != '"') {
if (*p == '"') {
if (p[1] == '"') {
p += 2;
} else {
p++; /* closing quote */
break;
}
} else {
p++;
}
}
/* skip to end of field */
while (*p != '\0' && *p != ',') p++;
} else {
while (*p != '\0' && *p != ',') p++;
}
if (*p == ',') p++;
field++;
}
if (field != idx) return -1;
/* extract field idx */
p = line;
field = 0;
len = 0;
while (*p != '\0') {
if (field == idx) break;
if (*p == '"') {
p++;
while (*p != '\0' && *p != '"') {
if (*p == '"') {
if (p[1] == '"') {
p += 2;
} else {
p++;
break;
}
} else {
p++;
}
}
while (*p != '\0' && *p != ',') p++;
} else {
while (*p != '\0' && *p != ',') p++;
}
if (*p == ',') p++;
field++;
}
if (field != idx) return -1;
/* copy field */
p = line;
field = 0;
while (*p != '\0') {
if (field == idx) break;
if (*p == '"') {
p++;
while (*p != '\0' && *p != '"') {
if (*p == '"') {
if (p[1] == '"') {
p += 2;
} else {
p++;
break;
}
} else {
p++;
}
}
while (*p != '\0' && *p != ',') p++;
} else {
while (*p != '\0' && *p != ',') p++;
}
if (*p == ',') p++;
field++;
}
if (field != idx) return -1;
/* copy field content */
p = line;
field = 0;
while (*p != '\0') {
if (field == idx) break;
if (*p == '"') {
p++;
while (*p != '\0' && *p != '"') {
if (*p == '"') {
if (p[1] == '"') {
p += 2;
} else {
p++;
break;
}
} else {
p++;
}
}
while (*p != '\0' && *p != ',') p++;
} else {
while (*p != '\0' && *p != ',') p++;
}
if (*p == ',') p++;
field++;
}
if (field != idx) return -1;
/* copy field content */
p = line;
field = 0;
while (*p != '\0') {
if (field == idx) break;
if (*p == '"') {
p++;
while (*p != '\0' && *p != '"') {
if (*p == '"') {
if (p[1] == '"') {
p += 2;
} else {
p++;
break;
}
} else {
p++;
}
}
while (*p != '\0' && *p != ',') p++;
} else {
while (*p != '\0' && *p != ',') p++;
}
if (*p == ',') p++;
field++;
}
if (field != idx) return -1;
/* copy field content */
p = line;
field = 0;
while (*p != '\0') {
if (field == idx) break;
if (*p == '"') {
p++;
while (*p != '\0' && *p != '"') {
if (*p == '"') {
if (p[1```c
#include <stddef.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
if (line == NULL || out == NULL || cap == 0) return -1;
if (idx < 0) return -1;
const char *p = line;
int field = 0;
size_t len = 0;
while (*p != '\0') {
if (field == idx) break;
if (*p == '"') {
/* quoted field */
p++;
while (*p != '\0' && *p != '"') {
if (*p == '"') {
if (p[1] == '"') {
p += 2;
} else {
p++; /* closing quote */
break;
}
} else {
p++;
}
}
/* skip to end of field */
while (*p != '\0' && *p != ',') p++;
} else {
while (*p != '\0' && *p != ',') p++;
}
if (*p == ',') p++;
field++;
}
if (field != idx) return -1;
/* extract field idx */
p = line;
field = 0;
len = 0;
while (*p != '\0') {
if (field == idx) break;
if (*p == '"') {
p++;
while (*p != '\0' && *p != '"') {
if (*p == '"') {
if (p[1] == '"') {
p += 2;
} else {
p++;
break;
}
} else {
p++;
}
}
while (*p != '\0' && *p != ',') p++;
} else {
while (*p != '\0' && *p != ',') p++;
}
if (*p == ',') p++;
field++;
}
if (field != idx) return -1;
/* copy field */
p = line;
field = 0;
while (*p != '\0') {
if (field == idx) break;
if (*p == '"') {
p++;
while (*p != '\0' && *p != '"') {
if (*p == '"') {
if (p[1] == '"') {
p += 2;
} else {
p++;
break;
}
} else {
p++;
}
}
while (*p != '\0' && *p != ',') p++;
} else {
while (*p != '\0' && *p != ',') p++;
}
if (*p == ',') p++;
field++;
}
if (field != idx) return -1;
/* copy field content */
p = line;
field = 0;
while (*p != '\0') {
if (field == idx) break;
if (*p == '"') {
p++;
while (*p != '\0' && *p != '"') {
if (*p == '"') {
if (p[1] == '"') {
p += 2;
} else {
p++;
break;
}
} else {
p++;
}
}
while (*p != '\0' && *p != ',') p++;
} else {
while (*p != '\0' && *p != ',') p++;
}
if (*p == ',') p++;
field++;
}
if (field != idx) return -1;
/* copy field content */
p = line;
field = 0;
while (*p != '\0') {
if (field == idx) break;
if (*p == '"') {
p++;
while (*p != '\0' && *p != '"') {
if (*p == '"') {
if (p[1] == '"') {
p += 2;
} else {
p++;
break;
}
} else {
p++;
}
}
while (*p != '\0' && *p != ',') p++;
} else {
while (*p != '\0' && *p != ',') p++;
}
if (*p == ',') p++;
field++;
}
if (field != idx) return -1;
/* copy field content */
p = line;
field = 0;
while (*p != '\0') {
if (field == idx) break;
if (*p == '"') {
p++;
while (*p != '\0' && *p != '"') {
if (*p == '"') {
if (p[1Served via llama.cpp CUDA · 36.1 tok/s · 0.9 GB RAM · coding total 83/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 num = 0;
while (*s && count < max) {
if (*s == '-') {
negative = true;
s++;
} else if (*s == ',') {
if (negative) num = -num;
out[count++] = num;
num = 0;
negative = false;
s++;
} else {
num = num * 10 + (*s - '0');
s++;
}
}
if (count < max) {
if (negative) num = -num;
out[count++] = num;
}
return count;
}#include <ctype.h>
#include <stdbool.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
bool negative = false;
int num = 0;
while (*s && count < max) {
if (*s == '-') {
negative = true;
s++;
} else if (*s == ',') {
if (negative) num = -num;
out[count++] = num;
num = 0;
negative = false;
s++;
} else {
num = num * 10 + (*s - '0');
s++;
}
}
if (count < max) {
if (negative) num = -num;
out[count++] = num;
}
return count;
}#include <ctype.h>
#include <stdbool.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
bool negative = false;
int num = 0;
while (*s && count < max) {
if (*s == '-') {
negative = true;
s++;
} else if (*s == ',') {
if (negative) num = -num;
out[count++] = num;
num = 0;
negative = false;
s++;
} else {
num = num * 10 + (*s - '0');
s++;
}
}
if (count < max) {
if (negative) num = -num;
out[count++] = num;
}
return count;
}Implement in C11: `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>
uint32_t bswap32(uint32_t x) {
return ((x >> 24) & 0x000000FF) |
((x >> 8) & 0x0000FF00) |
((x << 8) & 0x0000FF00) |
((x << 24) & 0xFF000000);
}Implement in C11: `void trim(char *s)`. Strips leading and trailing whitespace (space, tab, newline) from s in place. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
#include <string.h>
void trim(char *s) {
if (s == NULL) return;
char *start = s;
char *end = s + strlen(s);
while (start < end && isspace((unsigned char)*start)) {
start++;
}
while (end > start && isspace((unsigned char)*end)) {
end--;
}
if (start < end) {
size_t len = end - start + 1;
memmove(s, start, len);
s[len] = '\0';
} else {
s[0] = '\0';
}
}#include <ctype.h>
#include <string.h>
void trim(char *s) {
if (s == NULL) return;
char *start = s;
char *end = s + strlen(s);
while (start < end && isspace((unsigned char)*start)) {
start++;
}
while (end > start && isspace((unsigned char)*end)) {
end--;
}
if (start < end) {
size_t len = end - start + 1;
memmove(s, start, len);
s[len] = '\0';
} else {
s[0] = '\0';
}
}Implement in C11: `int atoi_strict(const char *s, int *out)`. Parses an optionally negative decimal integer from s into out. Returns 0 on success, -1 if s is not exactly one valid integer (empty, junk, trailing chars). No overflow tests. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || *s == '\0') {
return -1;
}
int sign = 1;
if (*s == '-') {
sign = -1;
s++;
} else if (*s == '+') {
s++;
}
int value = 0;
while (*s != '\0') {
if (!isdigit((unsigned char)*s)) {
return -1;
}
value = value * 10 + (*s - '0');
s++;
}
*out = sign * value;
return 0;
}#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || *s == '\0') {
return -1;
}
int sign = 1;
if (*s == '-') {
sign = -1;
s++;
} else if (*s == '+') {
s++;
}
int value = 0;
while (*s != '\0') {
if (!isdigit((unsigned char)*s)) {
return -1;
}
value = value * 10 + (*s - '0');
s++;
}
*out = sign * value;
return 0;
}#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || *s == '\0') {
return -1;
}
int sign = 1;
if (*s == '-') {
sign = -1;
s++;
} else if (*s == '+') {
s++;
}
int value = 0;
while (*s != '\0') {
if (!isdigit((unsigned char)*s)) {
return -1;
}
value = value * 10 + (*s - '0');
s++;
}
*out = sign * value;
return 0;
}Implement in C11: `int 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) {
while (*str != '\0') {
if (*pat == '\0' || (*pat != '*' && *pat != '?' && *pat != *str)) {
if (*pat == '*') {
if (*(pat + 1) == '\0') return 1;
for (const char *tmp = str; *tmp != '\0'; ++tmp) {
if (glob_match(pat + 1, tmp)) return 1;
}
return 0;
}
if (*pat == '?' || *pat == *str) {
++pat;
++str;
} else {
return 0;
}
} 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>
#include <ctype.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
const char *p = line;
int field_idx = 0;
int in_quotes = 0;
size_t out_idx = 0;
char c;
while (*p) {
if (in_quotes) {
if (*p == '"') {
if (p[1] == '"') {
if (out_idx < cap - 1) {
out[out_idx++] = '"';
}
p += 2;
} else {
in_quotes = 0;
p++;
}
} else {
if (out_idx < cap - 1) {
out[out_idx++] = *p++;
} else {
p++;
}
}
} else {
if (*p == '"') {
in_quotes = 1;
p++;
} else if (*p == ',') {
if (field_idx == idx) {
if (out_idx < cap) {
out[out_idx] = '\0';
} else {
out[cap - 1] = '\0';
}
return (int)out_idx;
}
field_idx++;
p++;
out_idx = 0;
} else {
if (field_idx == idx) {
if (out_idx < cap - 1) {
out[out_idx++] = *p;
}
p++;
} else {
p++;
}
}
}
if (*p == '\0' && field_idx == idx) {
if (out_idx < cap) {
out[out_idx] = '\0';
} else {
out[cap - 1] = '\0';
}
return (int)out_idx;
}
}
return -1;
}#include <stddef.h>
#include <ctype.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
const char *p = line;
int field_idx = 0;
int in_quotes = 0;
size_t out_idx = 0;
char c;
while (*p != '\0' && *p != '\n' && *p != '\r') {
if (field_idx == idx) {
if (in_quotes) {
p++;
while (*p != '\0' && *p != '\n' && *p != '\r') {
if (*p == '"') {
if (*(p + 1) == '"') {
if (out_idx < cap - 1) out[out_idx++] = '"';
p += 2;
} else {
in_quotes = 0;
p++;
}
} else {
if (out_idx < cap - 1) out[out_idx++] = *p++;
else {
p++;
break;
}
}
}
} else {
while (*p != '\0' && *p != '\n' && *p != '\r' && *p != ',') {
if (out_idx < cap - 1) out[out_idx++] = *p++;
else {
p++;
break;
}
}
}
if (out_idx < cap) out[out_idx] = '\0';
return (int)out_idx;
}
if (*p == '"') {
in_quotes = !in_quotes;
p++;
} else {
while (*p != '\0' && *p != '\n' && *p != '\r' && *p != ',') {
p++;
}
if (*p == ',') {
field_idx++;
p++;
}
}
}
return -1;
}#include <stddef.h>
#include <ctype.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
const char *p = line;
int field_idx = 0;
int in_quotes = 0;
size_t out_idx = 0;
char c;
while (*p != '\0' && *p != '\n' && *p != '\r') {
if (field_idx == idx) {
if (in_quotes) {
p++;
while (*p != '\0' && *p != '\n' && *p != '\r') {
if (*p == '"') {
if (*(p + 1) == '"') {
if (out_idx < cap - 1) out[out_idx++] = '"';
p += 2;
} else {
in_quotes = 0;
p++;
}
} else {
if (out_idx < cap - 1) out[out_idx++] = *p++;
else {
p++;
break;
}
}
}
} else {
while (*p != '\0' && *p != '\n' && *p != '\r' && *p != ',') {
if (out_idx < cap - 1) out[out_idx++] = *p++;
else {
p++;
break;
}
}
}
if (out_idx < cap) out[out_idx] = '\0';
return (int)out_idx;
}
if (*p == '"') {
in_quotes = !in_quotes;
p++;
} else {
while (*p != '\0' && *p != '\n' && *p != '\r' && *p != ',') {
p++;
}
if (*p == ',') {
field_idx++;
p++;
}
}
}
return -1;
}Served via llama.cpp CUDA · 8.9 tok/s · 1.4 GB RAM · coding total 81/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++;
if (end == s) return;
end--;
for (char *start = s; start < end; start++, end--) {
char temp = *start;
*start = *end;
*end = temp;
}
}void reverse_string(char *s) {
if (s == NULL) {
return;
}
char *end = s;
while (*end != '\0') {
end++;
}
if (end == s) {
return;
}
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++;
}
if (end == s) {
return;
}
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.
(no code extracted)int count_words(const char *s) {
if (s == NULL) {
return 0;
}
int word_count = 0;
int in_word = 0;
while (*s != '\0') {
if (*s == ' ') {
if (in_word) {
word_count++;
in_word = 0;
}
} else {
if (!in_word) {
in_word = 1;
}
}
s++;
}
if (in_word) {
word_count++;
}
return word_count;
}int count_words(const char *s) {
if (s == NULL) {
return 0;
}
int word_count = 0;
int in_word = 0;
while (*s != '\0') {
if (*s == ' ') {
if (in_word) {
word_count++;
in_word = 0;
}
} else {
if (!in_word) {
in_word = 1;
}
}
s++;
}
if (in_word) {
word_count++;
}
return word_count;
}Implement in C11: `void itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
(no code extracted)(no code extracted)(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;
const char *current = s;
while (current != '\0' && count < max) {
const char *end = current;
while (*end != ',' && *end != '\0') {
end++;
}
char *endptr;
int num = strtoi(current, &endptr, 10);
out[count++] = num;
current = end;
if (*current == ',') {
current++;
}
}
return count;
}#include <stdlib.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
const char *current = s;
while (current != '\0' && count < max) {
const char *end = current;
while (*end != ',' && *end != '\0') {
end++;
}
char *endptr;
int num = strtoi(current, &endptr, 10);
out[count++] = num;
current = end;
if (*current == ',') {
current++;
}
}
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++;
char *end = s;
while (*end) end++;
end--;
while (end > start && (*end == ' ' || *end == '\t' || *end == '\n'))
end--;
if (start > end) {
*s = '\0';
} else {
char *dst = s;
while (dst <= end)
*dst++ = *start++;
*dst = '\0';
}
}Implement in C11: `int cmp_desc(const void *a, const void *b)`. qsort comparator ordering int values descending. Must be safe against overflow. Reply with only a C code block. No main function, no tests, no explanation.
(no code extracted)(no code extracted)(no code extracted)Implement in C11: `int atoi_strict(const char *s, int *out)`. Parses an optionally negative decimal integer from s into out. Returns 0 on success, -1 if s is not exactly one valid integer (empty, junk, trailing chars). No overflow tests. Reply with only a C code block. No main function, no tests, no explanation.
#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || *s == '\0') {
return -1;
}
int sign = 1;
const char *current = s;
if (*current == '+' || *current == '-') {
sign = (*current == '-') ? -1 : 1;
current++;
if (*current == '\0') {
return -1;
}
}
if (!isdigit((unsigned char)*current)) {
return -1;
}
int value = 0;
while (isdigit((unsigned char)*current)) {
value = value * 10 + (*current - '0');
current++;
}
if (*current != '\0') {
return -1;
}
*out = sign * value;
return 0;
}#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || *s == '\0') {
return -1;
}
const char *current = s;
int sign = 1;
if (*current == '+' || *current == '-') {
sign = (*current == '-') ? -1 : 1;
current++;
if (*current == '\0') {
return -1;
}
}
if (!isdigit((unsigned char)*current)) {
return -1;
}
int value = 0;
while (*current != '\0') {
if (!isdigit((unsigned char)*current)) {
return -1;
}
value = value * 10 + (*current - '0');
current++;
}
*out = sign * value;
return 0;
}#include <ctype.h>
int atoi_strict(const char *s, int *out) {
if (s == NULL || *s == '\0') {
return -1;
}
const char *current = s;
int sign = 1;
if (*current == '+' || *current == '-') {
sign = (*current == '-') ? -1 : 1;
current++;
if (*current == '\0') {
return -1;
}
}
if (!isdigit((unsigned char)*current)) {
return -1;
}
int value = 0;
while (*current != '\0') {
if (!isdigit((unsigned char)*current)) {
return -1;
}
value = value * 10 + (*current - '0');
current++;
}
*out = sign * value;
return 0;
}Implement in C11: `int glob_match(const char *pat, const char *str)`. Returns 1 if str matches the shell-style pattern pat, else 0. '*' matches any sequence (including empty), '?' matches exactly one character. All other characters match literally. The whole string must match. Reply with only a C code block. No main function, no tests, no explanation.
(no code extracted)(no code extracted)(no code extracted)Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
(no code extracted)(no code extracted)(no code extracted)Served via llama.cpp CUDA · 9.3 tok/s · 4.7 GB RAM · coding total 73/126
Implement in C11: `void itoa(int value, char *buf)`. Writes the decimal string of value into buf (NUL-terminated). Must handle 0 and negatives, including INT_MIN. Reply with only a C code block. No main function, no tests, no explanation.
#include <limits.h>
#include <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: `int parse_csv_ints(const char *s, int *out, int max)`. Parses comma-separated integers from s into out (at most max). Returns the count stored. Input is well-formed (digits, optional leading minus, commas). Reply with only a C code block. No main function, no tests, no explanation.
#include <stdio.h>
#include <string.h>
int parse_csv_ints(const char *s, int *out, int max) {
int count = 0;
int i = 0;
int j = 0;
int neg = 0;
while (s[i] != '\0') {
if (s[i] == '-') {
neg = 1;
i++;
}
if (isdigit(s[i])) {
out[count] = neg * atoi(s + i);
count++;
i += strlen(s + i);
neg = 0;
}
if (s[i] == ',') {
i++;
}
}
return count;
}Implement in C11: `void trim(char *s)`. Strips leading and trailing whitespace (space, tab, newline) from s in place. Reply with only a C code block. No main function, no tests, no explanation.
#include <string.h>
void trim(char *s) {
size_t i, j;
for (i = 0; s[i] && strchr(" \t\n", s[i]); i++)
;
for (j = strlen(s) - 1; j >= 0 && strchr(" \t\n", s[j]); j--)
;
s[j + 1] = 0;
memmove(s, s + i, j - i + 1);
}Implement in C11: `void ring_init(Ring *r); int ring_push(Ring *r, int v); int ring_pop(Ring *r, int *out)`. Circular buffer, capacity 8, FIFO. Include this exact definition in your code: typedef struct { int data[8]; int head; int count; } Ring; push returns 0 on success, -1 when full; pop returns 0 on success, -1 when empty. Pushes after pops must reuse freed slots (wraparound).
Reply with only a C code block. No main function, no tests, no explanation.#include <stdint.h>
typedef struct {
int data[8];
int head;
int count;
} Ring;
void ring_init(Ring *r) {
r->head = 0;
r->count = 0;
}
int ring_push(Ring *r, int v) {
if (r->count == 8) {
return -1;
}
r->data[r->head] = v;
r->head = (r->head + 1) % 8;
r->count++;
return 0;
}
int ring_pop(Ring *r, int *out) {
if (r->count == 0) {
return -1;
}
*out = r->data[r->head];
r->head = (r->head + 1) % 8;
r->count--;
return 0;
}#include <stdint.h>
typedef struct {
int data[8];
int head;
int count;
} Ring;
void ring_init(Ring *r) {
r->head = 0;
r->count = 0;
}
int ring_push(Ring *r, int v) {
if (r->count == 8) {
return -1;
}
r->data[r->head] = v;
r->head = (r->head + 1) % 8;
r->count++;
return 0;
}
int ring_pop(Ring *r, int *out) {
if (r->count == 0) {
return -1;
}
*out = r->data[r->head];
r->head = (r->head + 1) % 8;
r->count--;
return 0;
}#include <stdint.h>
typedef struct {
int data[8];
int head;
int count;
} Ring;
void ring_init(Ring *r) {
r->head = 0;
r->count = 0;
}
int ring_push(Ring *r, int v) {
if (r->count == 8) {
return -1;
}
r->data[r->head] = v;
r->head = (r->head + 1) % 8;
r->count++;
return 0;
}
int ring_pop(Ring *r, int *out) {
if (r->count == 0) {
return -1;
}
*out = r->data[r->head];
r->head = (r->head + 1) % 8;
r->count--;
return 0;
}Implement in C11: `int atoi_strict(const char *s, int *out)`. Parses an optionally negative decimal integer from s into out. Returns 0 on success, -1 if s is not exactly one valid integer (empty, junk, trailing chars). No overflow tests. Reply with only a C code block. No main function, no tests, no explanation.
int atoi_strict(const char *s, int *out) {
int neg = 0, i;
for (i = 0; s[i] == ' '; i++)
;
if (s[i] == '-') {
neg = 1;
i++;
}
if (s[i] < '0' || s[i] > '9')
return -1;
for (; s[i] >= '0' && s[i] <= '9'; i++)
;
if (s[i] != '\0')
return -1;
*out = neg ? -atoi(s) : atoi(s);
return 0;
}int atoi_strict(const char *s, int *out) {
int neg = 0, i;
for (i = 0; s[i] == ' '; i++)
;
if (s[i] == '-') {
neg = 1;
i++;
}
if (s[i] < '0' || s[i] > '9')
return -1;
for (; s[i] >= '0' && s[i] <= '9'; i++)
;
if (s[i] != '\0')
return -1;
*out = neg ? -atoi(s) : atoi(s);
return 0;
}int atoi_strict(const char *s, int *out) {
int neg = 0, i;
for (i = 0; s[i] == ' '; i++)
;
if (s[i] == '-') {
neg = 1;
i++;
}
if (s[i] < '0' || s[i] > '9')
return -1;
for (; s[i] >= '0' && s[i] <= '9'; i++)
;
if (s[i] != '\0')
return -1;
*out = neg ? -atoi(s) : atoi(s);
return 0;
}Implement in C11: `int glob_match(const char *pat, const char *str)`. Returns 1 if str matches the shell-style pattern pat, else 0. '*' matches any sequence (including empty), '?' matches exactly one character. All other characters match literally. The whole string must match. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdio.h>
#include <string.h>
int glob_match(const char *pat, const char *str) {
int p = 0, s = 0;
while (1) {
if (pat[p] == '*') {
if (str[s] == '\0') return 1;
p++;
} else if (pat[p] == '?') {
if (str[s] == '\0') return 0;
p++; s++;
} else if (pat[p] == '\0') {
if (str[s] == '\0') return 1;
} else if (pat[p] == str[s]) {
p++; s++;
} else {
return 0;
}
}
}#include <stdio.h>
#include <string.h>
int glob_match(const char *pat, const char *str) {
int p = 0, s = 0;
while (1) {
if (pat[p] == '*') {
if (str[s] == '\0') return 1;
p++;
} else if (pat[p] == '?') {
if (str[s] == '\0') return 0;
p++; s++;
} else if (pat[p] == '\0') {
if (str[s] == '\0') return 1;
} else if (pat[p] == str[s]) {
p++; s++;
} else {
return 0;
}
}
}#include <stdio.h>
#include <string.h>
int glob_match(const char *pat, const char *str) {
int p = 0, s = 0;
while (1) {
if (pat[p] == '*') {
if (str[s] == '\0') return 1;
p++;
} else if (pat[p] == '?') {
if (str[s] == '\0') return 0;
p++; s++;
} else if (pat[p] == '\0') {
if (str[s] == '\0') return 1;
} else if (pat[p] == str[s]) {
p++; s++;
} else {
return 0;
}
}
}Implement in C11: `double rpn_eval(const char *expr)`. Evaluates a space-separated Reverse Polish Notation expression of non-negative integers and operators + - * /. Returns the result as a double. Input is always valid and non-empty. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
double rpn_eval(const char *expr) {
double stack[100];
int top = 0;
bool is_operator[100];
char token[10];
while (expr[0] != '\0') {
if (isspace(expr[0])) {
expr++;
continue;
}
if (isdigit(expr[0]) || expr[0] == '.') {
double num = strtod(expr, &token);
stack[top++] = num;
expr = token;
} else if (expr[0] == '+' || expr[0] == '-' || expr[0] == '*' || expr[0] == '/') {
is_operator[top - 1] = true;
expr++;
} else {
printf("Invalid token: %s\n", expr);
return 0.0;
}
}
while (top > 1) {
if (!is_operator[top - 2]) {
stack[top - 2] += stack[top - 1];
top--;
} else if (is_operator[top - 2] && (stack[top - 2] == 0 || stack[top - 1] == 0)) {
stack[top - 2] += stack[top - 1];
top--;
} else {
switch (stack[top - 2]) {
case '+':
stack[top - 2] = stack[top - 1] + stack[top - 2];
break;
case '-':
stack[top - 2] = stack[top - 1] - stack[top - 2];
break;
case '*':
stack[top - 2] = stack[top - 1] * stack[top - 2];
break;
case '/':
stack[top - 2] = stack[top - 1] / stack[top - 2];
break;
}
top--;
}
}
return stack[0];
}#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
double rpn_eval(const char *expr) {
double stack[100];
int top = 0;
bool is_operator[100];
char token[10];
while (expr[0] != '\0') {
if (isspace(expr[0])) {
expr++;
continue;
}
if (isdigit(expr[0]) || expr[0] == '.') {
double num = strtod(expr, &token);
stack[top++] = num;
expr = token;
} else if (expr[0] == '+' || expr[0] == '-' || expr[0] == '*' || expr[0] == '/') {
is_operator[top - 1] = true;
expr++;
} else {
printf("Invalid token: %s\n", expr);
return 0.0;
}
}
while (top > 1) {
if (!is_operator[top - 2]) {
stack[top - 2] += stack[top - 1];
top--;
} else if (is_operator[top - 2] && (stack[top - 2] == 0 || stack[top - 1] == 0)) {
stack[top - 2] += stack[top - 1];
top--;
} else {
switch (stack[top - 2]) {
case '+':
stack[top - 2] = stack[top - 1] + stack[top - 2];
break;
case '-':
stack[top - 2] = stack[top - 1] - stack[top - 2];
break;
case '*':
stack[top - 2] = stack[top - 1] * stack[top - 2];
break;
case '/':
stack[top - 2] = stack[top - 1] / stack[top - 2];
break;
}
top--;
}
}
return stack[0];
}#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
double rpn_eval(const char *expr) {
double stack[100];
int top = 0;
bool is_operator[100];
char token[10];
while (expr[0] != '\0') {
if (isspace(expr[0])) {
expr++;
continue;
}
if (isdigit(expr[0]) || expr[0] == '.') {
double num = strtod(expr, &token);
stack[top++] = num;
expr = token;
} else if (expr[0] == '+' || expr[0] == '-' || expr[0] == '*' || expr[0] == '/') {
is_operator[top - 1] = true;
expr++;
} else {
printf("Invalid token: %s\n", expr);
return 0.0;
}
}
while (top > 1) {
if (!is_operator[top - 2]) {
stack[top - 2] += stack[top - 1];
top--;
} else if (is_operator[top - 2] && (stack[top - 2] == 0 || stack[top - 1] == 0)) {
stack[top - 2] += stack[top - 1];
top--;
} else {
switch (stack[top - 2]) {
case '+':
stack[top - 2] = stack[top - 1] + stack[top - 2];
break;
case '-':
stack[top - 2] = stack[top - 1] - stack[top - 2];
break;
case '*':
stack[top - 2] = stack[top - 1] * stack[top - 2];
break;
case '/':
stack[top - 2] = stack[top - 1] / stack[top - 2];
break;
}
top--;
}
}
return stack[0];
}Implement in C11: `int csv_field(const char *line, int idx, char *out, size_t cap)`. Extracts field idx (0-based) from one RFC-4180 CSV line into out (NUL-terminated, truncated to cap-1 chars). Quoted fields may contain commas and doubled quotes '""' which unescape to a single quote. Returns the field's unescaped length, or -1 if idx is out of range. Reply with only a C code block. No main function, no tests, no explanation.
#include <stdio.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
int i, j, in_quotes, len = 0;
char c;
for (i = 0; line[i] != '\0' && idx-- > 0; i++) {
if (line[i] == '"') {
in_quotes = !in_quotes;
while (line[i] == '"') i++;
}
}
if (idx < 0) {
return -1;
}
i--;
while (line[i] == ' ') i--;
while (line[i] != '\0' && line[i] != ',' && line[i] != '"') {
if (len < cap - 1) {
out[len++] = line[i++];
} else {
return len;
}
}
out[len] = '\0';
return len;
}#include <stdio.h>
#include <string.h>
int csv_field(const char *line, int idx, char *out, size_t cap) {
int i, j, in_quotes, len = 0;
const char *p = line;
if (idx < 0 || idx >= strcspn(line, ","))
return -1;
for (i = 0; i < idx; i++) {
while (*p && *p != ',' && *p != '"')
p++;
if (*p)
p++;
}
in_quotes = 0;
while (*p && (in_quotes || *p != ',')) {
if (*p == '"')
in_quotes = !in_quotes;
else if (in_quotes && *(p + 1) == '"') {
out[len++] = '"';
p++;
} else if (in_quotes) {
while (*p && *p != '"') {
out[len++] = *p;
p++;
}
} else {
while (*p && *p != ',' && *p != '"') {
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++;
}
in_quotes = 0;
while (*p && (in_quotes || *p != ',')) {
if (*p == '"')
in_quotes = !in_quotes;
else if (in_quotes && *(p + 1) == '"') {
out[len++] = '"';
p++;
} else if (in_quotes) {
while (*p && *p != '"') {
out[len++] = *p;
p++;
}
} else {
while (*p && *p != ',' && *p != '"') {
out[len++] = *p;
p++;
}
}
}
out[len] = '\0';
return len;
}