Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * TODO Write a one-sentence summary of your class here. TODO Follow it with
- * additional details about its purpose, what abstraction it represents, and how
- * to use it.
- *
- * @author TODO Your Name
- * @version TODO Date
- *
- * @author Period - TODO Your Period
- * @author Assignment - JMCh10 Lipogrammer
- *
- * @author Sources - TODO list collaborators
- */
- public class LipogramAnalyzer
- {
- private String text;
- /**
- * Constructor: Saves the text string
- *
- * @param text
- * String to analyze
- */
- public LipogramAnalyzer( String text )
- {
- this.text = text;
- }
- /**
- * Returns the text string with all characters equal to letter replaced with
- * '#'.
- *
- * @param letter
- * character to replace
- * @return text string with all characters equal to letter replaced with '#'
- */
- public String mark( char letter )
- {
- int length = text.length();
- String newText = "";
- for ( int i = 0; i < length; i++ ) // i is position in the string
- {
- if ( text.charAt( i ) == letter )
- {
- newText += '#';
- }
- else
- {
- newText += text.charAt( i );
- }
- }
- return newText;
- }
- /**
- * Returns a String that concatenates all "offending" words from text that
- * contain letter; the words are separated by '\n' characters; the returned
- * string does not contain duplicate words: each word occurs only once;
- * there are no punctuation or whitespace characters in the returned string.
- *
- * @param letter
- * character to find in text
- * @return String containing all words with letter
- */
- public String allWordsWith( char letter )
- {
- String finalString = "";
- int length = text.length();
- int j;
- if ( ( length == 1 ) && ( text.charAt( 0 ) == letter ) )
- {
- finalString += text.charAt( 0 );
- }
- else
- {
- for ( int i = 0; i < length; i += j ) // for all characters in the
- // string
- {
- boolean wordFound = false;
- boolean whileLoop = true;
- boolean spaceFound = false;
- boolean oneWord = false;
- boolean letterLast = false;
- boolean oneLetterString = false;
- j = i; // set j equal to the current position
- while ( whileLoop ) // for each word
- {
- if ( text.charAt( j ) == letter ) // checks if letter is
- // found
- {
- if ( ( ( !wordFound )
- && ( !finalString.contains( extractWord( i ) ) ) )
- || ( ( text.charAt( i ) == letter )
- && ( !finalString
- .contains( extractWord( i ) ) ) ) )
- {
- wordFound = true;
- }
- }
- if ( ( ( j == length - 1 ) || ( text.charAt( j ) == ' ' )
- || ( j == length + 1 )) && ( text.length() != 1 ) )
- {
- whileLoop = false;
- if ( j == length - 1 )
- {
- if ( text.charAt( j ) == letter )
- {
- if ( text.charAt( j - 1 ) == ' ' )
- {
- oneWord = true;
- wordFound = true;
- }
- else
- {
- wordFound = true;
- }
- }
- }
- else
- {
- if ( ( text.charAt( j ) == ' ' )
- || ( text.charAt( j - 1 ) == ' ' ) )
- {
- if ( text.charAt( j ) == ' ' )
- {
- spaceFound = true;
- }
- if ( j == text.length() - 1 )
- {
- if ( ( text.charAt( j - 1 ) == ' ' )
- || ( j - 1 == -1 ) )
- {
- oneWord = true;
- wordFound = true;
- }
- }
- else if ( text.charAt( j + 1 ) == ' ' )
- {
- if ( ( text.charAt( j - 1 ) == ' ' )
- || ( j - 1 == -1 ) )
- {
- oneWord = true;
- wordFound = true;
- }
- }
- }
- }
- }
- j++;
- }
- if ( wordFound ) // if the word is found do this:
- {
- if ( oneLetterString )
- {
- finalString += text.charAt( 0 );
- }
- if ( oneWord )
- {
- finalString += text.charAt( j - 1 );
- }
- if ( spaceFound )
- {
- for ( int x = i; x <= j - 2; x++ )
- {
- if ( Character.isLetter( text.charAt( x ) ) )
- {
- finalString += text.charAt( x );
- }
- }
- }
- else
- {
- for ( int x = i; x <= j - 1; x++ )
- {
- if ( ( Character.isLetter( text.charAt( x ) ) )
- && ( !finalString
- .contains( extractWord( x ) ) ) )
- {
- finalString += text.charAt( x );
- }
- }
- }
- finalString += "\n";
- }
- i = 0;
- }
- }
- return finalString;
- }
- // made public for test purposes
- /**
- * Returns the word that contains character at pos excluding any punctuation
- * or whitespace.
- *
- * @param pos
- * location of character
- *
- * @return word that contains character at pos
- */
- public String extractWord( int pos )
- {
- String finalString = "";
- int endCount = 0;
- int startCount = 0;
- boolean endFound = false;
- boolean startFound = false;
- boolean spaceFound = false;
- boolean oneWord = false;
- if ( pos == text.length() - 1 )
- {
- if ( pos - 1 == -1 )
- {
- oneWord = true;
- }
- else
- {
- if ( text.charAt( pos - 1 ) == ' ' )
- {
- oneWord = true;
- }
- }
- }
- else if ( text.charAt( pos + 1 ) == ' ' )
- {
- if ( pos - 1 == -1 )
- {
- oneWord = true;
- }
- else
- {
- if ( text.charAt( pos - 1 ) == ' ' )
- {
- oneWord = true;
- }
- }
- }
- if ( text.charAt( pos ) == ' ' )
- {
- spaceFound = true;
- }
- while ( !endFound )
- {
- if ( pos + endCount + 1 < text.length() )
- {
- if ( ( text.charAt( pos + endCount + 1 ) == ' ' )
- || ( pos + endCount == text.length() - 1 ) )
- {
- endFound = true;
- }
- else
- {
- endCount++;
- }
- }
- else if ( pos + endCount + 1 == text.length() )
- {
- endFound = true;
- }
- }
- while ( !startFound )
- {
- if ( pos - startCount - 1 > -1 )
- {
- if ( ( text.charAt( pos - startCount - 1 ) == ' ' )
- || ( pos - startCount == 0 ) )
- {
- startFound = true;
- }
- else
- {
- startCount++;
- }
- }
- else if ( pos - startCount - 1 == -1 )
- {
- startFound = true;
- pos++;
- }
- }
- if ( ( !spaceFound ) || ( !oneWord ) )
- {
- if ( pos - startCount == 1 )
- {
- for ( int i = pos - startCount - 1; i < pos + endCount; i++ )
- {
- if ( Character.isLetter( text.charAt( i ) ) )
- {
- finalString += text.charAt( i );
- }
- }
- }
- else
- {
- for ( int i = pos - startCount; i <= pos + endCount; i++ )
- {
- if ( Character.isLetter( text.charAt( i ) ) )
- {
- finalString += text.charAt( i );
- }
- }
- }
- }
- else if ( spaceFound )
- {
- finalString = "";
- }
- else if ( oneWord )
- {
- finalString += text.charAt( pos );
- }
- return finalString;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement