I would like to have a function in C for generic string parsing. It should be able to handle delimiters, string patterns, and provide a way to access the parsed tokens easily.
Assistant
Here's a simple implementation of a generic string parsing function in C. This function will tokenize an input string based on specified delimiters and store the tokens in a dynamically allocated array of strings. The user can also provide a count of how many tokens were found.
c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char** parse_string(const char* input, const char* delimiters, int* token_count) {
char* str = strdup(input);
char* token;
int count = 0;
char** tokens = NULL;
token = strtok(str, delimiters);
while (token != NULL) {
++count;
tokens = realloc(tokens, count * sizeof(char*));
tokens[count - 1] = strdup(token);
token = strtok(NULL, delimiters);
}
free(str);
*token_count = count;
return tokens;
}
void free_tokens(char** tokens, int count) {
for (int i = 0; i < count; ++i) {
free(tokens[i]);
}
free(tokens);
}
int main() {
const char* input = "This is a sample string, with some delimiters!";
const char* delimiters = " ,!";
int token_count;
char** tokens = parse_string(input, delimiters, &token_count);
for (int i = 0; i < token_count; ++i) {
printf("Token %d: %s\n", i + 1, tokens[i]);
}
free_tokens(tokens, token_count);
return 0;
}
This implementation uses strtok() from the C standard library for tokenization. The parse_string() function allocates memory for the tokens and returns a pointer to the array of strings. The
User
now let's write it without the use of strtok().
Assistant
Sure! Here's an implementation of the parse_string function without using strtok():
c
User
now let's create a version that takes an array for token strings, instead of allocating memory.
Assistant
Here's a version of the parse_string function that takes an array of character pointers to store tokens, instead of dynamically allocating memory:
c
User
let's do a version where instead of a fixed array, the function takes an argument for the maximum number of tokens. It's the caller's responsibility to free the token array and the tokens inside.
Assistant
Here's a version of the parse_string function that takes an additional argument for the maximum number of tokens. The function will allocate memory for the token array and the individual tokens, and it's the caller's responsibility to free them:
c
User
let's create a C function that trims whitespace from the beginning and end of a string.
Assistant
Here's a C function that trims the whitespace from the beginning and end of a string:
c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char* trim_whitespace(const char* input) {
const char* start = input;
const char* end = input + strlen(input) - 1;
// Find the first non-whitespace character
while (isspace(*start)) {
start++;
}
// Find the last non-whitespace character
while (isspace(*end) && end > start) {
end--;
}
// Calculate the trimmed string length
size_t trimmed_length = end - start + 1;
// Allocate memory for the trimmed string
char* trimmed_string = malloc((trimmed_length + 1) * sizeof(char));
// Copy the trimmed string and add the null terminator
strncpy(trimmed_string, start, trimmed_length);
trimmed_string[trimmed_length] = '\0';
return trimmed_string;
}
int main() {
const char* input = " This is a string with whitespace. ";
char* trimmed_string = trim_whitespace(input);
printf("Trimmed string: '%s'\n", trimmed_string);
free(trimmed_string);
return 0;
}
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
bool is_delimiter(char c, const char* delimiters) {
for (size_t i = 0; i < strlen(delimiters); ++i) {
if (c == delimiters[i]) {
return true;
}
}
return false;
}
char** parse_string(const char* input, const char* delimiters, int* token_count) {
size_t input_len = strlen(input);
char* str = strdup(input);
char* token_start = str;
int count = 0;
char** tokens = NULL;
for (size_t i = 0; i <= input_len; ++i) {
if (is_delimiter(str[i], delimiters) || str[i] == '\0') {
if (token_start != &str[i]) {
++count;
tokens = realloc(tokens, count * sizeof(char*));
size_t token_len = &str[i] - token_start;
tokens[count - 1] = malloc((token_len + 1) * sizeof(char));
strncpy(tokens[count - 1], token_start, token_len);
tokens[count - 1][token_len] = '\0';
}
token_start = &str[i + 1];
}
}
free(str);
*token_count = count;
return tokens;
}
void free_tokens(char** tokens, int count) {
for (int i = 0; i < count; ++i) {
free(tokens[i]);
}
free(tokens);
}
int main() {
const char* input = "This is a sample string, with some delimiters!";
const char* delimiters = " ,!";
int token_count;
char** tokens = parse_string(input, delimiters, &token_count);
for (int i = 0; i < token_count; ++i) {
printf("Token %d: %s\n", i + 1, tokens[i]);
}
free_tokens(tokens, token_count);
return 0;
}
In this implementation, I've added a helper function is_delimiter to check if a character is a delimiter. The parse_string function iterates through the input string, checking each character to determine if it's a delimiter or not, and creating tokens accordingly.
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define MAX_TOKENS 100
#define MAX_TOKEN_LENGTH 100
bool is_delimiter(char c, const char* delimiters) {
for (size_t i = 0; i < strlen(delimiters); ++i) {
if (c == delimiters[i]) {
return true;
}
}
return false;
}
int parse_string(const char* input, const char* delimiters, char tokens[][MAX_TOKEN_LENGTH]) {
size_t input_len = strlen(input);
char* str = strdup(input);
char* token_start = str;
int count = 0;
for (size_t i = 0; i <= input_len; ++i) {
if (is_delimiter(str[i], delimiters) || str[i] == '\0') {
if (token_start != &str[i]) {
size_t token_len = &str[i] - token_start;
if (count < MAX_TOKENS && token_len < MAX_TOKEN_LENGTH) {
strncpy(tokens[count], token_start, token_len);
tokens[count][token_len] = '\0';
++count;
}
else {
break;
}
}
token_start = &str[i + 1];
}
}
free(str);
return count;
}
int main() {
const char* input = "This is a sample string, with some delimiters!";
const char* delimiters = " ,!";
char tokens[MAX_TOKENS][MAX_TOKEN_LENGTH];
int token_count = parse_string(input, delimiters, tokens);
for (int i = 0; i < token_count; ++i) {
printf("Token %d: %s\n", i + 1, tokens[i]);
}
return 0;
}
In this version, the parse_string function takes a two-dimensional array tokens as a parameter to store the parsed tokens. The MAX_TOKENS and MAX_TOKEN_LENGTH constants define the maximum number of tokens and the maximum length of each token. Note that this approach has limitations as it might not work for very long strings or a large number of tokens, as it depends on the fixed size of the provided array.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
bool is_delimiter(char c, const char* delimiters) {
for (size_t i = 0; i < strlen(delimiters); ++i) {
if (c == delimiters[i]) {
return true;
}
}
return false;
}
char** parse_string(const char* input, const char* delimiters, int max_tokens, int* token_count) {
size_t input_len = strlen(input);
char* str = strdup(input);
char* token_start = str;
int count = 0;
char** tokens = malloc(max_tokens * sizeof(char*));
for (size_t i = 0; i <= input_len; ++i) {
if (is_delimiter(str[i], delimiters) || str[i] == '\0') {
if (token_start != &str[i]) {
size_t token_len = &str[i] - token_start;
if (count < max_tokens) {
tokens[count] = malloc((token_len + 1) * sizeof(char));
strncpy(tokens[count], token_start, token_len);
tokens[count][token_len] = '\0';
++count;
} else {
break;
}
}
token_start = &str[i + 1];
}
}
free(str);
*token_count = count;
return tokens;
}
void free_tokens(char** tokens, int count) {
for (int i = 0; i < count; ++i) {
free(tokens[i]);
}
free(tokens);
}
int main() {
const char* input = "This is a sample string, with some delimiters!";
const char* delimiters = " ,!";
int max_tokens = 10;
int token_count;
char** tokens = parse_string(input, delimiters, max_tokens, &token_count);
for (int i = 0; i < token_count; ++i) {
printf("Token %d: %s\n", i + 1, tokens[i]);
}
free_tokens(tokens, token_count);
return 0;
}
In this version, the parse_string function takes an additional max_tokens argument to limit the number of tokens. The function allocates memory for the token array and individual tokens within the given limit. The free_tokens function can be used to free the memory allocated for the tokens and the token array.
This function, trim_whitespace, takes a string as input and returns a newly allocated string with the whitespace removed from the beginning and end of the input string. The caller is responsible for freeing the memory allocated for the trimmed string.