How do I remove the space at the end of the result? Code: import java.util.Scanner; public class FinalExamAnswers{ public static void main(String [] args) { manipulateString(); //calls function } //your code here public static void manipulateString() { Scanner sc=new Scanner(System.in); //create Scanner instance System.out.println("Enter a sentence"); String sentence=sc.nextLine(); //input a sentence String[] words=sentence.split(" "); //split the sentence at space and store it in array for(int i=0;i=0;i--) //i from last index to 0 { System.out.print(words[i]+" "); //print word at index i and space } } }
How do I remove the space at the end of the result?
Code:
import java.util.Scanner;
public class FinalExamAnswers{
public static void main(String [] args)
{
manipulateString(); //calls function
}
//your code here
public static void manipulateString()
{
Scanner sc=new Scanner(System.in); //create Scanner instance
System.out.println("Enter a sentence");
String sentence=sc.nextLine(); //input a sentence
String[] words=sentence.split(" "); //split the sentence at space and store it in array
for(int i=0;i<words.length;i++) //i from 0 to last index
{
if(i%2==0) //if even index
words[i]=words[i].toUpperCase(); //converted to upper case
else //if odd index
words[i]=words[i].toLowerCase(); //converted to lower case
}
for(int i=words.length-1;i>=0;i--) //i from last index to 0
{
System.out.print(words[i]+" "); //print word at index i and space
}
}
}
Step by step
Solved in 2 steps