Saturday, November 9, 2013

Reverse Letters or Words [Actionscript3]

This is my first post. I’m still a noob to as3. so, every intersting thing I do will be posted in this blog.
Below is a simple script written by me to reverse the letters and words of a String.




<Code>


//creating a new String and print it
var myText:String = new String ("Hello Viewer How Are You");
trace(myText);
//assigning a variable for final output
var reverseLetters = "";
//splitting the String into Letters
var letters:Array = myText.split("");
// making reverseLetters String using a for loop
for(var i=myText.length-1;i>-1;i--){
reverseLetters = reverseLetters + letters[i];
}
//printing the final output of reversed letters
trace(reverseLetters);


/* As same way above to reverse the words, only thing new is splitting myText string where spaces are */
var reverseWords = "";
var words:Array = myText.split(" ");
// for the for loop j reffers the (number of words - 1)
for(var j=4;j>-1;j--){
reverseWords = reverseWords + words[j] + " ";
}
trace(reverseWords);

No comments:

Post a Comment