import java.util.Scanner; interface StringFunction { String reverse(String input); } public class StringLambdaExample1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); StringFunction reverseFunction = (str) -> { String reversedString = ""; for (int index = str.length() - 1; index >= 0; index--) { reversedString += str.charAt(index); } return reversedString; }; String fixedString = " javaTpoint"; System.out.println("Reversed word using lambda function: " + reverseFunction.reverse(fixedString)); System.out.println("Enter a string to be reversed:"); String userInput = scanner.nextLine(); System.out.println("After reversing: " + reverseFunction.reverse(userInput)); scanner.close(); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter