Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

Program to search for a string into another string

Posted By: Adalhard Fischer     Category: C Programming     Views: 4518

Program to search for a string into another string.

Code for Program to search for a string into another string in C Programming

#include <stdio.h>
#include <conio.h>
#include <string.h>

int xstrsearch ( char *, char * ) ;
void show( ) ;

void main( )
{
    char s1[ ] = "NagpurKicit" ;
    char s2[ ] = "Kicit" ;
    int pos ;

    clrscr( ) ;

    printf ( "String s1: %s\n", s1 ) ;

    printf ( "String s2: %s\n", s2 ) ;

    /* search if s2 is present in s1 */
pos = xstrsearch ( s1, s2 ) ; printf ( "\nThe pattern string is found at position: %d\n", pos ) ; getch( ) ; } /* searches for the given pattern s2 into the string s1 */
int xstrsearch ( char * s1, char * s2 ) { int i, j, k ; int l1 = strlen ( s1 ) ; int l2 = strlen ( s2 ) ; for ( i = 0 ; i <= l1 - l2 ; i++ ) { j = 0 ; k = i ; while ( ( s1[k] == s2[j] ) && ( j < l2 ) ) { k++ ; j++ ; } if ( j == l2 ) return i ; } return -1 ; }
  
Share: 


Didn't find what you were looking for? Find more on Program to search for a string into another string Or get search suggestion and latest updates.

Adalhard Fischer
Adalhard Fischer author of Program to search for a string into another string is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!