Java
Programming and Technical
Programming
Arrays
Write a function that accepts a sentence as a parameter, and returns the same with each of its words reversed. The returned sentence should have 1 blank space between each pair of words.
Demonstrate the usage of this function from a main program.
Example:
Parameter: “jack and jill went up a hill”
Return Value: “kcaj dna llij tnew pu a llih”
Read Solution (Total 10)
-
- import java.util.Scanner;
public class MAKHAL {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Enter Your Name");
Scanner in = new Scanner(System.in);
String n = in.nextLine();
String s1 = n;
String[] s2 = s1.split(" ");
for (String string : s2)
{
StringBuffer sb = new StringBuffer();
sb.append(string);
sb.reverse();
System.out.print(sb);
System.out.print(" ");
}
}
}
/*output :
Enter Your Name
jack and jill went up a hill
kcaj dna llij tnew pu a llih */ - 8 years agoHelpfull: Yes(13) No(0)
- class Solution
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String sentence=sc.nextLine();
String[] word=sentence.split(" ");
String rev="";
for(int i=0;i - 7 years agoHelpfull: Yes(2) No(0)
- package constructure;
import java.util.Scanner;
public class Kamal {
public static void main(String[] args) {
System.out.println("enter the name");
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
String s1=s;
String s2[]=s1.split(" ");
for(String s3:s2)
{
StringBuffer sb =new StringBuffer();
sb.append(s3);
sb.reverse();
System.out.print(sb);
System.out.print(" ");
}
}
}
output
enter the name
bangalore patna sofware
erolagnab antap erawfos - 7 years agoHelpfull: Yes(1) No(0)
- import java.util.Scanner;
class convertion {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String str;
System.out.println("Enter string");
str = sc.nextLine();
String str1[] = str.split(" ");
for (int i = 0; i < str1.length; i++) {
StringBuffer bu = new StringBuffer();
bu.append(str1[i]);
System.out.print(bu.reverse());
System.out.print(" ");
}
}
} - 7 years agoHelpfull: Yes(1) No(0)
- class HelloWorld {
public static void main(String[] args) {
String s="jack and jill went up a hill”;
String[] s1=s.split(" ");
for(int i=0;i - 3 years agoHelpfull: Yes(1) No(0)
- Hi Jagriti, in which company was this question asked?
- 9 years agoHelpfull: Yes(0) No(0)
- import java.util.Scanner;
class ReverseSentence
{
public static String reverse(String s)
{
String []str=s.split(" ");
String st="";
for(int i=0;i=0;j--)
st=st+ch[j];
st=st+" ";
}
return st;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Plese Enter the sentence here..");
String str=sc.nextLine();
String s=reverse(str);
System.out.println(s);
}
} - 8 years agoHelpfull: Yes(0) No(0)
- class Solution
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String sentence=sc.nextLine();
String[] word=sentence.split(" ");
String rev="";
for(int i=0;i - 7 years agoHelpfull: Yes(0) No(0)
- public class Reversesentence {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("Enter the sentence");
String str=s.nextLine();
String[] str2=str.split(" ");
int l=str2.length;
for(int i=0;i - 6 years agoHelpfull: Yes(0) No(0)
- public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "jack and jill went up the hill";
String temp [] = str.split(" ");
String help = "";
StringBuffer sb;
for(int i = 0; i < temp.length ; i++) {
sb = new StringBuffer(temp[i]);
temp[i] = sb.reverse().toString();
}
for(int i = 0; i < temp.length; i++) {
help = help+ temp[i] + " ";
}
System.out.println(help);
} - 5 years agoHelpfull: Yes(0) No(0)
Java Other Question