Program to match two given strings using function

This is the program which makes use of function matchany(s1,s2) which returns the first location in the string s1 where any character from the string s2 occurs, or -1 if s1 contains no character from s2.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "stdio.h"
#include "conio.h"
#include "string.h"
void main()
{
 int matchany(char str1[],char str2[]);//function declaration
 char str1[50],str2[50];
 int s;
 clrscr();
 printf("Enter string 1 and string 2\n");
 scanf("%s%s",&str1,&str2);
 s=matchany(str1,str2);
 if(s==-1)
 printf("\nNo match found");
 else
 printf("\nThe location where the first match occured is %d",s);
 getch();
}
 
int matchany(char str1[],char str2[])
{
 int i,j;
 for(i=0;i<strlen(str2);i++)
 {
  for(j=0;j<strlen(str1);j++)
  {
   if(str2[i]==str1[j])
   {
    return j+1;
   }
  }
 }
 return -1;
}

output 1:

Enter string 1 and string 2
hello
world
The location where the first match occured is 5

Output 2:

Enter string 1 and string 2
ice
smooth
No match found
Chitra
Chitra

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in