Friday, 26 September 2014

Number Of Occurence

One Of very commonly asked problem in Job Interviews is To find number of occurrence of given word in given paragraph . This very easy question but people still make mistakes .

How to do it  :-

  • Find first occurrence of first character of word in paragraph 
  • If found >>  from that point check next character of word match with next character of paragraph
  • Check until last character of word match with consecutive next character of paragraph
  • If all character match increment  count by 1.
  • Repeat all above statement but now search perform on paragraph started at index of end of last character until which our search is completed 

C++ Program to find no of Occurrence :-

  1. /*
  2.  To check Occurrence of Given word in Given Paragraph
  3.  >>Case sensitive
  4. */
  5. #include<iostream>
  6. #include<string.h>
  7. using namespace std;
  8. main(){
  9.         char para[500];
  10.         char word[20];
  11.         int i,j,flag,count=0;
  12.         cout<<"Enter Paragraph String : ";
  13.         gets(para);
  14.         cout<<"Enter Word to search : ";
  15.         gets(word);
  16.        
  17.         i=0;
  18.         while(i<strlen(para)){
  19.                 if(word[0]==para[i]){
  20.                 // if first element of word found check for rest of element
  21.                 // of word at consecutive position if all match found increment count
  22.                         i++;              
  23.                         j=1;
  24.                         flag=1; //flag == True
  25.                         while(j<strlen(word)){
  26.                                 if(word[j]!=para[i]){
  27.                                         flag=0; //Mismatch found So flag=False
  28.                                         break;  
  29.                                 }
  30.                                 i++;
  31.                                 j++;
  32.                         }
  33.                         if(flag) //if flag==True
  34.                            count++;
  35.                 }
  36.                 else
  37.                   i++;
  38.         }
  39.         cout<<"No of Occurence :: "<<count;
  40.         return 0;
  41. }

Note :-  We can use this technique  to find all URL in a given webpage  


Python >> Find Number of Occurrences :-

  1. # Find Occurrence Of Given Word In Given Paragraph
  2. # case-Insensitive
  3. def find_occ(para,word):
  4.     '''
  5.     Find Occurrence of word in para and return no of occurrence
  6.     ->case Insensitive Search
  7.     (string,string)->int
  8.    
  9.    '''
  10.     Occurrence=0
  11.     para=para.lower() #all in lowercase
  12.     word=word.lower()
  13.     index=-1
  14.     while True:
  15.         index=para.find(word,index+1) #if next word is not found return -1
  16.         if index!=-1:
  17.             Occurrence +=1
  18.         else:
  19.             break
  20.     return Occurrence
  21. if __name__=='__main__':
  22.     para=raw_input('Enter Paragraph :')
  23.     word=raw_input("Enter word to Search :")
  24.     print 'Occurence of "',word,'" in given paragraph is ::',find_occ(para,word)
  25.     raw_input('\n\nhttp://beginer2cs.blogspot.com/')

Output:-

Occurence of Given word in string using python and c++

2 comments:

  1. Hello very cool site!! Guy .. Beautiful .. Amazing ..
    I'll bookmark your blog and take the feeds also࿦I am satisfied to find numerous useful
    info right here in the publish, we'd like develop more strategies in this regard, thanks for sharing.
    . . . . .

    Here is my blog - book of ra online, www.bookofraspieler.com,

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete

THANKS FOR UR GREAT COMMENT

Blogger Widgets