C get Substring from String

This section illustrates you how to get the substring using library function.
You can see in the given example, we have used the library function strstr()
provided by the header file <string.h>. The function is declared
as:
char *strstr(const char *str1, const char *str2);
In the given code, we have consider will as str2 and the whole string as
str1. The function strstr() finds the occurrence of str2 in the
str1 and returns
the pointer to occurrence of str2 in str1. If no match found, then a null pointer
is returned.
Here is the code:
SUB.C
#include <string.h>
#include <stdio.h>
#include <conio.h>
void main() {
char *ch;
clrscr();
ch = strstr("Where there is will, there is a way.", "will");
printf("The substring is: %s", ch);
getch();
}
|
Output will be displayed as:
SUB.EXE

Download Source Code

|