Reputation: 153
I'm following this online Java course, after every session there is few assignments, and I can't seem to solve this one.
I know that's not the point of an exercise, but I tried everything and I can't see any other way.
Write a program that returns number of occurrences of a string in another string. For example:
Input:
First String: the
Second String: the students are working hard in the faculty of Engineering because **the**y love it
Output:
3
Note: You should use nested loops. Don’t use some methods like indexOf or substring
This is my code so far, I know it's not complete, and I'm pretty sure most of this decisions statements aren't needed.
Edit: This is a C# code, I was testing on both Java and C#
String Text1 = Console.ReadLine();
String Text2 = Console.ReadLine();
int counter = 0;
for (int y = 0; y < Text1.Length; y++)
{
for (int x = 0; x < Text2.Length; x++)
{
if (Text1[y] == Text2[x])
{
if (y + 1 < Text1.Length && x + 1 < Text2.Length)
{
if (Text1[y + 1] == Text2[x + 1])
{
}
}
}
}
}
Upvotes: 1
Views: 2855
Reputation: 425
@Mody- As this is an assignment, I suggest you to follow "public int indexOf(String str)" method in String.class and replicate something similar with little modification to solve your problem.
Upvotes: 0
Reputation: 726599
Since you are working on an exercise, I wouldn't fix your code, but help you fix it yourself.
First, some basics: in Java you use charAt(i)
to access an individual character. For example,
char ch = Text1.charAt(3);
gets you the forth character from the beginning of the string (indexes are zero-based).
Now for the algorithm: you want to iterate through all positions of the second string with enough characters after it to make a match with the first string possible. This means that the index goes from zero, inclusive, to the position Text2.length()-Text1.length()
, exclusive. This is the starting position of the potential match - let's call it p
.
Given a position p
, you want to iterate through all characters of Text1
starting at zero, and compare them to Text2
characters starting with the character at position p
. Let's say the index in Text1
is called i
, and it goes from zero, inclusive, to Text1.length()
, exclusive. Then you want to compare Text1.getChar(i)
for equality with Text1.charAt(i+p)
.
You need to finish the comparison with all characters of Text1
before you declare a match. You need to know the result in the outer loop, so you want to use a bool
flag. Set the flag to true
before the inner loop. If you see a mismatch, set the flag to false
, and break out of the loop.
If the flag is true
after the inner loop, increment the counter.
Upvotes: 1
Reputation: 1440
public static int count(String text1, String text2)
{
int res = 0;
for (int i=0;i<text2.length()-text1.length();i++)
{
if (text1.charAt(0) == text2.charAt(i))
{
boolean found = true;
for (int j=0;j<text1.length() && found;j++)
if (text1.charAt(j) != text2.charAt(i+j))
found = false;
if (found)
res++;
}
}
return res;
}
Upvotes: 1