Sunday, July 8, 2012

Search Sub String individual

Q. Write a C program to accept two string from user, first is main string and second is searching sub string from main string, and count how many times its repeat individual in main string.
OR
Q. WAP to accept two string and find out how many times individual sub-string found in main string.


For example:


First main string: This is a good program
Second search string: is
Result: 1


Ans.


/*c program for search sub string from main string and counting how many times its repeat individual*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
 int i,j,count=0;
 char str[40],fstr[10];
 printf("Enter main string: ");
 gets(str);
 printf("Enter sub string: ");
 gets(fstr);
 for(i=0; str[i]!='\0'; i++)
 {
  if(str[i-1]==' ')
   if(str[i]==fstr[0])
   {
    for(j=0; str[i]==fstr[j] && fstr[j]!='\0'; i++,j++);
       i--;
    if(fstr[j]=='\0')
       count++;
   }
 }
 printf("%d",count);
 getch();
 return 0;
}


/***************Output***************/


Output of search individual sub string from main string C program
Screen shot for search sub-string individual from main string


Related Programs:

  1. Reverse all string
  2. Reverse all words but not string
  3. Reverse only first character of string & add extra word
  4. Display string vertically
  5. Search sub string from main string
  6. Change Title case of string
  7. Position and repetition of character in string

No comments:

Post a Comment