Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home C-tutorials C get Substring from String

Related Tutorials


 
 

Share on Google+Share on Google+

C get Substring from String

Advertisement
This section illustrates you how to get the substring using library function.

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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 6, 2009

Related Tutorials

Discuss: C get Substring from String  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com