Advertisement
Guest User

Lab

a guest
Sep 14th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. //this lab has a seris of parts designed to utilize strings to solve problems/questions
  2. package unitone;
  3. import java.util.*;
  4. public class lab2
  5. {
  6. /**
  7. * Method inString is a method which finds the first and last occurence of a character within a string
  8. *
  9. * @param s is a String that is searched to find the occurences
  10. * @param search is a string that the character being searched in s
  11. * @return returns how many occurences of search in s
  12. */
  13. public static int[] inString( String s , String search ){
  14.  
  15. int arr[]= new int[2];
  16. arr[0]= s.indexOf(search);
  17. arr[1]=s.lastIndexOf(search);
  18. return arr;
  19.  
  20.  
  21.  
  22. }
  23. /**
  24. * Method modString takes indexes 0-x of a string and adds it on to the back of the string in all caps
  25. *
  26. * @param s is the string being looked at
  27. * @param x is the value of the index that are going to be made all caps
  28. * @return Is the new string
  29. */
  30. public static String modString( String s , int x ){
  31. String q=s.substring(0,x+1);
  32. q=q.toUpperCase();
  33.  
  34. String m= s +q;
  35. return m;
  36. }
  37. /**
  38. * Method changeCapitals changes all charcters that are capitals to non capitals and vice versa
  39. *
  40. * @param s is the string which will be evaluated
  41. * @return The new string
  42. */
  43. public static String changeCapitals( String s ){
  44. String u=s.toUpperCase();
  45. String p;
  46. String p1=new String();
  47. for(int i=0; i<s.length();i++){//traverses the string
  48. if(u.substring(i,i+1).equals(s.substring(i,i+1))){//if its uppercase make lowercase
  49.  
  50. p=s.substring(i,i+1).toLowerCase();
  51.  
  52. }
  53. else{//if its lowercase make uppercase
  54. p=s.substring(i,i+1).toUpperCase();
  55.  
  56. }
  57. p1+=p;
  58. }
  59. return p1;
  60. }
  61. /**
  62. * Method howManyDifferent finds how many characters are the same between two strings
  63. *
  64. * @param s1 Is the first string being evaluated
  65. * @param s2 Is the second string being evaluated
  66. * @return how many occurences of the same character
  67. */
  68. public static int howManyDifferent( String s1, String s2 ){
  69. String ss1;
  70. String ss2;
  71. int count=0;
  72. if(s1.length()<=s2.length()){// if s1 is the shorter string do this
  73. for(int i=0; i<(s1.length());i++){//traverse the strings letter by letter until you reach the length of s1
  74. ss1=s1.substring(i,i+1);
  75. ss2=s2.substring(i,i+1);
  76. if (ss1.equalsIgnoreCase(ss2)){//if they are the same increase the count by 1
  77. count++;
  78. }
  79. }
  80. }
  81. else if (s1.length()>s2.length()){// if s2 is shorter do this
  82. for(int i=0; i<(s2.length());i++){//traverse the strings letter by letter until you reach the length of s2
  83. ss1=s1.substring(i,i+1);
  84. ss2=s2.substring(i,i+1);
  85. if (ss1.equalsIgnoreCase(ss2)){//if they are the same increase the count by 1
  86. count++;
  87. }
  88. }
  89. }
  90. return count;
  91. }
  92. /**
  93. * Method searchArrayList searches for a string within an array
  94. *
  95. * @param arr the array holding strings
  96. * @param search the string that is being searched for
  97. * @return how many occurences of the string in the array
  98. */
  99. public static int searchArrayList( String arr[] , String search ){
  100. int count=0;
  101.  
  102. for (int i=0; i<arr.length;i++){//traversing the array
  103. arr[i]=arr[i].toLowerCase();
  104. search=search.toLowerCase();
  105. if (arr[i].contains(search)){//if that index of the array has the string increase count by one
  106. count++;
  107. }
  108.  
  109. }
  110.  
  111.  
  112. return count;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement