Write ac program to search the position of a substring within a string use string methods

The includes() method performs a case-sensitive search to determine whether one string may be found within another string, returning true or false as appropriate.

Try it

Syntax

includes(searchString)
includes(searchString, position)

Parameters

searchString

A string to be searched for within str. Cannot be a regex.

position Optional

The position within the string at which to begin searching for searchString. (Defaults to 0.)

Return value

true if the search string is found anywhere within the given string; otherwise, false if not.

Exceptions

Description

This method lets you determine whether or not a string includes another string.

Case-sensitivity

The includes() method is case sensitive. For example, the following expression returns false:

'Blue Whale'.includes('blue')  // returns false

You can work around this constraint by transforming both the original string and the search string to all lowercase:

'Blue Whale'.toLowerCase().includes('blue')  // returns true

Examples

Using includes()

const str = 'To be, or not to be, that is the question.'

console.log(str.includes('To be'))        // true
console.log(str.includes('question'))     // true
console.log(str.includes('nonexistent'))  // false
console.log(str.includes('To be', 1))     // false
console.log(str.includes('TO BE'))        // false
console.log(str.includes(''))             // true

Specifications

Specification
ECMAScript Language Specification
# sec-string.prototype.includes

Browser compatibility

BCD tables only load in the browser

See also

10

New! Save questions or answers and organize your favorite content.
Learn more.

Here is a program to accept a:

  1. Sentence from a user.
  2. Word from a user.

How do I find the position of the word entered in the 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;
}

the Tin Man

156k41 gold badges209 silver badges297 bronze badges

asked Aug 6, 2012 at 21:53

Write ac program to search the position of a substring within a string use string methods

3

The ptr pointer will point to the beginning of word, so you can just subtract the location of the sentence pointer, sntnc, from it:

pos = ptr - sntnc;

answered Aug 6, 2012 at 22:00

GingiGingi

2,0891 gold badge18 silver badges33 bronze badges

2

Just for reference:

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'.
    }
}

answered Apr 17, 2013 at 14:36

Write ac program to search the position of a substring within a string use string methods

xtrmxtrm

9369 silver badges22 bronze badges

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

pos=ptr-sntc;

This only works because sntc and ptr are pointers to the same string. To clarify when I say occurence it is the position of the first matching char when the matching string is found within your target string.

answered Aug 6, 2012 at 22:03

You can use this simple strpos modification

#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;
}

answered Nov 7, 2017 at 14:46

Howard JHoward J

4113 silver badges7 bronze badges

For some reasons I was having trouble with strstr(), and I also wanted index.

I made this function to find the position of substring inside a bigger string (if exists) otherwise return -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"); 
     }
 } 

answered Mar 24, 2014 at 4:55

Write ac program to search the position of a substring within a string use string methods

My comment to the ORIGINAL post in this thread: This declaration is INCORRECT:

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

C code would not even compile : it will fail on this line:

    ptr = strstr(sntnc,word);

So the line shall be changed to :

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

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

answered Sep 26, 2014 at 9:30

Write ac program to search the position of a substring within a string use string methods

derloderlo

491 bronze badge

How do you find the location of a substring in a string in C?

Find Index of Substring in String in C Language To find the index of given substring in a string, iterate over the indices of this string and check if there is match with the substring from this index of this string in each iteration.

How can you find a substring in a string?

You can get substring from the given String object by one of the two methods:.
public String substring(int startIndex): This method returns new String object containing the substring of the given string from specified startIndex (inclusive). ... .
public String substring(int startIndex, int endIndex):.

How do you find the position of the first occurrence of a substring in a string in C?

The function strstr returns the first occurrence of a string in another string. This means that strstr can be used to detect whether a string contains another string. In other words, whether a string is a substring of another string.

What is substring in string in C#?

In C#, Substring() is a string method. It is used to retrieve a substring from the current instance of the string. This method can be overloaded by passing the different number of parameters to it as follows: String. Substring(Int32) Method.