A simple program that I wrote to search a string or a word to another string using C language. The code is very simple and easy to understand.
Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com.
My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.
My mobile number here in the Philippines is 09173084360.
Program Listing
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
int xstrsearch ( char *, char * ) ;
void show( ) ;
int main( )
{
char s1[ ] = "PomperadaJake" ;
char s2[ ] = "Jake" ;
int pos ;
system("cls");
printf("Search a String To Another String in C")
printf("\n\n");
printf ( "String One: %s\n", s1 ) ;
printf ( "String Two: %s\n", s2 ) ;
pos = xstrsearch ( s1, s2 ) ;
printf ( "\nThe pattern string is found at position: %d\n", pos ) ;
getch( ) ;
}
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 ;
}
No comments:
Post a Comment