-2

I am trying to solve a java program where i need to reverse every word keeping special characters in same place. Input: String s = "Why $Java Is# Great?" Output: yhW $avaJ sI# taerG?

I was able to do word reverse but not sure how to handle special characters in place.

SVK
  • 1
  • 1

4 Answers4

1

I won't give you code, as this could be an assignment and in that case you should complete it on your own, but here is (one of) the solution(s):

  1. Use a Pattern and Matcher with the regex of \w+ (consecutive word characters) to find single words without altering other stuff.
  2. Create a loop that you will use the setup from step-1 to handle each word without altering anything else.
  3. Inside the loop, using the index and length of the matches to have the range of indexes in the string which the word is in, do a simple loop that swaps the order of the characters in that range.

Since the first loop handles finding every word (and nothing else), and the second, inner loop handles reversing the words (and nothing else), the result should be what you expect.

CosmicGiant
  • 6,275
  • 5
  • 43
  • 58
1

Solution using stack :

String s = "Why $Java Is# Great?";
    String Output= "yhW $avaJ sI# taerG?";
    StringBuilder sb1 = new StringBuilder();
    Stack<Character> stack = new Stack<>();
    for(int i=0; i<s.length();i++){
        char ch = s.charAt(i);
        if(!((ch >='a' && ch<='z') ||
            (ch >='A' && ch <='Z'))){
             while(!stack.isEmpty()){
                 sb1.append(stack.pop());
             }
             sb1.append(ch);
            }else{
                stack.push(ch);
            }
            }
            System.out.println(sb1.toString());
            System.out.println(sb1.toString().equals(Output));
        }
Code_Eat_Sleep
  • 303
  • 2
  • 6
0

The java.lang.Character.isLetter(char ch) determines if the specified character is a letter.

Here is a small pseudocode

if(!character.isLetter) don't reverse

Hope that helped

hsnsd
  • 1,728
  • 12
  • 30
  • an own implementation using ascii values of characters isn't that hard to implement. A learner (as it seems), should try to refrain from using these methods as much as possible! – sbsatter Oct 22 '17 at 03:39
  • @sbs: What? If there were pedagogical value in writing it, sure, do it yourself once. But the ASCII implementation is neither interesting nor appropriate to teach in the modern world. – Davis Herring Oct 22 '17 at 04:44
  • @DavisHerring there are helper methods and classes for a variety of tasks. Learners who develop themselves often don't get to learn the underlying concepts. I go by a simple rule, do from scratch as much as possible when learning. Professionally, even our own simple implementations are less preferred. – sbsatter Oct 22 '17 at 08:59
  • @sbs: I like this idea, but I think “some characters are letters and others aren’t” is too obvious to count. “From scratch” is a relative thing anyway, or we’d be suggesting assembly language! – Davis Herring Oct 22 '17 at 23:54
  • @DavisHerring I agree. Of course going that direction would lead you to develop hardware to send those bits. Let's not go there. p.s. if that's too obvious though, one can always level up his game. – sbsatter Oct 23 '17 at 05:46
0

A simple solution would be to write a method that checks if a character is special or not, e.g. call it isSpecialChar() that returns a boolean. If you're unfamiliar as of how to do it, check representing chars using ASCII values. It's pretty basic and straightforward.

Somewhat you have to do the following: check the character, append to the string appropriately. If this is a word, you can reverse it using another helper method, for instance, reverse() which would return the string reversed. For better performance and learning, try using string builder instead of regular string.

References: How to get ASCII from char

About String builders

sbsatter
  • 591
  • 3
  • 22