Skip to content

Instantly share code, notes, and snippets.

@OlgaKulikova
Last active February 8, 2017 04:59
Show Gist options
  • Save OlgaKulikova/07c74bee616a639bd253 to your computer and use it in GitHub Desktop.
Save OlgaKulikova/07c74bee616a639bd253 to your computer and use it in GitHub Desktop.
Lesson6_2
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