Position of Substrings

How to determine different positions of substrings within a string in C?

There are numerous functions available in C to achieve the purpose of finding different positions of a substring with a string.

strstr():

To obtain the first occurrence of substring in a string the function used is strstr().

The syntax for this function is

char *strstr( const char *str1, const char *str2 );

The header file that must be included while using this function strstr() is <string.h>

In the above the function strstr() returns a pointer to the first occurrence of str2 in str1. If no match is found then NULL is returned.

strrstr():

This function is used to obtain the last occurrence of substring in a string.

The syntax for this function is

strrstr(const char *s, const char *subs );

The header file that must be included while using the function strrstr() is <sdgstd.h>

This returns a pointer which points to the last occurrence of a substring within another string. If the substring is not found, this will be the NULL pointer.

Editorial Team at Geekinterview is a team of HR and Career Advice members led by Chandra Vennapoosa.

Editorial Team – who has written posts on Online Learning.


Pin It