Write a C program that displays the position or index in the string S where the string T begins, or – 1 if S doesn’t contain T.
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(); }
Input & Output:
1.Enter the first string: kali Enter the string to be searched: li second string is found in the first string at 2 position 2.Enter the first string: nagaraju Enter the string to be searched: raju second string is found in the first string at 4 position 3.Enter the first string: nagarjuna Enter the string to be searched: ma -1
-
UpdatedDec 25, 2014
-
Views23,475
You May Like
Write a C program to find the roots of a quadratic equation
.
Write a C program to count the lines, words and characters in a given text.
Write a C program to find the sum of individual digits of a positive integer.
Write a C program, which takes two integer operands and one operator from the user, performs the operation and then prints the result. (Consider the operators +,-,*, /, % and use switch statement
)
Write a C program to generate all the prime numbers
between 1 and n, where n is a value supplied by the user.
Write C programs that use both recursive
and non-recursive functions
- To find the
factorial
of a given integer. - To find the
GCD
(greatest common divisor) of two given integers.