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 step 5
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();
/* Entering the main string */
puts("Enter the first string: ");
gets(s);
/* Entering the string whose
position or index to be displayed */
puts("Enter the string to be searched: ");
gets(t);
/*Searching string t in string s */
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:
1.enter the first string:
kali
Enter the string to
be seareched:
li
second string is
found in the first string at2position
2.enter the first
string:
nagaraju
Enter the string
to be seareched:
raju
second string is
found in the first string at4position
3.enter the first string:
nagarjuna
Enter the string
to be seareched:
ma
-1
Conclusion:
The program is error free
VIVA QUESATIONS:
1) What
is the difference between printf() and
puts() ?
Ans: puts() is used to display the
string at a time and it doesn’t take any
integers values but printf() takes any values as defined by the user
2) define
pointer variable ?
Ans: pointer variables are defined as variable that contain the memory
addresses of data or executable code.
3) What
is use of the strcmp() function ?
Ans: This function compares two
strings character by character and returns a value 0 if both strings are equal
and non zero value if the strings are different.
No comments:
Post a Comment