
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
What is strstr() Function in C language?
The C library function char *strstr(const char *haystack, const char *needle) function finds the first occurrence of the substring needle in the string haystack. The terminating '\0' characters are not compared.
An array of characters is called a string.
Declaration
The syntax for declaring an array is as follows −
char stringname [size];
For example − char string[50]; string of length 50 characters
Initialization
- Using single character constant −
char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}
- Using string constants −
char string[10] = "Hello":;
Accessing − There is a control string "%s" used for accessing the string till it encounters ‘\0’.
The strstr() Function
It is used to search whether a substring is present in the main string or not.
It returns pointer to first occurrence of s2 in s1.
Syntax
The syntax for strstr() function is as follows −
strstr(mainsring,substring);
Example
The following program shows the usage of strstr() function.
#include<stdio.h> void main(){ char a[30],b[30]; char *found; printf("Enter a string:
"); gets(a); printf("Enter the string to be searched for:
"); gets(b); found=strstr(a,b); if(found) printf("%s is found in %s in %d position",a,b,found-a); else printf("-1 since the string is not found"); }
Output
When the above program is executed, it produces the following result −
Enter a string: how are you Enter the string to be searched for: you you is found in 8 position
Example 2
Let’s see another program on strstr() function.
Given below is a C program to find, if a string is present in another string as substring by using strstr library function −
#include<stdio.h> #include<string.h> void main(){ //Declaring two strings// char mainstring[50],substring[50]; char *exists; //Reading strings// printf("Enter the main string :
"); gets(mainstring); printf("Enter the sub string you would want to check if exists in main string :"); gets(substring); //Searching for sub string in main string using library function// exists = strstr(mainstring,substring); //Conditions// if(exists){ printf("%s exists in %s ",substring,mainstring); } else { printf("'%s' is not present in '%s'",substring,mainstring); } }
Output
When the above program is executed, it produces the following result −
Enter the main string : TutorialsPoint c Programming Enter the sub string you would want to check if exists in main string :Programming Programming exists in TutorialsPoint c Programming
- Related Articles
- strstr() function in C/C++
- strstr() function in PHP
- What is strlen function in C language?
- What is strcoll() Function in C language?
- What is strspn() Function in C language?
- What is strcpy() Function in C language?
- What is strcat() Function in C language?
- What is strncat() Function in C language?
- What is strcmp() Function in C language?
- What is strncmp() Function in C language?
- What is strncpy() Function in C language?
- What is strrev() Function in C language?
- What is function prototype in C language
- What is exit() function in C language?
- What is strtok() function in C language?
