Reputation: 1001
Let long_text
, keyword1
and keyword2
be three char*
pointers. _keyword1_
and _keyword2_
being two substrings of long_text
. Using strstr(long_text, keyword1)
I can get a char*
which points to the first occurrence of keyword1
in long_text
, and using strstr(long_text, keyword2)
I can get a char*
which points to the first occurrence of keyword2
in long_text
. keyword1
and keyword2
do not overlap.
Is there a way to extract a substring from long_text
representing the string between keyword1
and keyword2
using the two char*
obtained from strstr()
?
#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;
}
Upvotes: 2
Views: 676
Reputation: 6795
This function can do your job . .
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 ;
}
Upvotes: 0
Reputation: 4791
Yeah..
This is C-like and uses char *
and supporting char array.
#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;
}
Upvotes: 2
Reputation: 406
This is the part you are missing
// 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';
Upvotes: 3
Reputation: 17438
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");
}
}
}
Upvotes: 1