How to extract substring between single quotation marks in C

4 Answers

0 votes
#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'
  
*/
 

 



answered Feb 11, 2025 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

int main(void) {
    const char* str = "C is a 'general-purpose' programming language";
    char* subString = NULL;  
    char copystr[512] = ""; 
    
    strcpy(copystr, str);

    subString = strtok(copystr, "'"); // find the first single quotation marks
    subString = strtok(NULL, "'"); // find the second single quotation marks
    
    if (subString == NULL) {
        printf("Not found\n");
    }
    else {
        printf("'%s'\n", subString);
    }
    
    return 0;
}

  
  
/*
run:
  
'general-purpose'
  
*/
 

 



answered Feb 11, 2025 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

void extractSubstring(const char *str, char *subString) {
    const char *start = strchr(str, '\'');
    
    if (start != NULL) {
        const char *end = strchr(start + 1, '\'');
        if (end != NULL) {
            size_t length = end - start - 1;
            strncpy(subString, start + 1, length);
            subString[length] = '\0'; // Null-terminate the subString string
        }
    }
}

int main() {
    const char* str = "C is a 'general-purpose' programming language";
    char subString[64] = ""; 

    extractSubstring(str, subString);

    printf("'%s'\n", subString);

    return 0;
}

  
  
/*
run:
  
'general-purpose'
  
*/
 

 



answered Feb 11, 2025 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

int main() {
    const char* str = "C is a 'general-purpose' programming language";
    char subString[64] = ""; 

    if (sscanf(str, "%*[^']'%[^']'", subString) == 1) {
        printf("'%s'\n", subString);
    } 
    else {
        printf("No single quotation marks in string.\n");
    }

    return 0;
}

  
  
/*
run:

'general-purpose'
  
*/
 

 



answered Feb 11, 2025 by avibootz

Related questions

1 answer 67 views
1 answer 102 views
1 answer 97 views
1 answer 85 views
1 answer 114 views
1 answer 101 views
...