-1

I have written TCPClient.java and TCPServer.java. TCPServer which will simply return the reverse string which user enters in TCPClient. For example:

Input: hello there Output: olleh ereht

Now i want to change the order in the strings. For example:

Input: hello there Output: there hello

Below is my code for only reversing, how do i do to reverse the order?

//TCPClient

package orderreversetcpclient;

//TCPClient.java
import java.io.*;
import java.net.*;

class OrderReverseTCPClient {
   public static void main(String argv[]) throws Exception
   {
      String sentence;
      String modifiedSentence;
      BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
      Socket clientSocket;
      clientSocket = new Socket("27.147.162.222", 6323);
      DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
      BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
      sentence = inFromUser.readLine();
      outToServer.writeBytes(sentence + '\n');
      modifiedSentence = inFromServer.readLine();
      System.out.println("FROM SERVER: " + modifiedSentence);
      clientSocket.close();
   }
}

//TCPServer

package OrderReverseTCPServer;

import java.io.*;
import java.net.*;

class TCPServer {
   public static void main(String argv[]) throws Exception
   {
      String clientSentence;
      //String capitalizedSentence = null;
      ServerSocket welcomeSocket = new ServerSocket(6333);
      while(true) {
         Socket connectionSocket = welcomeSocket.accept();
         BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
         DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
         clientSentence = inFromClient.readLine();
         String sendToClient = new StringBuilder(clientSentence).reverse().toString()+'\n';
         outToClient.writeBytes(sendToClient) ;
      }
   }
}
Hasib Hasan
  • 93
  • 1
  • 2
  • 10
  • 1
    This question likely has naught to do with TCP; isolate the *actual* problem, refine the title, and remove the irrelevant tags. Also, try searching for "reverse each word". – user2864740 Jun 21 '14 at 18:52
  • http://stackoverflow.com/questions/2441501/reverse-each-word-of-hello-world-in-java – user2864740 Jun 21 '14 at 18:53
  • Thats just the reverse of the string. I want to reverse the order. not every characters. FOr example: Hello world will be "world hello" – Hasib Hasan Jun 21 '14 at 19:01
  • See the linked question. (And the problem still doesn't appear to have anything to do with TCP or network programming.) – user2864740 Jun 21 '14 at 19:02

1 Answers1

0

You can use someting like that

public String reverseString(String recievedMessage) {
    // the simpliest splitting. Think about ".,!?" symbols
    String[] words = recievedMessage.split(" ");
    StringBuilder sb = new StringBuilder();
    for (int i = words.length - 1; i > 0 ; i--) {
        // Here space symbol may be enhanced to more difficult cases ".?,"
        sb.append(words[i]).append(" ");
    }
    // to delete last space symbol call trim() function.
    return sb.toString().trim();
}
Solorad
  • 904
  • 9
  • 21