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