Jump to content

need logic for the program

- - - - -

  • Please log in to reply
3 replies to this topic

#1
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
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.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
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.


#4
Ritwik I the programmer

Ritwik I the programmer

    Newbie

  • Members
  • PipPip
  • 17 posts
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