-1

I'm trying to solve this problem but it doesn't seem to work in any way. I tried to search for the problem but all other answers use methods or arrays and we haven't studied them yet so I have to solve the problem with loops only. This is the question:

Write a program that returns number of occurrences of a string in another string.
E.g.
Input:
First String: the
Second String: the students are working hard in the faculty of Engineering because they love it
Output:
3

this is what I tried:

    Scanner input = new Scanner(System.in); 
    String s1 = input.nextLine();
    String s2 = input.nextLine();
    int count = 0;

    String text="";

    for(int i = 0; i<s1.length(); i++){
        for(int j =0; j<=s2.length() ; j++){
            if(s1.charAt(i)==s2.charAt(j) && i ==0){
                for(int m=1; m<s1.length();m++){
                    if(s1.charAt(i+1)==s2.charAt(j+1)){
                        if(m==s1.length()){
                            count++;
                        }else{
                            break;
                        }
                    }
                }
            }
        }
    }

    System.out.println(count);

I think I have a problem with the logic. This is what I'm thinking: the program should check the first letter of the first string then check if it checks if it equals the first letter of the second string, then if it doesn't equal it then the program should loop through the second string until it finds a letter that equals the first letter of the first string, if it finds a letter that equals the first string it checks if the second letter of the first string equals the next letter of the other string, if yes then it checks if thats the last letter of the first string if it is then count increases by one, if it isn't it loops the the next characters. I tried to do it a couple of other ways non of my code worked. Can someone help me with the logic?

Markus Mitterauer
  • 1,560
  • 1
  • 14
  • 28
maya91
  • 47
  • 9
  • 6
    Have you tried debugging your program? Because that's what you should do. – f1sh Jan 03 '17 at 17:01
  • @f1sh I don't know how to debug this is my first course taking programming, but I don't get any errors in the code unless I run it, it give index out of bound exception – maya91 Jan 03 '17 at 17:04
  • Might worth looking at http://stackoverflow.com/questions/275944/java-how-do-i-count-the-number-of-occurrences-of-a-char-in-a-string – Badran Jan 03 '17 at 17:04
  • You did it in O(n^2), you can do it O(n) – Alon Jan 03 '17 at 17:07
  • @PatrickParker I came across that question but as I mentioned I need to solve it without arrays and the solution provided in that question uses arrays – maya91 Jan 03 '17 at 17:09
  • 1
    @maya91 here's another one that doesn't use arrays http://stackoverflow.com/a/6267655/7098259 – Patrick Parker Jan 03 '17 at 17:11
  • https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/StringUtils.java#L6783 and https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/CharSequenceUtils.java#L70 – Alexandre Cartapanis Jan 03 '17 at 17:29
  • Thank you for the links but all these use methods and things that I haven't heard of before. The solution should be using nested loops – maya91 Jan 03 '17 at 17:39

1 Answers1

1

the program should check the first letter of the first string then check if it checks if it equals the first letter of the second string,

OK

then if it doesn't equal it then the program should loop through the second string until it finds a letter that equals the first letter of the first string,

If that's what you want to do, then traversing s2 should be the basis of your outermost loop. But you have written it with traversing s1 as the outermost loop.

if it finds a letter that equals the first string it checks if the second letter of the first string equals the next letter of the other string, if yes then it checks if thats the last letter of the first string if it is then count increases by one, if it isn't it loops the the next characters.

OK. so you only need one index into s2 (the outer loop), and one index into the potential match with s1 (the inner loop). You don't need another loop beyond that.

A final note, be careful that the sum of your outer loop index (traversing s2) plus the inner index (traversing potential match) does not exceed the bounds of s2. As you have written it now, it can exceed the string lengths, and in fact it does.

String s1 = "the";
String s2 = "the students are working hard in the faculty of Engineering because they love it";
int count = 0;
assert(!s1.isEmpty());
int s2LastPositionOfPotentialMatch = s2.length() - s1.length();
for(int i = 0; i < s2LastPositionOfPotentialMatch + 1; i++){
    for(int j = 0; j < s1.length(); j++) {
        if(s1.charAt(j) != s2.charAt(i+j)) {
            break;
        } else if(j + 1 == s1.length()) {
            count++;
        }
    }
}
System.out.println(count);  
Patrick Parker
  • 4,863
  • 4
  • 19
  • 51