Strings in ActionScript

Type a sentence and press enter to create the cryptogram.

Get Adobe Flash player

stage.addEventListener(KeyboardEvent.KEY_DOWN,entry);
function entry(e:KeyboardEvent):void{
	if(e.keyCode==13) {
		encrypt();
	} //pressed enter
} //entry
function encrypt():void {
	var alph:String="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var sub:String=shuffle(alph);
	var s:String=txtInput.text;
	s=s.toUpperCase();
	var s2:String="";
	var i:int;
	for(i=0;i<s.length;i++) {
		var ch:String=s.charAt(i);
		var pos=alph.indexOf(ch); //is it a letter?
		if(pos>=0) //letter
			s2+=sub.charAt(pos);
		else s2+=ch; //other character
	}//loop to replace
	txtOutput.text=s2;
}
function shuffle(s:String):String {
	var letters:Array=s.split(""); //make an array of the letters
	var i, j:int; 
	var temp:String; //when we swap two letters, we store one of them temporarily
	for(i=0;i<letters.length;i++) { //loop through the letters
		j=Math.random()*letters.length; //random letter
		while(j!=i && s.charAt(j)!=letters[j]) {
			j=Math.random()*letters.length; //try again
		} //get a replacement letter
		//swap letters i and j
		temp=letters[i]; //store letter[i]temporarily so it isn't erased in the next step 
		letters[i]=letters[j]; //replace letter[i] with letter[j]
		letters[j]=temp; //put former letter [i]from temp  into letter[j]
	} //loop to scramble
	var s2:String="";
	for(i=0;i<letters.length;i++) { //loop to make string2 from letters in array
		s2=s2+letters[i];  //concatenate with the +
	} //loop to concatenate the scrambled letters
	return s2;
} //shuffle
Download the movie.

NEXT: Text Files