By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,514 Members | 1,843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,514 IT Pros & Developers. It's quick & easy.

how to find the number of occurence of a substring in a string?

P: 1
i've been asked to write a program to find the number of times that a given word occurs in a sentence.sentence and the word both are to be take as input from the user.
for example:

EnTER A SENTENCE: "the cat sat on the mat".
ENTER THE WORD :"the"

The word "the" occurs twice.
May 26 '07 #1
Share this Question
Share on Google+
2 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
So what do you have on this?

I'm sure you've read the Posting Guidelines, which ask you to post what you have tried, what error messages you are getting, or the specific part of code you're having trouble with.
May 26 '07 #2

blazedaces
100+
P: 284
i've been asked to write a program to find the number of times that a given word occurs in a sentence.sentence and the word both are to be take as input from the user.
for example:

EnTER A SENTENCE: "the cat sat on the mat".
ENTER THE WORD :"the"

The word "the" occurs twice.
How do you know? Honestly, the first step (and usually the hardest) is understanding the logic/reasoning of how someone/something might figure that out.

How did you know the word "the" occured twice? You might say you counted how many times you saw the word "the"... Ok, well now you know your program will have to do just that: count how many times a word appears.

You might ask, how would the computer do that? Don't... ask how you would/did? You might say, huh? I just counted how many times the word "the" appeared. Well how did you know the word "the" was appearing, I mean, if the word "that" appeared, would you count it? No, you would say, I would know because "that" is spelled differently... so what you're saying is that you know the word "the" appeared and not the word "that" because it's spelled "t h e".

See how this develops? This is the process you have to go through for every program you ever write.

Hope this helped and good luck...
-blazed
May 26 '07 #3

Post your reply

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