Last active
December 18, 2021 07:51
-
-
Save aadipoddar/ad39e013ee20d5c0bcbd4b4810568fea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Input a Sentence and Print it in Ascending Order | |
| * | |
| * AS YOU SOW SO SHALL YOU REAP. | |
| * AS SO SOW YOU YOU REAP SHALL. | |
| */ | |
| import java.util.Scanner; | |
| class Sentence_Ascending | |
| { | |
| public static void main(String[] args) { | |
| Scanner sc = new Scanner(System.in); | |
| System.out.println("Enter a sentence: "); | |
| String sentence = sc.nextLine(); | |
| sentence = sentence.toUpperCase(); | |
| int k = 0; | |
| int countWords = 0; | |
| char character = ' '; | |
| String word = ""; | |
| String temp = ""; | |
| String finalSentence = ""; | |
| boolean isEndLineStatementPresent = false; | |
| char endLineStatement = ' '; | |
| if(sentence.charAt(sentence.length()-1) == '.' || sentence.charAt(sentence.length()-1) == '?' || sentence.charAt(sentence.length()-1) == '!') | |
| { | |
| isEndLineStatementPresent = true; | |
| endLineStatement = sentence.charAt(sentence.length()-1); | |
| sentence = sentence.substring(0, sentence.length()-1); | |
| } | |
| for(int i = 0; i < sentence.length(); i++) { | |
| character = sentence.charAt(i); | |
| if (character == ' ' || i == sentence.length() - 1) { | |
| countWords++; | |
| } | |
| } | |
| String[] wordArray = new String[countWords]; | |
| for(int i = 0; i < sentence.length(); i++) { | |
| character = sentence.charAt(i); | |
| if(character != ' ') { | |
| word += character; | |
| } | |
| if(character == ' ' || i == sentence.length() - 1) { | |
| wordArray[k] = word; | |
| word = ""; | |
| k++; | |
| } | |
| } | |
| for(int i = 0; i < wordArray.length - 1; i++) { | |
| for(int j = 0; j < wordArray.length - i - 1; j++) { | |
| if(wordArray[j].length() > wordArray[j + 1].length()) { | |
| temp = wordArray[j]; | |
| wordArray[j] = wordArray[j + 1]; | |
| wordArray[j + 1] = temp; | |
| } | |
| } | |
| } | |
| for(int i = 0; i < wordArray.length - 1; i++) | |
| { | |
| if(wordArray[i].length() == wordArray[i + 1].length()) | |
| { | |
| if(!wordArray[i].equals(wordArray[i + 1])) | |
| { | |
| k = 0; | |
| while((int)wordArray[i].charAt(k) == (int)wordArray[i + 1].charAt(k)) | |
| { | |
| k++; | |
| if((int)wordArray[i].charAt(k) > (int)wordArray[i + 1].charAt(k)) | |
| { | |
| temp = wordArray[i]; | |
| wordArray[i] = wordArray[i + 1]; | |
| wordArray[i + 1] = temp; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| if(isEndLineStatementPresent) { | |
| for(int i = 0; i < wordArray.length; i++) { | |
| finalSentence += wordArray[i] + " "; | |
| } | |
| finalSentence = finalSentence.substring(0, finalSentence.length() - 1); | |
| finalSentence += endLineStatement; | |
| } | |
| else { | |
| for(int i = 0; i < wordArray.length; i++) { | |
| finalSentence += wordArray[i] + " "; | |
| } | |
| finalSentence = finalSentence.substring(0, finalSentence.length() - 1); | |
| } | |
| System.out.println(finalSentence); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment