471,873 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

counting frequency of a substring in a string

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.

Jul 24 '06 #1
11 9813
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.


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
9,065 Expert Mod 8TB
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
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
9,065 Expert Mod 8TB
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


remember if you have a string declared

char string[15];

and it contains the string "Hello World!" then

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

Jul 25 '06 #5
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
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)

<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) >
<cfset qty = 0 >
Jan 15 '09 #7
542 512MB
As Banfa says:
Expand|Select|Wrap|Line Numbers
  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;}
This of course wouldn`t work but you get the idea?
Jan 15 '09 #8
542 512MB
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
  2. if(array[i]==char(105) && array[i-1)==char(72))count++;
.....should count the instances of 'Hi'.
I hope this is not classified as spoon feeding but ammends must be made.
Jan 16 '09 #9
11,448 Expert 8TB
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,

Jan 16 '09 #10
542 512MB
Thanks JosAH....I wasn`t aware so will do so in future
Jan 17 '09 #11
151 100+
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

Post your reply

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

Similar topics

5 posts views Thread by cassandra.flowers | last post: by
4 posts views Thread by Victor Engmark | last post: by
5 posts views Thread by Anders K. Jacobsen [DK] | last post: by
7 posts views Thread by Chris Lasher | last post: by
27 posts views Thread by Simon Biber | last post: by
12 posts views Thread by KWSW | last post: by
reply views Thread by zermasroor | last post: by

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.