In: Computer Science
JAVA
package stringPractice1068;
public class SomePracticeStringMethods {
/* returns true if c is a punctuation mark or false otherwise
*
* Punctuation mark is defined as:
* apostrophe '
* comma ,
* period .
* semicolon ;
* colon :
* exclamation point !
* question mark ?
*
* (You don't have to worry about any others)
*/
public static boolean isPunct(char c) {
/* placeholder just so that the function
* compiles. fill in your implementation here.
*
* there is a similar placeholder
* for each of the remaining functions */
return true;
}
/*
* returns the index of the first punctuation mark in s or
* -1 if s contains no punctuation marks
*/
public static int indexOfFirstPunct(String s) {
return -1;
}
/*
* returns the index of the first occurrence of a punctuation mark
in s starting
* from index startPosition or -1 if there are none at index
* startPosition or later. Notice that this method has the same name
as the
* previous one, but that it takes a different number of arguments.
This is
* perfectly legal in Java. It's called "method overloading"
*/
public static int indexOfFirstPunct(String s, int startPosition)
{
return -1;
}
/*
* returns the index of the last occurrence of a punctuation mark in
s or -1 if s
* contains none
*/
public static int indexOfLastPunct(String s) {
return -1;
}
/*
* returns s in reverse. For example, if s is "Apple", the method
returns the
* String "elppA"
*/
package stringPractice1068; public class SomePracticeStringMethods { public static boolean isPunct(char c) { return c == '\'' || c == ',' || c == '.' || c == ';' || c == ':' || c == '!' || c == '?'; } public static int indexOfFirstPunct(String s) { return indexOfFirstPunct(s, 0); } public static int indexOfFirstPunct(String s, int startPosition) { for(int i = startPosition; i < s.length(); ++i) { if(isPunct(s.charAt(i))) { return i; } } return -1; } public static int indexOfLastPunct(String s) { return indexOfLastPunct(s, s.length()-1); } public static int indexOfLastPunct(String s, int endPosition) { for(int i = endPosition; i >= 0; --i) { if(isPunct(s.charAt(i))) { return i; } } return -1; }
}