Hi Guys,
Need some api for string comparison and for also shuffling characters in a string.
Ex: local string = “Sudheer”
shuffled string should be some thing like “herduse”…
[import]uid: 50443 topic_id: 17204 reply_id: 317204[/import]
Hi Guys,
Need some api for string comparison and for also shuffling characters in a string.
Ex: local string = “Sudheer”
shuffled string should be some thing like “herduse”…
[import]uid: 50443 topic_id: 17204 reply_id: 317204[/import]
Try this:
local function shuffleString(inputStr)
local outputStr = "";
local strLength = string.len(inputStr);
while (strLength ~=0) do
--get a random character of the input string
local pos = math.random(strLength);
--insert into output string
outputStr = outputStr..string.sub(inputStr,pos,pos);
--remove the used character from input string
inputStr = inputStr:sub(1, pos-1) .. inputStr:sub(pos+1);
--get new length of the input string
strLength = string.len(inputStr);
end
return outputStr;
end
local function main()
local testString = "Shuffle Me!!";
print("Before Shuffle: ",testString);
testString = shuffleString(testString);
print("After Shuffle: ",testString);
end
--run the code:
main();
cheers [import]uid: 33608 topic_id: 17204 reply_id: 64971[/import]
One more thing about this code, you would probsbly need to change the random seed, so you don’t always get the same result.
Just add this code to the first line of the function:
math.randomseed( os.time() );
[import]uid: 33608 topic_id: 17204 reply_id: 64972[/import]
Thaks dude, It’s great response…
Y there no API’s like strcmp and other string manipulation API’s?
Also noticed that math.rand is not working… .using floor [import]uid: 50443 topic_id: 17204 reply_id: 65046[/import]
LUA (and corona) has some pretty powerful string functions:
http://developer.anscamobile.com/content/strings
math.random should be working (not math.rand, check your spelling).
If you mean math.round(), then its available from build 514.
Hope this helps… [import]uid: 33608 topic_id: 17204 reply_id: 65082[/import]