Skip to main content

PROGRAMMING FOR PROBLEM SOLVING LAB : Program 39

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

Popular posts from this blog

Learning and Development Interview Questions and answers for Mathematics-1.

  1). What is Mean, Mode and Median? Solution Answer: The mean is the average of a  collection of numbers or terms in a sequence. To calculate the mean use a formula is sum of total terms divided  by number of terms. The mode  is the most f requent number or term in a sequence. It means the number that occurred  highest number of times  in a sequence. To find the mode arrange the numbers in ascending or descending order and verify which number repeated most number of times in a sorted sequence. The median is the middle number/term where the sequence is arranged in ascending or descending order. If the sorted sequence have odd number of terms then  divide by 2 and round up to get the position of the median number.  If the  sorted sequence  have  even  number of terms then  divide by 2  to get the position of the median number.  2 ). What is the Difference between Fractional and Rational number? Solu...