检查子string存在于C中的一个string中

我想检查一个string是否包含像C中的子string:

char *sent = "this is my sample example"; char *word = "sample"; if (/* sentence contains word */) { /* .. */ } 

在C ++ string::find什么来代替string::find

 if(strstr(sent, word) != NULL) { /* ... */ } 

请注意,如果find单词, strstr返回一个指向已sent单词开始的指针。

为此使用strstr

http://www.cplusplus.com/reference/clibrary/cstring/strstr/

所以,你会写…

 char *sent = "this is my sample example"; char *word = "sample"; char *pch = strstr(sent, word); if(pch) { ... } 

尝试使用指针…

 #include <stdio.h> #include <string.h> int main() { char str[] = "String1 subString1 Strinstrnd subStr ing1subString"; char sub[] = "subString"; char *p1, *p2, *p3; int i=0,j=0,flag=0; p1 = str; p2 = sub; for(i = 0; i<strlen(str); i++) { if(*p1 == *p2) { p3 = p1; for(j = 0;j<strlen(sub);j++) { if(*p3 == *p2) { p3++;p2++; } else break; } p2 = sub; if(j == strlen(sub)) { flag = 1; printf("\nSubstring found at index : %d\n",i); } } p1++; } if(flag==0) { printf("Substring NOT found"); } return (0); } 

你可以尝试这个find子string的存在,并提取和打印它:

 #include <stdio.h> #include <string.h> int main(void) { char mainstring[]="The quick brown fox jumps over the lazy dog"; char substring[20], *ret; int i=0; puts("enter the sub string to find"); fgets(substring, sizeof(substring), stdin); substring[strlen(substring)-1]='\0'; ret=strstr(mainstring,substring); if(strcmp((ret=strstr(mainstring,substring)),substring)) { printf("substring is present\t"); } printf("and the sub string is:::"); for(i=0;i<strlen(substring);i++) { printf("%c",*(ret+i)); } puts("\n"); return 0; } 

这里是如何报告find的子string的第一个字符的位置:

在上面的代码中replace这一行:

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

有:

 printf("substring %s was found at position %d \n", substring,((int) (substring - mainstring))); 

这段代码实现了search如何工作的逻辑(其中一种方法),而不使用任何现成的函数:

 public int findSubString(char[] original, char[] searchString) { int returnCode = 0; //0-not found, -1 -error in imput, 1-found int counter = 0; int ctr = 0; if (original.Length < 1 || (original.Length)<searchString.Length || searchString.Length<1) { returnCode = -1; } while (ctr <= (original.Length - searchString.Length) && searchString.Length > 0) { if ((original[ctr]) == searchString[0]) { counter = 0; for (int count = ctr; count < (ctr + searchString.Length); count++) { if (original[count] == searchString[counter]) { counter++; } else { counter = 0; break; } } if (counter == (searchString.Length)) { returnCode = 1; } } ctr++; } return returnCode; } 

这个简单的代码也可以实现:为什么使用这些代码:

 int main(void) { char mainstring[]="The quick brown fox jumps over the lazy dog"; char substring[20]; int i=0; puts("enter the sub stirng to find"); fgets(substring, sizeof(substring), stdin); substring[strlen(substring)-1]='\0'; if (strstr(mainstring,substring)) { printf("substring is present\t"); } printf("and the sub string is:::"); printf("%s",substring,"\n"); return 0; } 

但棘手的部分将是在原始string的哪个位置报告子string开始…

 #include <stdio.h> #include <string.h> int findSubstr(char *inpText, char *pattern); int main() { printf("Hello, World!\n"); char *Text = "This is my sample program"; char *pattern = "sample"; int pos = findSubstr(Text, pattern); if (pos > -1) { printf("Found the substring at position %d \n", pos); } else printf("No match found \n"); return 0; } int findSubstr(char *inpText, char *pattern) { int inplen = strlen(inpText); while (inpText != NULL) { char *remTxt = inpText; char *remPat = pattern; if (strlen(remTxt) < strlen(remPat)) { /* printf ("length issue remTxt %s \nremPath %s \n", remTxt, remPat); */ return -1; } while (*remTxt++ == *remPat++) { printf("remTxt %s \nremPath %s \n", remTxt, remPat); if (*remPat == '\0') { printf ("match found \n"); return inplen - strlen(inpText+1); } if (remTxt == NULL) { return -1; } } remPat = pattern; inpText++; } }