C
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.

| How to get a substring if the positions are indicated? For example, if I want to extract "viv" from the word "vivek"? |

| Try assigning the string to a pointer and try the pointer traversal to get the string with desired position |

|
#include #include int main(){ char *s="vivek"; char *t; int i; clrscr() for(i=0;i<3;i++){ t[i]=s[i]; } t[i]='n'; printf("%s",t); getch(); return 0; } |

|
#include #include int main(){ char *s="vivek"; char *t; int i; clrscr() for(i=0;i<3;i++){ t[i]=s[i]; } t[i]='n'; printf("%s",t); getch(); return 0; } |