You need to sign in to do that
Don't have an account?
fiona gentry
How to Return an array of characters in reversed order for alphanumeric characters only however all other non-alphanumeric characters need to retain their original position
Dear gurus,
I have a problem where
GIVEN there is a string of characters, and i need to ivoke a reverse method is invoked such that it returns an array of characters in reversed order for alphanumeric characters only however all other non-alphanumeric characters need to retain their original position -
How to Implement solution in Apex?
I tried below code but no luck
Thanks,
Fiona
I have a problem where
GIVEN there is a string of characters, and i need to ivoke a reverse method is invoked such that it returns an array of characters in reversed order for alphanumeric characters only however all other non-alphanumeric characters need to retain their original position -
How to Implement solution in Apex?
I tried below code but no luck
public class assessment { public static List<String> reverseSpecialString(String input) { List<String> resultList = new List<String>(); // IMPLEMENT resultList = input.split(''); string reversedString=''; for(integer i=resultList.size()-1;i>=0;i--) { reversedString=reversedString+resultList[i]; } return resultList; } public static void validateSolution() { String input = 'ABC@HI#J2'; List<String> expected = new List<String> {'2','J','I','@','H','C','#','B','A'}; List<String> actual = reverseSpecialString(input); System.assertEquals(expected, actual, 'Invalid Results'); } }
Thanks,
Fiona
we can use isAlphaNumeric() method to check the string characters and reverse it , and add the nonalphanumeric values at the exact index position using map. Let me know if that helps.
Thanks
Surya G
All Answers
we can use isAlphaNumeric() method to check the string characters and reverse it , and add the nonalphanumeric values at the exact index position using map. Let me know if that helps.
Thanks
Surya G
what is the signifcance for adding I and stringMap.get(I) in resultList?
I'm taking the nonalphanumeric values and index as key in the map, so once I did with reversing the alphanumeric string, I add the nonalphanumeric values at their original index using map, where I is the index and stringMap.get(I) gives the nonalphanumeric value at that particular index.