473,503 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

counting frequency of a substring in a string

2 New Member
Hi,
I am trying to count the number of substrings within a string. I am fairly new to programming and need a headstart. Any info would be appreciated.

Thanks,
A
Jul 24 '06 #1
11 10051
Ashish_CPP
35 New Member
Hi,
I am trying to count the number of substrings within a string. I am fairly new to programming and need a headstart. Any info would be appreciated.

Thanks,
A

Can you elobrate your question, I m unable to understand what you mean by substring and on what basis you want to search it.
Jul 25 '06 #2
Banfa
9,065 Recognized Expert Moderator Expert
Well a simple method would be to start by finding each occurance of the first character of the substring in the string, then at each occurance check to see if the substring exists there.
Jul 25 '06 #3
b_or_not2c
2 New Member
I want to check how many times the word Hi has occured in the sentence
"Hi I feel really High when I visit High Mountains"
Jul 25 '06 #4
Banfa
9,065 Recognized Expert Moderator Expert
So you want a result of 1?

Like I said make an attempt and post it, we'll help you any problems you are having with it.

You may wish to use functions like

strncmp
memcmp

remember if you have a string declared

char string[15];

and it contains the string "Hello World!" then

string[0] == 'H'
string[1] == 'e'

etc.
Jul 25 '06 #5
Ashish_CPP
35 New Member
U can have a string say, "Hi", and memcmp it with from the start of each letter of the main string, I suggest u not to use strcmp becoz if at all ur string contains a numerical zero then u could end up with errors.

U can compare ur string like this in a for loop:-

memcmp( &Mainstr[i], "Hi", 2);
Jul 26 '06 #6
michaelmckavanagh
1 New Member
This example uses coldfusion but the same technique can be used in most languages... I simply compare the length of the original string with the length of the string with my search string stripped out. I then divide the difference by the length of the search string. This is a fast and neat technique. (Note: The algorithm will find 3 occurrences of "IS" which is correct, if you want to use spaces like this, " IS ", it will find 2 occurrences)

MichaelMcKavanagh
<email removed>


<cfset originalStr = " THIS IS A FUNNY TEST IS IT NOT " >
<cfset searchStr = "IS" >

<cfif len(searchStr) GT 0>
<cfset qty = (len(originalStr)-len(replace(originalStr,searchStr,"","ALL")))/len(searchStr) >
<cfelse>
<cfset qty = 0 >
</cfif>
Jan 15 '09 #7
whodgson
542 Contributor
As Banfa says:
Expand|Select|Wrap|Line Numbers
  1.  
  2. for(int i=0;i<?;i++)//scan every second element of the string
  3. {flag=0;
  4. if(array[i]=="H")flag=1;
  5. i++;
  6. if(flag==1 && array[i]=='i')count++;
  7. cout<<"There are  "<<count<<"instances of 'Hi' in the string"<<endl;}
  8.  
This of course wouldn`t work but you get the idea?
Jan 15 '09 #8
whodgson
542 Contributor
Now that I`ve had another look at this, my thread # 8 is very unhelpfull (or plain wrong) and should be scratched
If the string is in a char array and you traverse it with a for loop, the statement in the body of this loop:
Expand|Select|Wrap|Line Numbers
  1.  
  2. if(array[i]==char(105) && array[i-1)==char(72))count++;
  3.  
.....should count the instances of 'Hi'.
I hope this is not classified as spoon feeding but ammends must be made.
Jan 16 '09 #9
JosAH
11,448 Recognized Expert MVP
@whodgson
Why not simply use 'H' and 'i' instead of those magic numbers? Your code will fail miserably on any non ASCII encoding computer. Don't use magic numbers, never.

kind regards,

Jos
Jan 16 '09 #10
whodgson
542 Contributor
Thanks JosAH....I wasn`t aware so will do so in future
Jan 17 '09 #11
Man4ish
151 New Member
simple roll your char array and keep track of count of H and i coming together (i after H ) ,that's all
Jan 17 '09 #12

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

Similar topics

5
6716
by: cassandra.flowers | last post by:
Hi, I have another string handling question for the group, since you have all been so helpful in the past. Thank you. Basically, I want to do something really simple: Search a main...
4
6557
by: Victor Engmark | last post by:
When looking for a method to fetch unique elements and counting the number of occurences of each of them, I found quite a lot of gross examples of complex XSL. But after realizing the subtle...
3
2408
by: sparks | last post by:
Besides doing a loop is there a command that will give the number of occurreneces of a chr in a string? The only way I can think of is to do a while loop and count variable. thanks for info ...
5
2837
by: Anders K. Jacobsen [DK] | last post by:
Hi We have a rather large asp.net project with serveral utility projects (written in C#). Is there at tool out there which can give an estimate of the total amount of code lines all projects...
7
4171
by: Chris Lasher | last post by:
Hi all, How can one count all the permutations of a substring in a string? For a more concrete example, let's say targetstr = 'AAA' and probestr = 'AA' I want to consider how many times one...
27
5561
by: Simon Biber | last post by:
I was reading http://en.wikipedia.org/wiki/Poker_probability which has a good description of how to count the frequency of different types of poker hands using a mathematical approach. A sample...
8
4283
by: writeanand | last post by:
How can I count the frequency of words in a ASCII File using STL? a) I dont know what words will be found in the file b) The max number of occurrences is 10,000 per word (in case that matters) ...
12
3856
by: KWSW | last post by:
Got this assignment due a few weeks later and since I am done with the up coming assignment, decided to try out the next one early rather than rush later. First part of it requires me to do a...
4
2028
by: sandvet03 | last post by:
Im a newby to programing 2 months exp.. I am trying to write code in C to take a input string from the user and then the input substring. I wish to compare the substring to the string and count the...
0
7074
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
7273
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,...
1
6982
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5572
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.