Posted By

kanuwadhwa on 03/19/11


Tagged

actionscript flash palindrome Flex


Versions (?)

Palindrome


 / Published in: ActionScript 3
 

Looking forward for the most optimized solution for the same

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
  3. <mx:Script>
  4. <![CDATA[
  5. private var str:String = "abcba";
  6. public function init():void{
  7. var len:int = str.length;
  8. for (var i:int = 0; i < len/2; i++)
  9. {
  10. var c1:String = str.charAt(i);
  11. var c2:String = str.charAt(len - i - 1); // 5-0-1 = 4, 5-1-1=3, 5-2-1 =2
  12. txt.text = "\n f1>> "+c1+ " >>>b1>> "+c2
  13. if (c1 != c2)
  14. {
  15. txt.text += "\n false";
  16. }
  17. else
  18. {
  19. txt.text += "\n true";
  20. }
  21. }
  22. }
  23. ]]>
  24. </mx:Script>
  25. <mx:TextArea x="118" y="57" width="553" height="412" id="txt"/>
  26. </mx:Application>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: ElntCor on March 19, 2011

This is not completely correct. You need to add a break, otherwise it shows true when only the character(s) in the middle are equal:

if (c1 != c2){ txt.text += "\n false"; break; }else{ txt.text += "\n true"; }

You need to login to post a comment.