Sorting a Atring array
I have a String and i have to use a bubble sort it and i can't figure it out. i used the SUBSTRING to break the string in to words. here is my code so far any help would be greatly appreciated.publicclass stringArraySort{
publicstaticvoid main(String[] args){
String Pledge ="i pledge allegiance to the flag of the united states of america, and to the republic for which it stands: one nation under god, indivisible, with liberty and justice for all." ;
// String to be segmented
int count = 0;// Number of substrings
char separator =' ';// Substring separator
int j = 0 ;
String Swap =" " ;
// Determine the number of substrings
int index = 0;
do
{
++count;// Increment count of substrings
++index;// Move past last position
index = Pledge.indexOf(separator, index);
}
while (index != -1);
// Extract the substring into an array
String[] subStr =new String[count];// Allocate for substrings
index = 0;// Substring start index
int endIndex = 0;// Substring end index
for(int i = 0; i < count; i++)
{
endIndex = Pledge.indexOf(separator,index);// Find next separator
if(endIndex == -1)// If it is not found
subStr[i] = Pledge.substring(index);// extract to the end
else// otherwise
subStr[i] = Pledge.substring(index, endIndex);// to end index
index = endIndex + 1;// Set start for next cycle
}
//java.util.Arrays.sort(subStr);
// Display the substrings
for(int i = 0; i < subStr.length; i++)
System.out.println(subStr[i]);
// java.util.Arrays.sort(subStr);
for(j=0; j<subStr.length;j++){
if(subStr[j] == subStr[j+1]){
Swap = subStr[j];
subStr[j]=subStr[j+1];
subStr[j+1]=Swap;
}
}
}
}
>

