write me a program that reverses the order of a sentence
Does that mean the sentence is reversed in terms of each letter? Or is it each word? Or is it each word reversed? Well, I did one of each for kicks.
So here is each piece. Enjoy.
package com.blogpost.revolutioniselouder;
public class Main
{
/**
* This function reverses all letters in a string
* @param s string to reverse
* @return a string with all letters in reverse order
*/
public String reverseAllLetters( String s )
{
}
/**
* This function takes a string and a separator, splits the string and then individually
* reverses each of the letters. Keep in mind that punctuation will be reversed as well
* _if_ it is not used as the separator.
* @param s String to reverse words
* @param separator the pattern to split the string by
* @return a String with every word reversed. For example if the String is 'car cat', the returning
* reult is 'rac tac'.
*/
public String reverseLettersInAWord( String s, String separator )
{
/**
* Reverses Words in a string. If a string is 'car cat', the reversed string owuld be
* 'cat car'
* @param s String to reverse words
* @param separator the pattern to separate the words by
* @return String with words in reversed order
*/
public String reverseWords( String s, String separator )
{
/**
* Try some simple testing with one string for each string.
*/
public static void main( String [] args )
{
}
public class Main
{
/**
* This function reverses all letters in a string
* @param s string to reverse
* @return a string with all letters in reverse order
*/
public String reverseAllLetters( String s )
{
StringBuffer stringToReturn = new StringBuffer( s );
// Use the StringBuffer's reverse function to reverse all letters
stringToReturn = stringToReturn.reverse();
return stringToReturn.toString();
}
/**
* This function takes a string and a separator, splits the string and then individually
* reverses each of the letters. Keep in mind that punctuation will be reversed as well
* _if_ it is not used as the separator.
* @param s String to reverse words
* @param separator the pattern to split the string by
* @return a String with every word reversed. For example if the String is 'car cat', the returning
* reult is 'rac tac'.
*/
public String reverseLettersInAWord( String s, String separator )
{
StringBuffer stringToReturn = new StringBuffer();}
// split the string using separator passed in
String[] splitStrings = s.split( separator );
/* cycle through the split strings, reverse their letters, re-generate string */
for ( int i = 0; i < face="courier new"> {stringToReturn.append(reverseAllLetters( splitStrings[ i ] ) );}
stringToReturn.append( separator );
return stringToReturn.toString();
/**
* Reverses Words in a string. If a string is 'car cat', the reversed string owuld be
* 'cat car'
* @param s String to reverse words
* @param separator the pattern to separate the words by
* @return String with words in reversed order
*/
public String reverseWords( String s, String separator )
{
StringBuffer stringToReturn = new StringBuffer();}
String[] splitStrings = s.split( separator );
/* Take the last word, add it to the beginning of new string and continue until the split original string is parsed through */
for ( int i = splitStrings.length; i > 0; i-- )
{
stringToReturn.append( splitStrings[ i - 1 ] );
// add separator, so string looks normal
stringToReturn.append( separator );
}
return stringToReturn.toString();
/**
* Try some simple testing with one string for each string.
*/
public static void main( String [] args )
{
String s = "Never in the field of human conflict was so much owed by so many to so few.";}
Main m = new Main();
System.out.println( m.reverseAllLetters(s) );
System.out.println( m.reverseLettersInAWord( s, " " ) );
System.out.println( m.reverseWords( s, " " ) );
}
Hopefully, the output will be proof enough:
.wef os ot ynam os yb dewo hcum os saw tcilfnoc namuh fo dleif eht ni reveN
reveN ni eht dleif fo namuh tcilfnoc saw os hcum dewo yb os ynam ot os .wef
few. so to many so by owed much so was conflict human of field the in Never
Lets hope that meets the requirements.
4 comments:
Why StringBuffer instead of string concatenation? Is there anything else you could have used? What would have done different?
Strings are immutable and fiddling around with them is poor for performance. StringBuffers are just that... stuff you use to fiddle around with Strings, no? Fiddle around, finish with it, convert it to String and send it back.
As for what else could have been used, well I guess I could have used an array of characters. That might have actually been faster since it is a primitive data type. But really, is the performance that much of an issue on small strings?
Can we instrument that somehow? As in, if I wrote a function to use a char array instead of StringBuffer?
hey man, what does "face" do? i tried to code this myself for practice and i can't figure it out. and when i try to compile, my compiler doesn't recognize variable "face". thanks for the help
//to reverse only the letters of each wrd
input String S1
String S2[]=S1.split(" ");
int len=S2.length;
String S3="";
for(int i=0i<n;i++)
{String S3+=S[i].reverse();
}
System.out.println(S3);
Post a Comment