Write a C program that displays the position of a character ch in
the string S or – 1 if S doesn„t contain ch.
Algorithm:
Step 1: Start
Step 2: read the string and then
displayed
Step 3: read the string to be searched and then displayed
Step 4: searching the string T in string S and then perform the
following steps
i.
found = strstr(S, T)
ii.
if found print the second string is found in the first string at the
position. If not goto step5
Step 5: print the -1
Step 6: Stop
Program:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s[30], t[20];
char *found;
clrscr();
puts("Enter the first string: ");
gets(s);
puts("Enter the string to be searched: ");
gets(t);
found = strstr(s, t);
if(found)
{
printf("Second String is found in the First String at %d position.\n", found - s);
}
else
{
printf("-1");
}
getch();
}
OUTPUT:
Enter the first string:
hello
Enter the string to be searched:
l
Second String is found in the First String at 2 position.
Comments
Post a Comment