Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //this lab has a seris of parts designed to utilize strings to solve problems/questions
- package unitone;
- import java.util.*;
- public class lab2
- {
- /**
- * Method inString is a method which finds the first and last occurence of a character within a string
- *
- * @param s is a String that is searched to find the occurences
- * @param search is a string that the character being searched in s
- * @return returns how many occurences of search in s
- */
- public static int[] inString( String s , String search ){
- int arr[]= new int[2];
- arr[0]= s.indexOf(search);
- arr[1]=s.lastIndexOf(search);
- return arr;
- }
- /**
- * Method modString takes indexes 0-x of a string and adds it on to the back of the string in all caps
- *
- * @param s is the string being looked at
- * @param x is the value of the index that are going to be made all caps
- * @return Is the new string
- */
- public static String modString( String s , int x ){
- String q=s.substring(0,x+1);
- q=q.toUpperCase();
- String m= s +q;
- return m;
- }
- /**
- * Method changeCapitals changes all charcters that are capitals to non capitals and vice versa
- *
- * @param s is the string which will be evaluated
- * @return The new string
- */
- public static String changeCapitals( String s ){
- String u=s.toUpperCase();
- String p;
- String p1=new String();
- for(int i=0; i<s.length();i++){//traverses the string
- if(u.substring(i,i+1).equals(s.substring(i,i+1))){//if its uppercase make lowercase
- p=s.substring(i,i+1).toLowerCase();
- }
- else{//if its lowercase make uppercase
- p=s.substring(i,i+1).toUpperCase();
- }
- p1+=p;
- }
- return p1;
- }
- /**
- * Method howManyDifferent finds how many characters are the same between two strings
- *
- * @param s1 Is the first string being evaluated
- * @param s2 Is the second string being evaluated
- * @return how many occurences of the same character
- */
- public static int howManyDifferent( String s1, String s2 ){
- String ss1;
- String ss2;
- int count=0;
- if(s1.length()<=s2.length()){// if s1 is the shorter string do this
- for(int i=0; i<(s1.length());i++){//traverse the strings letter by letter until you reach the length of s1
- ss1=s1.substring(i,i+1);
- ss2=s2.substring(i,i+1);
- if (ss1.equalsIgnoreCase(ss2)){//if they are the same increase the count by 1
- count++;
- }
- }
- }
- else if (s1.length()>s2.length()){// if s2 is shorter do this
- for(int i=0; i<(s2.length());i++){//traverse the strings letter by letter until you reach the length of s2
- ss1=s1.substring(i,i+1);
- ss2=s2.substring(i,i+1);
- if (ss1.equalsIgnoreCase(ss2)){//if they are the same increase the count by 1
- count++;
- }
- }
- }
- return count;
- }
- /**
- * Method searchArrayList searches for a string within an array
- *
- * @param arr the array holding strings
- * @param search the string that is being searched for
- * @return how many occurences of the string in the array
- */
- public static int searchArrayList( String arr[] , String search ){
- int count=0;
- for (int i=0; i<arr.length;i++){//traversing the array
- arr[i]=arr[i].toLowerCase();
- search=search.toLowerCase();
- if (arr[i].contains(search)){//if that index of the array has the string increase count by one
- count++;
- }
- }
- return count;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement