2 changed files with 85 additions and 17 deletions
@ -0,0 +1,68 @@ |
|||
public class MiniString { |
|||
private String str; |
|||
public MiniString(String str) { |
|||
this.str = str; |
|||
} |
|||
public String makeSubstring(int beginIndex) { |
|||
int length = this.str.length(); |
|||
String ret = ""; |
|||
for (int i = beginIndex; i < length; i++){ |
|||
ret += this.str.charAt(i); |
|||
} |
|||
return ret; |
|||
} |
|||
public String makeSubstring(int beginIndex, int endIndex) { |
|||
int length = this.str.length(); |
|||
String ret = ""; |
|||
for (int i = beginIndex; i < endIndex; i++){ |
|||
ret += this.str.charAt(i); |
|||
} |
|||
return ret; |
|||
} |
|||
public int findIndexOf(char ch) { |
|||
int ret = -1; |
|||
for (int i = 0; i < this.str.length(); i ++ ){ |
|||
if (this.str.charAt(i) == ch) { |
|||
ret = i; |
|||
break; |
|||
} |
|||
} |
|||
return ret; |
|||
} |
|||
public int findIndexOf(char ch, int fromIndex) { |
|||
int ret = -1; |
|||
for (int i = fromIndex; i < this.str.length(); i ++ ){ |
|||
if (this.str.charAt(i) == ch) { |
|||
ret = i; |
|||
break; |
|||
} |
|||
} |
|||
return ret; |
|||
} |
|||
public int findIndexOf(String str) { |
|||
int ret = -1; |
|||
int sub_length = str.length(); |
|||
String cur_str; |
|||
for (int i = 0; i < this.str.length() - sub_length; i++){ |
|||
cur_str = this.makeSubstring(i,i + sub_length); |
|||
if (cur_str.equals(str)) { |
|||
ret = i; |
|||
break; |
|||
} |
|||
} |
|||
return ret; |
|||
} |
|||
public int findIndexOf(String str, int fromIndex) { |
|||
int ret = -1; |
|||
int sub_length = str.length(); |
|||
String cur_str; |
|||
for (int i = fromIndex; i < this.str.length() - sub_length; i++){ |
|||
cur_str = this.makeSubstring(i,i + sub_length); |
|||
if (cur_str.equals(str)) { |
|||
ret = i; |
|||
break; |
|||
} |
|||
} |
|||
return ret; |
|||
} |
|||
} |
|||
@ -1,59 +1,59 @@ |
|||
/* |
|||
* NEIU CS207-2 Fall 2018 |
|||
* Homework 4 TestStringPractice.java |
|||
* This is the class with a main(), to test StringPractice |
|||
*/ |
|||
|
|||
// * NEIU CS207-2 Fall 2018 |
|||
// * Homework 4 TestMiniString.java |
|||
// * This is the class with a main(), to test MiniString |
|||
|
|||
public class TestStringPractice |
|||
{ |
|||
public static void main(String[] args) |
|||
{ |
|||
StringPractice s1 = new StringPractice("Hello world, welcome to the Java world."); |
|||
MiniString s1 = new MiniString("Hello world, welcome to the Java world."); |
|||
|
|||
|
|||
/* |
|||
System.out.println(s1.makeSubstring(0)); |
|||
System.out.println(s1.makeSubstring(6)); |
|||
System.out.println(s1.makeSubstring(6, 14)); |
|||
System.out.println(s1.makeSubstring(13)); |
|||
System.out.println(s1.makeSubstring(0, 12)); |
|||
System.out.println(); |
|||
*/ |
|||
|
|||
|
|||
/* |
|||
|
|||
System.out.println(s1.findIndexOf('w')); |
|||
System.out.println(s1.findIndexOf('m')); |
|||
System.out.println(s1.findIndexOf('x')); |
|||
System.out.println(s1.findIndexOf('J')); |
|||
System.out.println(s1.findIndexOf('a')); |
|||
System.out.println(); |
|||
*/ |
|||
|
|||
|
|||
/* |
|||
|
|||
System.out.println(s1.findIndexOf('w', 7)); |
|||
System.out.println(s1.findIndexOf('m', 20)); |
|||
System.out.println(s1.findIndexOf('x', 10)); |
|||
System.out.println(s1.findIndexOf('J', 30)); |
|||
System.out.println(s1.findIndexOf('a', 30)); |
|||
System.out.println(); |
|||
*/ |
|||
|
|||
|
|||
/* |
|||
|
|||
System.out.println(s1.findIndexOf("wo")); |
|||
System.out.println(s1.findIndexOf("we")); |
|||
System.out.println(s1.findIndexOf("He")); |
|||
System.out.println(s1.findIndexOf("Ja")); |
|||
System.out.println(s1.findIndexOf("va")); |
|||
System.out.println(); |
|||
*/ |
|||
|
|||
|
|||
/* |
|||
|
|||
System.out.println(s1.findIndexOf("wo", 7)); |
|||
System.out.println(s1.findIndexOf("we", 20)); |
|||
System.out.println(s1.findIndexOf("He", 10)); |
|||
System.out.println(s1.findIndexOf("Ja", 30)); |
|||
System.out.println(s1.findIndexOf("va", 25)); |
|||
System.out.println(); |
|||
*/ |
|||
|
|||
|
|||
} // End main() |
|||
|
|||
} // End class TestStringPractice |
|||
} // End class TestMiniString |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue