In C, find the substring position in the line

Here is the program for adoption:

  • Sentence from the user.
  • A word from the user.

How to find the position of a word entered in a sentence?

#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char sntnc[50], word[50], *ptr[50]; int pos; puts("\nEnter a sentence"); gets(sntnc); fflush(stdin); puts("\nEnter a word"); gets(word); fflush(stdin); ptr=strstr(sntnc,word); //how do I find out at what position the word occurs in the sentence? //Following is the required output printf("The word starts at position #%d", pos); return 0; } 
+7
source share
6 answers

The ptr points to the beginning of the word , so you can simply subtract the sntnc sentence pointer from it:

 pos = ptr - sntnc; 
+15
source

The return strstr () is a pointer to the first occurrence of your "word", therefore

 pos=ptr-sntc; 

This only works because sntc and ptr are pointers to the same line. To clarify when I'm talking about occurrence, this is the position of the first char match when the corresponding line is in your target line.

+4
source

For reference only:

 char saux[] = "this is a string, try to search_this here"; int dlenstr = strlen(saux); if (dlenstr > 0) { char *pfound = strstr(saux, "search_this"); //pointer to the first character found 's' in the string saux if (pfound != NULL) { int dposfound = int (pfound - saux); //saux is already pointing to the first string character 't'. } } 
+2
source

For some reason, I am having problems with strstr (), and I also need an index.

I made this function to find the position of a substring inside a larger string (if exists), otherwise it returns -1.

  int isSubstring(char * haystack, char * needle) { int i = 0; int d = 0; if (strlen(haystack) >= strlen(needle)) { for (i = strlen(haystack) - strlen(needle); i >= 0; i--) { int found = 1; //assume we found (wanted to use boolean) for (d = 0; d < strlen(needle); d++) { if (haystack[i + d] != needle[d]) { found = 0; break; } } if (found == 1) { return i; } } return -1; } else { //fprintf(stdout, "haystack smaller\n"); } } 
+1
source

My comment on the ORIGINAL post in this thread: This ad is NOT GOOD:

  char sntnc[50], word[50], *ptr[50]; 

C code doesn't even compile: on this line it will fail:

  ptr = strstr(sntnc,word); 

Thus, the line should be changed to:

  char sntnc[50], word[50], *ptr; 

And you DO NOT need the memeory allocated for 'ptr string'. You just need a pointer to char.

0
source

You can use this simple modification of strpos

 #include <stdio.h> #include <stdlib.h> #include <string.h> int strpos(char *haystack, char *needle, int offset); int main() { char *p = "Hello there all y'al, hope that you are all well"; int pos = strpos(p, "all", 0); printf("First all at : %d\n", pos); pos = strpos(p, "all", 10); printf("Second all at : %d\n", pos); } int strpos(char *hay, char *needle, int offset) { char haystack[strlen(hay)]; strncpy(haystack, hay+offset, strlen(hay)-offset); char *p = strstr(haystack, needle); if (p) return p - haystack+offset; return -1; } 
0
source

All Articles