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”
Answer Posted / vinay
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<l;i++) {
StringBuffer sb=new StringBuffer(str2[i]);
StringBuffer revstr = sb.reverse();
System.out.print(" "+revstr);
}
}
}
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
What are header files and explain what are its uses in c programming?
What is pragma in c?
the process of defining something in terms of itself is called (or) in C it is possible for the functions to call themselves. A function called a) nested function b) void function c) recursive function d) indifinite function
Does c have an equivalent to pascals with statement?
What are the advantages of using new operator as compared to the function malloc ()?
What does %p mean c?
What is the purpose of the statement: strcat (S2, S1)?
Is c dynamically typed?
What is #pragma statements?
What is the difference between memcpy and memmove?
Do array subscripts always start with zero?
What are Macros? What are its advantages and disadvantages?
Can i use “int” data type to store the value 32768? Why?
What are the Advantages of using macro
Does sprintf put null character?