2

I'm relatively new to Java but I'm trying my best to get a hold of it!

So in my assignment, I was asked to get an input from the user(string) and append a word to the end of every word of the input String. I'm probably missing something because I can't figure out something so simple.

For example, we get the input from the user

This is how I like it

And I would like it as:

Thismeow ismeow howmeow Imeow likemeow itmeow

I can not use any if/for/while statements/loops in the solution of this problem, however, I'm allowed to use Java String Class API

This is the code I have so far

public class Exercise2 {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        System.out.println("Please enter a string : ");
        String userString = scan.nextLine();

        String inputString = "meow";

        //I'm aware that for the next like concatenating is just adding
        //the word meow with the last word of the userString.
        String newString = userString.concat(inputString);

        scan.close();
    }
}

Question: How do I get the desired output?

Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30
GeekyMeow
  • 39
  • 1
  • 8
  • You'll have to tokenize that one String into words, append the suffix, and then knit them back together. Can you do each step? – duffymo Sep 29 '17 at 19:44
  • 1
    Sorry, yeah, basically how do I get the desired output ^^' – GeekyMeow Sep 29 '17 at 19:46
  • How is the "word" defined? In `Yes of course.` is `course.` (with dot at the end) a "word" or `course` (without the dot). – lexicore Sep 29 '17 at 20:13
  • @lexicore I don't have to worry about any punctuation if that is what you're referring. – GeekyMeow Sep 29 '17 at 20:18
  • @GeekyMeow You should define what a "word" is. Something delimited by spaces? – lexicore Sep 29 '17 at 20:19
  • @lexicore my apologies, the problem is I wasn't even given any criteria except that I shouldn't worry about any punctuation. So I can not confirm the exact definition of the "word". – GeekyMeow Sep 29 '17 at 20:23
  • @GeekyMeow Bad enough. – lexicore Sep 29 '17 at 20:41
  • [Questions asking for *homework help* must include a summary of the work you've done so far to solve the problem, and a detailed description of the difficulty you are having solving it.](https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems) *does not work*, *please help me* are not acceptable. –  Oct 29 '17 at 00:05
  • @JarrodRoberson I have done that. What else do you want me to state? – GeekyMeow Oct 29 '17 at 00:58
  • @GeekyMeow no you have not, there is not even an attempt as solving the problem in the *code* that you posted. It might as well not even be there. This is a polite **send me teh codez** and nothing more. –  Oct 29 '17 at 01:29
  • I'll make sure I post my attempts next time. I posted this after I had submitted my assignment. But I assure you I'm not trying to do a **send me teh code so I can have a free code writing service thing** @JarrodRoberson – GeekyMeow Oct 29 '17 at 01:49

2 Answers2

2

This question has a number of answers already, but as far as I can say, none of them get it right. Either it does not work with multiple spaces (as every space is replaced with "meow "). Or multiple spaces are repaced with "meow " - thus spaces or lost. Or space at the end (or absence thereof) is not handled correctly.

So here's my take:

    String userString = "This is  how   I    like    it";
    System.out.println(userString.replaceAll("([^\\s])(\\s|$)", "$1meow$2"));

Results in:

Thismeow ismeow  howmeow   Imeow    likemeow    itmeow

I assume that "word" is a sequence of non-space characters delimited by space characters or beginning or end of the string. Then last character of the "word" is a non-space character which is immediately followed by a space character or end of string. This can be detected by a regex ([^\s])(\s|$). First group will represent the last character of the word, second group - the following space or end of string.

So to append meow to each word we could insert it between the last character of the word and the following space/end of string. This is what $1meow$2 does. $1 references the first group from regex (last character of the word), $2 - the second group (following space/end of string).

Meow.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • However, off-topic: I just have to add, suppose the word was also an input in a separate string i.e `String inputString` that is scanned from the user, in that case, how will you be writing it down? Because in `$1meow$2`, `$1` & `$2` refers to the regex & is enclosed with " ", which you can't use if you're using the variable `inputString` – GeekyMeow Sep 30 '17 at 02:52
  • 1
    `"$1" + inputString + "$2`? – lexicore Sep 30 '17 at 06:49
  • Yeah, that worked perfectly. Thanks! – GeekyMeow Oct 01 '17 at 14:50
0

You could use replaceAll

userString = userString + " ";
userString =userString.replaceAll("\\s+"," ");  // removing multiple spaces
userString = userString.replaceAll(" ","meow ");

to make it work for last word, you need to append ' ' at the end.

And yes, trim() the last space, which was added at very first.

Ravi
  • 30,829
  • 42
  • 119
  • 173