#include <stdio.h>
#include <string.h>
#include <stdlib.h> // Include for malloc and free
void extract_substring_between_single_quotation_marks(const char* str, char** subString) {
char* copy_str = malloc(strlen(str) + 1); // Allocate memory
if (copy_str == NULL) {
perror("malloc failed");
*subString = NULL;
return;
}
strcpy(copy_str, str);
char* first_quote = strtok(copy_str, "'");
if (first_quote == NULL) {
free(copy_str);
*subString = NULL;
return;
}
char* second_quote = strtok(NULL, "'");
if (second_quote == NULL) {
free(copy_str);
*subString = NULL;
return;
}
*subString = malloc(strlen(second_quote) + 1); // Allocate for substring
if (*subString == NULL) {
perror("malloc failed");
free(copy_str);
return;
}
strcpy(*subString, second_quote);
free(copy_str); // Free the copy of the original string
}
int main(void) {
const char* str = "C is a 'general-purpose' programming language";
char* subString = NULL;
extract_substring_between_single_quotation_marks(str, &subString);
if (subString == NULL) {
printf("Not found\n");
} else {
printf("'%s'\n", subString);
free(subString); // Free the allocated substring
}
return 0;
}
/*
run:
'general-purpose'
*/