Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12

Thread: clarification

  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    clarification

    hey
    i was just wondering, if i need the user to input a sentence then the program would flip every word in the sentence, do i use an array? or do i just use a string?
    thank you

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,574
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    What does "flip every word" mean?

    Does it mean this:

    ?naem "drow yreve pilf" seod tahW

    Or this:

    tahW seod "pilf yreve drow" naem?

    Or something else?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    Code:
    <script type = "text/javascript">
    
    function reverse(form) {
    var str = form.revtext.value;
    var revstr = str.split("").reverse().join("");
    form.revtext.value = revstr;
    }
    
    </script>
    
    <form>
    <input type=text name=revtext value= "Hello World">
    <input type=button value="Reverse" onClick="reverse(this.form)">
    </form>
    Or a slight modification to reverse the order of words rather than letters:-

    Code:
    <script type = "text/javascript">
    
    function reverse(form) {
    var str = form.revtext.value;
    var revstr = str.split(" ").reverse().join(" ");
    form.revtext.value = revstr;
    }
    
    </script>
    Or reverse both words and letters:-

    Code:
    <script type = "text/javascript">
    
    function reverse(form) {
    var str = form.revtext.value;
    var revstr = str.split(" ").reverse().join(" ");
    revstr = revstr.split("").reverse().join("");
    form.revtext.value = revstr;
    }
    
    </script>


    “Education is the process of casting imitation pearls before real swine” - Irwin Edman
    Last edited by Philip M; 02-22-2012 at 12:51 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    What does "flip every word" mean?

    Does it mean this:

    ?naem "drow yreve pilf" seod tahW

    Or this:

    tahW seod "pilf yreve drow" naem?

    Or something else?
    no i meant like
    "bird" would be "irdb"

  • #5
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Code:
    <script type = "text/javascript">
    
    function reverse(form) {
    var str = form.revtext.value;
    var revstr = str.split("").reverse().join("");
    form.revtext.value = revstr;
    }
    
    </script>
    
    <form>
    <input type=text name=revtext value= "Hello World">
    <input type=button value="Reverse" onClick="reverse(this.form)">
    </form>
    Or a slight modification to reverse the order of words rather than letters:-

    Code:
    <script type = "text/javascript">
    
    function reverse(form) {
    var str = form.revtext.value;
    var revstr = str.split(" ").reverse().join(" ");
    form.revtext.value = revstr;
    }
    
    </script>
    Or reverse both words and letters:-

    Code:
    <script type = "text/javascript">
    
    function reverse(form) {
    var str = form.revtext.value;
    var revstr = str.split(" ").reverse().join(" ");
    revstr = revstr.split("").reverse().join("");
    form.revtext.value = revstr;
    }
    
    </script>


    “Education is the process of casting imitation pearls before real swine” - Irwin Edman
    thank you, this was really useful but my main question was how do i make the user input a "sentence" NOT just one word! like i want the code to change all the words in the sentence not just the first word, so should i use an array or how can i let the use enter a sentence?

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    Quote Originally Posted by talaaa View Post
    thank you, this was really useful but my main question was how do i make the user input a "sentence" NOT just one word! like i want the code to change all the words in the sentence not just the first word, so should i use an array or how can i let the use enter a sentence?
    <input type=text name=revtext value= "Hello World" >

    You can type as many words as you like into the textbox! Have you actually tested the scripts I offered you?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Regular Coder
    Join Date
    Sep 2002
    Posts
    462
    Thanks
    0
    Thanked 20 Times in 20 Posts
    If you just want a word scrambler try looking up 'javascript rot13' function on google...at least I think thats still the name of it.
    NO Limits!! DHCreationStation.com
    ------------------------------------------------------------
    For projects using MediaTypes (MIMETypes) visit E-BAM.net -(updated weekly)

    Broken items wanted for tinkerin'! PostItNow@BrokenEquipment.com
    Global Complaint Dept.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    Quote Originally Posted by c1lonewolf View Post
    If you just want a word scrambler try looking up 'javascript rot13' function on google...at least I think thats still the name of it.
    As I understand it the OP is not looking for aa word scrambler. He wants to reverse the order of the characters in a sentence.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #9
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    <input type=text name=revtext value= "Hello World" >

    You can type as many words as you like into the textbox! Have you actually tested the scripts I offered you?
    but i want it to act on each word not the entire sentence as one!
    Quote Originally Posted by c1lonewolf View Post
    If you just want a word scrambler try looking up 'javascript rot13' function on google...at least I think thats still the name of it.
    not a scrambler, i just want to put the first letter at the end
    Quote Originally Posted by Philip M View Post
    As I understand it the OP is not looking for aa word scrambler. He wants to reverse the order of the characters in a sentence.
    i'm a she , and i want to put the first letter of each word at the end of each word

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    Quote Originally Posted by talaaa View Post
    but i want it to act on each word not the entire sentence as one!

    not a scrambler, i just want to put the first letter at the end

    i'm a she , and i want to put the first letter of each word at the end of each word
    You did not make that very clear! I hope we are done now!

    Code:
    <script type = "text/javascript">
    
    function flip(form) {
    var str = form.revtext.value;
    var revstr = str.split(" ");
    for (var i=0; i<revstr.length; i++) {
    var first = revstr[i].charAt(0);
    var rest = revstr[i].substring(1);
    revstr[i] = rest+first;
    }
    revstr = revstr.join(" ");
    form.revtext.value = revstr;
    }
    
    </script>
    
    <form>
    <input type="text" name="revtext" value= "Hello Cold Cruel World">
    <input type="button" value="Reverse" onClick="flip(this.form)">
    </form>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #11
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    You did not make that very clear! I hope we are done now!

    Code:
    <script type = "text/javascript">
    
    function flip(form) {
    var str = form.revtext.value;
    var revstr = str.split(" ");
    for (var i=0; i<revstr.length; i++) {
    var first = revstr[i].charAt(0);
    var rest = revstr[i].substring(1);
    revstr[i] = rest+first;
    }
    revstr = revstr.join(" ");
    form.revtext.value = revstr;
    }
    
    </script>
    
    <form>
    <input type="text" name="revtext" value= "Hello Cold Cruel World">
    <input type="button" value="Reverse" onClick="flip(this.form)">
    </form>
    thank youu and sorry for the inconvenience but i was wondering, can't i use a string tokenizer?

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    Quote Originally Posted by talaaa View Post
    thank youu and sorry for the inconvenience but i was wondering, can't i use a string tokenizer?
    No. that is a method of Java, not Javascript. Have we been doing your homework for you?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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