package PACKAGE1;
import java.io.*;
public class subStringCheck {
static String strng;
static char st[];
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the String");
strng = br.readLine();
st = strng.toCharArray();
subStringCheck obj = new subStringCheck();
System.out.println("Enter the sub string");
obj.substring(br.readLine());
}
public void substring(String str) {
char[] s1 = str.toCharArray();
int length = str.length();
int i = -1;
int count = 0;
int noOfTimes = 0;
boolean repeated = false;
while (i < (strng.length() - 1)) {
for (int s = 0; s < str.length(); s++) {
i++;
if (st[i] == s1[s]) {
count++;
if (count == length) {
noOfTimes++;
repeated = true;
break;
}
} else {
count = 0;
break;
}
}
}
if (repeated == true) {
System.out.println("The substring has repeated " + noOfTimes
+ " times");
} else {
System.out.println("Theere is no substring");
}
}
}
Tuesday, 28 January 2014
JAVA: Count the number of instances of substring within a string without using subString function
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment