Last active
February 8, 2017 04:59
-
-
Save OlgaKulikova/07c74bee616a639bd253 to your computer and use it in GitHub Desktop.
Lesson6_2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.Lesson6; | |
// Дано текст и определенное слово. Посчитать сколько раз заданное слово встречается в тексте. | |
public class Lesson6_2 { | |
public static void main(String[] args) { | |
String text = "Ехал Грека через реку, видит Грека в реке рак, сунул Грека руку в реку, рак за руку Грека цап"; | |
String word = "Грека"; | |
int pos = 0, count = 0; | |
do { | |
pos = text.indexOf(word, pos); | |
if (pos >= 0) { | |
count ++; | |
pos += word.length(); | |
} | |
} while (pos >= 0); | |
System.out.println(count); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment