Results 1 to 5 of 5
  1. #1
    jos
    jos is offline Member
    Join Date
    Jul 2014
    Posts
    1
    Rep Power
    0

    Default manipulating a string

    hey all,


    I have strings in forms like s="(S (NP (DT This)) (VP (VBZ is) (NP (DT an) (JJ easy) (NN sentence))))"

    Programatically, I need to have words which end with ')' to be in an extra pair of brackets.. such that

    s = "(S (NP (DT (This))) (VP (VBZ (is)) (NP (DT (an)) (JJ (easy)) (NN (sentence)))))

    Note that , words like "this" and "is" are now enclosed in brackets.

    I tried to create a program which would search for an alphabet followed by ')' and would put it in bracket... but then arrays can't have insertions in between.

    Is there any way this can be achieved ?

    Thanks

    Jos

  2. #2
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    3
    Rep Power
    0

    Default Re: manipulating a string

    I was with you up until
    but then arrays can't have insertions in between.
    What does this mean?

    Anyway, yes of course it can be achieved. Let's have a little think about it.

    Forget about your computer and coding for a minute. With a pencil and paper write out how you would solve this manually. You can look at that string and know what words need to be enclosed in parenthesis right? Now explore what it is that tells you what words they are. Ask yourself these questions:
    1. How do you recognise where the word starts?
    2. How do you recognise where the word ends?
    3. How do you identify where the opening parenthesis goes?
    4. How do you identify where the closing parenthesis goes?

    Once you have answered these questions you are well on the way to defining an algorithm to solve your problem.

    See how you get on, then we'll go from there.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default Re: manipulating a string

    Also, when parsing the original String, you can build the new String with the use of a StringBuilder (have a look at the API documentation for that class).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    3
    Rep Power
    0

    Default Re: manipulating a string

    Oh, and an FYI for the Mods: The OP cross posted this question on coderanch.com (Not sure what your policy is on this)

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,656
    Rep Power
    5

    Default Re: manipulating a string

    The policy is that cross posting is permitted as long as the links are provided in the threads to alert others.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Picture manipulating help
    By evolutiongsr8 in forum New To Java
    Replies: 0
    Last Post: 03-26-2011, 06:47 AM
  2. Manipulating BufferedImage
    By ace_quorthon in forum Java 2D
    Replies: 1
    Last Post: 01-08-2011, 04:35 PM
  3. Manipulating XML
    By JosephMConcepcion in forum XML
    Replies: 2
    Last Post: 04-26-2009, 12:01 AM
  4. Manipulating String Tokenizer
    By Bomber_Will in forum New To Java
    Replies: 2
    Last Post: 04-19-2009, 11:54 PM
  5. manipulating a string (Urgent)
    By ariz in forum New To Java
    Replies: 4
    Last Post: 03-31-2009, 05:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •