Description: |
Below example shows how to find out first upper case shorting next lower case shorting algorithms
|
Code 1: |
package com.kartik;
/**
*
* @author kartik
* Blog java2blogs.blogspot.com
* This sorting is Upper case wise first display and next display Lower case by index wise
*/
public class StringDemo {
public static void main(String[] args) {
//String[] toppings = {"mandal","Cheese","veloCity","sumitra", "Pepperoni","mocha","aMerica", "kaRtik","javA"};
String[] toppings={"Ukraine", "united Arab Emirates", "United Kingdom", "United States",
"uruguay", "Uzbekistan", "Vanuatu", "Vatican City", "vietnam", "Venezuela", "Wallis and Futuna", "Western Sahara", "Yemen", "Zambia", "Zimbabwe"};
SortingImplementation ss=new SortingImplementation();
ss.demString(toppings);
}
}
|
Code 2: |
package com.kartik;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
* @author kartik
* Blog java2blogs.blogspot.com
* This sorting is Upper case wise first display and next display Lower case by index wise
*/
public class SortingImplementation {
ArrayList<String> countrylist= new ArrayList<String>();
public void demString(String[] countryArray){
Arrays.sort(countryArray);
countrylist= new ArrayList<String>(Arrays.asList(countryArray));
List<String> tempCountryList=new ArrayList<String>();
int maxStringLength=0;
int tempStringLength=0;
int tempIndex=0;
int indexOfString=0;
//find out max length of string
for (String country : countrylist) {
tempStringLength=country.length();
if(tempStringLength>maxStringLength){
maxStringLength=tempStringLength;
}
}
//add upper case string to tempCountryList
for(int count=0;count<countrylist.size();count++){
for (String country : countrylist) {
if(country.length()<=maxStringLength){
indexOfString=tempIndex;
if(country.length()>indexOfString){
if(Character.isUpperCase(country.charAt(indexOfString))){
if(!tempCountryList.contains(country)){
tempCountryList.add(country);
tempIndex=indexOfString;
}
}
}
}
}
tempIndex++;
}
//add not upper case string to tempCountryList
tempIndex=0;
indexOfString=0;
for(int count=0;count<countrylist.size();count++){
for (String country : countrylist) {
if(country.length()<=maxStringLength){
indexOfString=tempIndex;
if(country.length()>indexOfString){
if(!Character.isUpperCase(country.charAt(indexOfString))){
if(!tempCountryList.contains(country)){
tempCountryList.add(country);
tempIndex=indexOfString;
}
}
}
}
}
tempIndex++;
}
//display sorting string array list
for (String country : tempCountryList) {
System.out.println(country);
}
}
}
|
Output: Ukraine United Kingdom United States Uzbekistan Vanuatu Vatican City Venezuela Wallis and Futuna Western Sahara Yemen Zambia Zimbabwe united Arab Emirates uruguay vietnam |
Example: |
2 comments
Click here for commentsgreat kartik , thank you very much on the solution you gave.
ReplyKartik you passed the challenge.
Reply