Computer Science, asked by LittleBoy, 1 year ago

Please Help!!!
Java Program...

Attachments:

Answers

Answered by QGP
9

Java Program

We are tasked to write a program to print the words of a string in reverse order. Now there is very simple way to do this.


Java provides a very simple function to split a string according to a specific delimiter:

String[] split(String regex)


This function returns an array of strings separated by delimiter mentioned in regex.


For example, suppose we name our original string as str. Then str.split(" ") would return an array of words which were originally separated by a space.


We can then run a loop in reverse through the resulting array and print the words. Here is the program:


import java.util.Scanner;


public class Brainly

{

   public static void main(String[] args)

   {

       Scanner sc = new Scanner(System.in);

       

       System.out.print("Enter the string: ");

       String str = sc.nextLine();

       

       String[] arr = str.split(" ");      //Splitting str with space as delimiter

       

       for(int i=arr.length-1;i>=0;i--)    //Running a loop in reverse through arr

       {

           System.out.print(arr[i]+" ");

       }

   }

}



_______________


However, if a program is to be written without the use of it, then we must think. Here is a logic I implemented in the following program.


We count the number of spaces in the original string. Assuming the string did not start with space, if there were n spaces, then there would be n+1 words.


We now create and initialise a String [] array of n+1 elements. This array will contain our words.


Then we run a loop running through the length of the string. We keep adding each character to our String array elements until a space is encountered, and then we add the next characters to the next String array element and proceed in this way.


So we finally have an array of strings, containing our words.


Now we just run a loop in reverse to print the words. Here's the program:


import java.util.Scanner;


public class Brainly

{

   public static void main(String[] args)

   {

       Scanner sc = new Scanner(System.in);

       

       System.out.print("Enter the string: ");

       String str = sc.nextLine();

       

       int numberOfSpaces = 0;

       

       for(int i=0;i<str.length();i++)     //Loop to count number of spaces

       {

           if(str. charAt(i)==' ')  

           {

               numberOfSpaces++;  

           }

       }

       

       //For n spaces, there will be n+1 words

       //This is assuming there is no space at the start of string

       

       String[] arr = new String[numberOfSpaces+1];

       

       for(int i=0;i<arr.length;i++)  

       {

           arr[i]="";      //Initialising Array Elements

       }

       

       int j=0;        //We will now store words in the array

       

       for(int i=0;i<str.length();i++)

       {

           if(str. charAt(i)==' ')      //When space is encountered, j is incremented

           {

               j++;

           }

           else        //Else the character would be added to the word in array

           {

               arr[j]+=str. charAt(i);

           }

       }

       

       //We now have the words stored in arr

       

       for(int i=arr.length-1;i>=0;i--)    //Running a loop in reverse through the array

       {

           System.out.print(arr[i]+" ");

       }

   }

}




Attachments:

LittleBoy: Thank you
Similar questions