#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

main()
{
    char ch1[20],ch2[20],*temp[10];
    int i,j,count,k,p,q,len1,len2;

    printf("Enter the string\n");
    scanf("%s",ch1);

    printf("Enter the string to be found\n");
    scanf("%s",ch2);

    for(i=0;ch1[i]!='\0';i++);
    len1=i;

    for(j=0;ch2[j]!='\0';j++);
    len2=j;

    k=0;
    count=0;

    for(i=0;i<len1;i++)
    {
        if(ch1[i]==ch2[0])
        {
            temp = &ch1[i];
            k++;
        }
    }
    for(i=0;i<k;i++)
    {
        for(p=0;p<len1;p++)
        {
            for(q=0;q<k;q++)
            {
                if(*ch1[p]==*temp[q])
                {
                    for(j=0;j<len2;j++)
                    {
                        if(ch2[j]==ch1[p])
                            p++;
                        if(ch2[j+1]=='\0')
                        {
                            count++;
                            j=0;
                        }
                    }
                    if(ch2[j]!=ch1[p])
                        break;
                }
            }
        }
    }
    printf("\nThe number of times word appears is\n%d\n",&count);
}

Recommended Answers

All 2 Replies

#include<stdio.h>
#include<string.h>
int search(char[],char[]);
void main()
{
 char a[100],b[40];
 int loc;
 printf("\n Enter the main string :");
 gets(a);
 printf("\n Enter the search string :");
 gets(b);

 loc = search(a,b);

 if(loc==-1)
      printf("\nNot found");
 else
      printf("\nFound at location %d",loc+1);
 getch();
}

int search(char a[],char b[])
{
 int i,j,k;
 i=0,j=0;

 while(a[i]!='\0')
  {
     while(a[i]!=b[0] && a[i]!='\0')
         i++;
     if(a[i]=='\0')
        return(-1);     //search can not continue
     k=i;

     while(a[i]==b[j] && a[i]!='\0' && b[j]!='\0')
     {
     i++;
     j++;
     }

    if(b[j]=='\0')
       return(k);
    if(a[i]=='\0')
       return(-1);
    i=k+1;
    j=0;
  }
}

try this code

Are you posting code to show others, or do you have problems with that program that you need help with? If it's the latter, please ask a question. If it's the former, let me know and I'll turn this thread into a code snippet.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.