使用C字符串中的两个char *获取子字符串

时间:2017-05-25 16:07:49

标签: c++ c string char

long_textkeyword1keyword2成为三个char*指针。 _keyword1__keyword2_long_text的两个子串。使用strstr(long_text, keyword1)我可以得到一个char*,指向keyword1中第一次出现long_text,并使用strstr(long_text, keyword2)我可以获得char*它指向keyword2中第一次出现long_textkeyword1keyword2不重叠。

是否可以使用从strstr()获得的两个long_textkeyword1中提取代表keyword2char*之间字符串的子字符串?

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(){
    char* long_text = "this is keyword1 and this is keyword2 in long_text";
    char* keyword1 = "keyword1";
    char* keyword2 = "keyword2";

    char* k1_start = strstr(long_text, keyword1);
    char* k2_start = strstr(long_text, keyword2);

    // TODO Be able to print " and this is "

    return 0;
}

4 个答案:

答案 0 :(得分:2)

这是你缺少的部分

// Move k1_start to end of keyword1
k1_start += strlen(keyword1);

// Copy text from k1_start to k2_start
char sub_string[32];
int  len = k2_start - k1_start;

strncpy(sub_string, k1_start, len);

// Be sure to terminate the string
sub_string[len] = '\0';

答案 1 :(得分:2)

是啊..

这是C-like并使用char *并支持char数组。

#include <stdio.h>
#include <string.h>

int main(void) {
    char* long_text = "key1(text)key2";
    char* keyword1 = "key1";
    char* keyword2 = "key2";

    char* k1 = strstr(long_text, keyword1);
    char* k2 = strstr(long_text, keyword2);

    // from first char of match up to first char of second match
    char text[strlen(k1) - strlen(k2)];

    int len = (int)strlen(k1);
    for (int i = 0;; i++) {
        text[i] = *k1; k1++;
        if (i == (len - strlen(k2))) {
            text[len - strlen(k2)] = '\0';
            break;
        }
    }
    char* res;
    //We have now only keyword1 + middle part, compare until diff.,
    //then remember position and just iterate from to it later.
    int j = 0;
    for (int i = 0;; i++) {
        if (*keyword1 == text[i]) {
            j = i;
            keyword1++;
        } else {
            res = &text[++j];
            break;
        }
    }

    printf("%s\n", res);

    return 0;
}

答案 2 :(得分:1)

void look_for_middle(const char *haystack,
                     const char *needle1, const char *needle2) {
    const char *start; /* start of region between keywords */
    const char *end;   /* end of region between keywords */
    const char *pos1;  /* match needle1 within haystack */
    const char *pos2;  /* match needle2 within haystack */
    int length;  /* length of region between needles */

    /* Look for needles in haystack. */
    pos1 = strstr(haystack, needle1);
    pos2 = strstr(haystack, needle2);
    if (pos1 != NULL && pos2 != NULL) {
        /* Both needles were found. */
        if (pos1 < pos2) {
            /* needle1 appears before needle2 */
            start = pos1 + strlen(needle1);
            end = pos2;
        } else {
            /* needle2 appears before needle1 */
            start = pos2 + strlen(needle2);
            end = pos1;
        }
        length = end - start;
    } else {
        /* One or more needles were not found. */
        start = NULL;
        end = NULL;
        length = 0;
    }

    /* Report result. */
    if (start != NULL) {
        /* Both needles were found. */
        if (length < 0) {
            printf("Needles overlap\n");
        } else {
            /*
             * In this printf, the "precision" of the string
             * is set so that it only prints the portion between
             * the needles.
             */
            printf("Middle pos %d len %d: %.*s\n",
                   (int)(start - haystack), length, length, start);
        }
    } else {
        if (pos1 == NULL) {
            printf("Needle 1 not found\n");
        }
        if (pos2 == NULL) {
            printf("Needle 2 not found\n");
        }
    }
}

答案 3 :(得分:0)

这个功能可以完成你的工作。

   char* strInner(char *long_text , char *keyword1 , char* keyword2)
    {

        char * a = strstr(long_text,keyword1);
        a+= strlen(keyword1);

        if(a==NULL)
        {
            cout<<"Keyword1 didn't found ! ";
            return a ;
        }

        char * b = strstr(a,keyword2);

        if(b==NULL)
        {
            cout<<"Keyword2 didn't found Or, found before Keyword1 ! ";
            return b ;
        }

        char *inner = (char*)malloc(strlen(a)-strlen(b));
        memcpy(inner,a,strlen(a)-strlen(b) );

        return inner ;

    }