Question

In: Computer Science

Change the code to sort list of strings without using any pre-defined functions Here is my...

Change the code to sort list of strings without using any pre-defined functions

Here is my code:

    int n;
    String temp;
    Scanner in = new Scanner(System.in);
    System.out.print("Enter number of names you want to enter:");
    n = in.nextInt();
    String names[] = new String[n];
    Scanner s1 = new Scanner(System.in);
    System.out.println("Enter all the names:");
    for(int i = 0; i < n; i++){
        names[i] = s1.nextLine();
    }
***CHANGE THIS PART***
    for (int i = 0; i < n; i++){
        for (int j = i + 1; j < n; j++){
            if (names[i].compareTo(names[j])>0) {
                temp = names[i];
                names[i] = names[j];
                names[j] = temp;
            }
        }
    }
*** I WANT TO SORT WITHOUT USING ".compareto()" OR ANY OTHER PRE-DEFINED FUNCTION***
    System.out.print("Names in Sorted Order:");
    for (int i = 0; i < n - 1; i++){
        System.out.print(names[i] + ",");
    }
    System.out.print(names[n - 1]);

Solutions

Expert Solution

Answer:-

Hello, I have modified the given program so as it can sort the array of Strings without using any predefined function. Please find it below.

Here I have written a user-defined function compareString which is used for comparing two strings. And from the main function, we are calling this compartString function for comparing the Strings of names array.

JAVA CODE:-

import java.util.Scanner;
class Solution
{
   /* compareString function for comparing two strings*/
   static int compareString(String a,String b)
   {
       a=a.toUpperCase();
       b=b.toUpperCase();
       /*Loop for visiting each character of Strings*/
       for(int i=0;i<a.length()&&i<b.length();i++)
       {
           /*Checking if the character at index i in both strings are equal or not*/
if(a.charAt(i)==b.charAt(i))
{

}
/* If String a is greter than String b then it will return 1*/
else if(a.charAt(i)>b.charAt(i))
{
   return 1;
}
/* If String a is lesser than String b then it will return -1*/
else
{
   return -1;
}
       }
   /*If both the Strings are equal then it will return 0.*/
       return 0;
   }


   public static void main(String [] args)
   {
int n;
String temp;
Scanner in = new Scanner(System.in);
System.out.print("Enter number of names you want to enter:");
n = in.nextInt();
String names[] = new String[n];
Scanner s1 = new Scanner(System.in);
System.out.println("Enter all the names:");
for(int i = 0; i < n; i++){
names[i] = s1.nextLine();
}
for (int i = 0; i < n; i++){
for (int j = i + 1; j < n; j++){
if (compareString(names[i],names[j])==1) {
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
System.out.print("Names in Sorted Order:");
for (int i = 0; i < n - 1; i++){
System.out.print(names[i] + ",");
}
System.out.print(names[n - 1]);

   }
}

Screenshot of Code:-

Please refer to the screenshots of the code for properly understanding the indentation of the code.

Screenshot 1:

Screenshot 2:

OUTPUT:-

NOTE:- compareString is not any predefined function. We have defined this function in our code.

I hope it would help.

Thanks!


Related Solutions

You are provided with an array of Strings and a list of Strings. Sort the elements...
You are provided with an array of Strings and a list of Strings. Sort the elements (1) in natural order, (2) in reverse natural order and (3) by the length of each String. You can fill in the details by using the following stub file: import java.util.Arrays; import java.util.Collections; import java.util.List; public class Midterm01 { public static void main(String[] args) { String[] arrayOfCities = { "Atlanta", "Savannah", "New York", "Dallas", "Rio" }; List<String> listOfCities = Arrays.asList("Atlanta", "Savannah", "New York", "Dallas",...
For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
Using the data set as a pre-defined variable in your program, write code that uses the...
Using the data set as a pre-defined variable in your program, write code that uses the dataset to print the first names of people who have BOTH above average math grades AND below average age from the dataset. The solutions for the textbook examples assume you are able to export in a framework like node.js, which is why in the data set I provide, I simply set the array of objects as a variable. Your code will be in the...
Is it possible for naturally produced proteins to have different functions without any change in amino...
Is it possible for naturally produced proteins to have different functions without any change in amino acid sequence or nucleotide sequence? How?
Here is my fibonacci code using pthreads. When I run the code, I am asked for...
Here is my fibonacci code using pthreads. When I run the code, I am asked for a number; however, when I enter in a number, I get my error message of "invalid character." ALSO, if I enter "55" as a number, my code automatically terminates to 0. I copied my code below. PLEASE HELP WITH THE ERROR!!! #include #include #include #include #include int shared_data[10000]; void *fibonacci_thread(void* params); void parent(int* numbers); int main() {    int numbers = 0; //user input....
converting strings to floats without using stdlib.h or string.h
converting strings to floats without using stdlib.h or string.h
using javascript and without using javascript sort. Sort an array from lowest to highest const x...
using javascript and without using javascript sort. Sort an array from lowest to highest const x = [201,28,30,-5] ​function sort(array){ // put your code here return //the answer } ​ sort(x) // output: [-5,28,30,201]
Hello, I need the Matlab code of the Fourier Transform without using the Matlab functions fft...
Hello, I need the Matlab code of the Fourier Transform without using the Matlab functions fft and dft. Applied to discrete signals. If you can with an example.Thank you!!
Gauss Jordan elimination using python for a 2d list (3x3) without using any library. Thanks
Gauss Jordan elimination using python for a 2d list (3x3) without using any library. Thanks
Java language 1. Sort any 10 keys using Min Heap. 2. Sort any 10 keys using...
Java language 1. Sort any 10 keys using Min Heap. 2. Sort any 10 keys using Max Heap.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT