Start New Discussion within our Software Development Community
#include<stdio.h>
#include<string.h>

int compare_string(char *first, char *second)
{
   while(*first==*second)
   {
      if ( *first == '\0' || *second == '\0' )
         break;

      first++;
      second++;
   }
   if( *first == '\0' && *second == '\0' )
      return 0;
   else
      return -1;
}

int main(void)
{
  char string[1000];
  char word[20];
  char *s;
  int len;


/***************************************************************************
*                                                                          *
* This program input sentence from user and signle word                    *
* and count the occurance of that inputed word                             *
* Return counter and word                                                  *
* Author:             M. Hammad AKhtar                                     * 
*
****************************************************************************/

  printf("Enter the string:\n");
  gets(string);
  printf("Enter the word:\n");
  gets(word);
  len = strlen(string);
  int k=0;
  int count=0;
  s=string;
  while(*s !='\0')
  {
    k++;
    if(*s == ' ')
    {
      int res;
      int l = k-2;
      char cl[l];
      cl[l] = string;
      char ll[l];
      ll[l] = word;
      res = compare_string(cl,ll);
      if(res==0)
      {
        count++;
      }
    }
    s++;
  }
  printf("The word %s is occur %d times",word,count);
  return 0;
}
This article has been dead for over six months. Start a new discussion instead.