473,387 Members | 3,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

count the number of occurences of a string in another string

This could be non contiguous. As an example
string1: "Hello"
string2: "Heelblloo"
occurences of string 1 in string2 is 2*3c2*2=12
among 2 e's in string 2, we can select only one and thus 2 possible ways.similarly for l and o. I am trying hard to implement it in the C programming,but getting messed up using dynamic programming to find the proper logic. suggestions please
Dec 16 '14 #1
7 1589
weaknessforcats
9,208 Expert Mod 8TB
rite a loop that arches down your target string a character at a time.

Next, modify the loop s it terminates when it reaches strlen(searchstring).


Next, inside the loop call strstr using your current position in the target string and your search string.

Use the return from strstr to see if you have an occurrence.
Dec 16 '14 #2
thanks for your response.I doubt that strstr function only compares the string as it is in the target string where as here in the example, the characters in the string need not be contiguous.I am new to strstr function and never used it. If my claim is wrong, please elaborate your suggestion with the code.
Dec 16 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
OK. Before I go on with more logic, I need to be sure of the problem conditions. What does:

Expand|Select|Wrap|Line Numbers
  1. string1: "Hello"
  2.  string2: "Heelblloo"
  3.  occurences of string 1 in string2 is 2*3c2*2=12
mean? I see only one occurrence of Hello because there is only one H in string1.
Dec 16 '14 #4
I will correct my title to "number of ways to form string1 from string2"
1) H can be found in 1st location of string2.Hence that should form as a part of the solution.
2) 'e' of string1 can be formed by selecting either of 'e' in 2nd and 3rd position from string2. thus 2 possible ways.
3) Thus we formed "He". "ll" can be formed by selecting "ll" from (4,6),(4,7) and (6,7) Thus 3c2 ways as we need two l's from 3. we can safely ignore 'b' as it does not form the solution.
4) similarly one 'o' can be selected from two 'o's at the final and last but one position..
5) Multiplying all will give final answer.

Note: The string2 should be scanned sequentially.
suggest me the best way to implement it.
Dec 17 '14 #5
weaknessforcats
9,208 Expert Mod 8TB
The easiest thing is to extract all the letters from string2 that occur in string1.

You would then need to find all combinations of these letters taken n at a time where n is the number of letters in string1.

After each combination you would compare the combination to the string1.

Eventually all matching combinations would appear.
Dec 18 '14 #6
I tried the approach, and it is giving the correct answer,until the character is repeating in the adjacent locations with O(n) complexity. But the code is failing if the string2 has repeated characters with some intermediate new characters like "hellbblaobllboo".

Expand|Select|Wrap|Line Numbers
  1. while(str2[k]!='\0' || str1[j]!='\0' )
  2.             {
  3.                 if(str1[j]==str2[k])
  4.                 {
  5.                     count2[j]+=1;
  6.                     if(str2[k]!='\0')
  7.                         k++;
  8.                 }
  9.                 else if (j==8) break;// end of string
  10.                 else j++;
  11.  
  12.             }
  13. for(j=0;j<8;j++)
  14.                 product=product*combination(count2[j],count1[j]);
  15. int combination(int n,int r)
  16. {
  17.     return fact(n)/(fact(n-r)*fact(r));
  18. }
How to fix this bug or could it done by dynamic programming?
Dec 20 '14 #7
weaknessforcats
9,208 Expert Mod 8TB
I was thinking more along the lines of:


Expand|Select|Wrap|Line Numbers
  1. if (!strcmp(string1,combo))
  2. {
  3.     increment match count
  4. }
Dec 20 '14 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: DataFreakFromUtah | last post by:
Hello! No question here, just a procedure for the archive. Search critera: count records imported count data imported count number of rows imported count number of records imported record import...
4
by: Jason Gleason | last post by:
What's the most efficient way to get the number of occurences of a certain string in another string..for instance i'm using the following code right now... private int CharacterCounter(String...
7
by: P. Schmidt-Volkmar | last post by:
Hi there, I have a string in which I want to calculate how often the character ';' occurs. If the character does not occur 42 times, the ";" should be added so the 42 are reached. My...
3
by: M.N.A.Smadi | last post by:
hi; say i have a text file with a string ( say '(XYZ)') and I want to find the number of line where this string has occured. What is the best way to do that? what about if that string was say a...
24
by: Derek Hart | last post by:
Is there an efficient line of code to count the number of instances of one string within another. If I have the sentence: "I want to go to the park, and then go home." It would give me a...
11
by: Mack | last post by:
Hi all, I want to write a program to count number of bits set in a number. The condition is we should not loop through each bit to find whether its set or not. Thanks in advance, -Mukesh
3
by: thulaseeram | last post by:
Hi, How to find out, the number of objects created to a particular class. Thanks, Ram
2
by: mfaisalwarraich | last post by:
Hi Everybody, I am using the following code to get the recordset of an external database. Dim dbPatients As Database Dim rsCountPatients As Recordset ' to count number of...
1
by: jlt206 | last post by:
This code <?php include("counter.php")?> on the webpage produces the count number. (function code below) I want to place the current number into a variable $MemberNo or into a FormField to be sent...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.