Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.27 KB | None | 0 0
  1. /**
  2. * TODO Write a one-sentence summary of your class here. TODO Follow it with
  3. * additional details about its purpose, what abstraction it represents, and how
  4. * to use it.
  5. *
  6. * @author TODO Your Name
  7. * @version TODO Date
  8. *
  9. * @author Period - TODO Your Period
  10. * @author Assignment - JMCh10 Lipogrammer
  11. *
  12. * @author Sources - TODO list collaborators
  13. */
  14. public class LipogramAnalyzer
  15. {
  16. private String text;
  17.  
  18.  
  19. /**
  20. * Constructor: Saves the text string
  21. *
  22. * @param text
  23. * String to analyze
  24. */
  25. public LipogramAnalyzer( String text )
  26. {
  27. this.text = text;
  28. }
  29.  
  30.  
  31. /**
  32. * Returns the text string with all characters equal to letter replaced with
  33. * '#'.
  34. *
  35. * @param letter
  36. * character to replace
  37. * @return text string with all characters equal to letter replaced with '#'
  38. */
  39. public String mark( char letter )
  40. {
  41. int length = text.length();
  42. String newText = "";
  43. for ( int i = 0; i < length; i++ ) // i is position in the string
  44. {
  45. if ( text.charAt( i ) == letter )
  46. {
  47. newText += '#';
  48. }
  49. else
  50. {
  51. newText += text.charAt( i );
  52. }
  53. }
  54. return newText;
  55. }
  56.  
  57.  
  58. /**
  59. * Returns a String that concatenates all "offending" words from text that
  60. * contain letter; the words are separated by '\n' characters; the returned
  61. * string does not contain duplicate words: each word occurs only once;
  62. * there are no punctuation or whitespace characters in the returned string.
  63. *
  64. * @param letter
  65. * character to find in text
  66. * @return String containing all words with letter
  67. */
  68. public String allWordsWith( char letter )
  69. {
  70. String finalString = "";
  71. int length = text.length();
  72. int j;
  73. if ( ( length == 1 ) && ( text.charAt( 0 ) == letter ) )
  74. {
  75. finalString += text.charAt( 0 );
  76. }
  77. else
  78. {
  79. for ( int i = 0; i < length; i += j ) // for all characters in the
  80. // string
  81. {
  82. boolean wordFound = false;
  83. boolean whileLoop = true;
  84. boolean spaceFound = false;
  85. boolean oneWord = false;
  86. boolean letterLast = false;
  87. boolean oneLetterString = false;
  88. j = i; // set j equal to the current position
  89. while ( whileLoop ) // for each word
  90. {
  91. if ( text.charAt( j ) == letter ) // checks if letter is
  92. // found
  93. {
  94. if ( ( ( !wordFound )
  95. && ( !finalString.contains( extractWord( i ) ) ) )
  96. || ( ( text.charAt( i ) == letter )
  97. && ( !finalString
  98. .contains( extractWord( i ) ) ) ) )
  99. {
  100. wordFound = true;
  101. }
  102. }
  103. if ( ( ( j == length - 1 ) || ( text.charAt( j ) == ' ' )
  104. || ( j == length + 1 )) && ( text.length() != 1 ) )
  105. {
  106. whileLoop = false;
  107. if ( j == length - 1 )
  108. {
  109. if ( text.charAt( j ) == letter )
  110. {
  111. if ( text.charAt( j - 1 ) == ' ' )
  112. {
  113. oneWord = true;
  114. wordFound = true;
  115. }
  116. else
  117. {
  118. wordFound = true;
  119. }
  120. }
  121. }
  122. else
  123. {
  124. if ( ( text.charAt( j ) == ' ' )
  125. || ( text.charAt( j - 1 ) == ' ' ) )
  126. {
  127. if ( text.charAt( j ) == ' ' )
  128. {
  129. spaceFound = true;
  130. }
  131. if ( j == text.length() - 1 )
  132. {
  133. if ( ( text.charAt( j - 1 ) == ' ' )
  134. || ( j - 1 == -1 ) )
  135. {
  136. oneWord = true;
  137. wordFound = true;
  138. }
  139. }
  140. else if ( text.charAt( j + 1 ) == ' ' )
  141. {
  142. if ( ( text.charAt( j - 1 ) == ' ' )
  143. || ( j - 1 == -1 ) )
  144. {
  145. oneWord = true;
  146. wordFound = true;
  147. }
  148. }
  149. }
  150. }
  151. }
  152. j++;
  153. }
  154. if ( wordFound ) // if the word is found do this:
  155. {
  156. if ( oneLetterString )
  157. {
  158. finalString += text.charAt( 0 );
  159. }
  160. if ( oneWord )
  161. {
  162. finalString += text.charAt( j - 1 );
  163. }
  164. if ( spaceFound )
  165. {
  166. for ( int x = i; x <= j - 2; x++ )
  167. {
  168. if ( Character.isLetter( text.charAt( x ) ) )
  169. {
  170. finalString += text.charAt( x );
  171. }
  172. }
  173. }
  174. else
  175. {
  176. for ( int x = i; x <= j - 1; x++ )
  177. {
  178. if ( ( Character.isLetter( text.charAt( x ) ) )
  179. && ( !finalString
  180. .contains( extractWord( x ) ) ) )
  181. {
  182. finalString += text.charAt( x );
  183. }
  184. }
  185. }
  186. finalString += "\n";
  187. }
  188. i = 0;
  189. }
  190. }
  191. return finalString;
  192. }
  193.  
  194.  
  195. // made public for test purposes
  196. /**
  197. * Returns the word that contains character at pos excluding any punctuation
  198. * or whitespace.
  199. *
  200. * @param pos
  201. * location of character
  202. *
  203. * @return word that contains character at pos
  204. */
  205. public String extractWord( int pos )
  206. {
  207. String finalString = "";
  208. int endCount = 0;
  209. int startCount = 0;
  210. boolean endFound = false;
  211. boolean startFound = false;
  212. boolean spaceFound = false;
  213. boolean oneWord = false;
  214. if ( pos == text.length() - 1 )
  215. {
  216. if ( pos - 1 == -1 )
  217. {
  218. oneWord = true;
  219. }
  220. else
  221. {
  222. if ( text.charAt( pos - 1 ) == ' ' )
  223. {
  224. oneWord = true;
  225. }
  226. }
  227. }
  228. else if ( text.charAt( pos + 1 ) == ' ' )
  229. {
  230. if ( pos - 1 == -1 )
  231. {
  232. oneWord = true;
  233. }
  234. else
  235. {
  236. if ( text.charAt( pos - 1 ) == ' ' )
  237. {
  238. oneWord = true;
  239. }
  240. }
  241. }
  242. if ( text.charAt( pos ) == ' ' )
  243. {
  244. spaceFound = true;
  245. }
  246. while ( !endFound )
  247. {
  248. if ( pos + endCount + 1 < text.length() )
  249. {
  250. if ( ( text.charAt( pos + endCount + 1 ) == ' ' )
  251. || ( pos + endCount == text.length() - 1 ) )
  252. {
  253. endFound = true;
  254. }
  255. else
  256. {
  257. endCount++;
  258. }
  259. }
  260. else if ( pos + endCount + 1 == text.length() )
  261. {
  262. endFound = true;
  263. }
  264. }
  265. while ( !startFound )
  266. {
  267. if ( pos - startCount - 1 > -1 )
  268. {
  269. if ( ( text.charAt( pos - startCount - 1 ) == ' ' )
  270. || ( pos - startCount == 0 ) )
  271. {
  272. startFound = true;
  273. }
  274. else
  275. {
  276. startCount++;
  277. }
  278. }
  279. else if ( pos - startCount - 1 == -1 )
  280. {
  281. startFound = true;
  282. pos++;
  283. }
  284. }
  285. if ( ( !spaceFound ) || ( !oneWord ) )
  286. {
  287. if ( pos - startCount == 1 )
  288. {
  289. for ( int i = pos - startCount - 1; i < pos + endCount; i++ )
  290. {
  291. if ( Character.isLetter( text.charAt( i ) ) )
  292. {
  293. finalString += text.charAt( i );
  294. }
  295. }
  296. }
  297. else
  298. {
  299. for ( int i = pos - startCount; i <= pos + endCount; i++ )
  300. {
  301. if ( Character.isLetter( text.charAt( i ) ) )
  302. {
  303. finalString += text.charAt( i );
  304. }
  305. }
  306. }
  307. }
  308. else if ( spaceFound )
  309. {
  310. finalString = "";
  311. }
  312. else if ( oneWord )
  313. {
  314. finalString += text.charAt( pos );
  315. }
  316. return finalString;
  317. }
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement