I need logic for the following program. please help me
Question :
Accept 2 strings and check how many times the second string is in the first string.
Example1:
Enter main string : This is a dog which is angry. This is a dog which eat much.
Enter string to check: This is a dog
output
2 times the 2nd string is present in the main string.
3 replies to this topic
#1
Posted 27 November 2011 - 09:35 AM
|
|
|
#2
Posted 27 November 2011 - 10:08 AM
Think about how you could do this by hand.
In all seriousness, the key to programming is figuring out the logic. If you don't learn how to do that, you'll never make it in coding.
My two sentence summary of the process:
See if the check-string is in the main-string. If it is, count, and check in the rest of the main string until you run out.
In all seriousness, the key to programming is figuring out the logic. If you don't learn how to do that, you'll never make it in coding.
My two sentence summary of the process:
See if the check-string is in the main-string. If it is, count, and check in the rest of the main string until you run out.
#3
Posted 28 November 2011 - 05:19 AM
Thank you. i am trying now...
WingedPanther said:
Think about how you could do this by hand.
In all seriousness, the key to programming is figuring out the logic. If you don't learn how to do that, you'll never make it in coding.
My two sentence summary of the process:
See if the check-string is in the main-string. If it is, count, and check in the rest of the main string until you run out.
In all seriousness, the key to programming is figuring out the logic. If you don't learn how to do that, you'll never make it in coding.
My two sentence summary of the process:
See if the check-string is in the main-string. If it is, count, and check in the rest of the main string until you run out.
#4
Posted 04 December 2011 - 06:04 AM
Basically you need to start from the beginning of the first string, and end at i < (l1 - l2) where l1 is the length of the first array, and l2 is that of the second. For every index(value of i - the outer loop variable), take a substring of the first string from the index to index + l2(substring() arguments for first string object), and check its equality with the second string(use equals() method of the substring). If it returns true, increment a counter. After the loop ends, display the value of the counter with message. You need to declare the counter befor the loop starts to avoid scope problems.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









