Swapping Pairs of Characters in a String in Java10 Sept 2024 | 5 min read Swapping characters within a string is a common operation in Java programming, involving rearranging individual characters to achieve a desired order. The process holds significance in various scenarios, such as data encryption, algorithmic transformations, or enhancing string manipulation capabilities within Java applications. Let's Explore various Java methods for character swapping and string generation. Method 1 (Using toCharArray)The Method involves converting the input string into a character array using the toCharArray Method, allowing direct manipulation of characters. It is suitable for scenarios where character-level modifications are needed. Filename: StringSwapperDemo.java Output: After swapping characters at positions 6 and 8: javatpniot After swapping characters at positions 0 and 9: tavatpoinj Original String: javatpoint Method 2 (Using subString())Utilizing the substring() Method, this Method extracts and manipulates substrings within the input string, making it helpful for swapping pairs of characters or performing specific operations on portions of the string. Filename: StringSwapperExample.java Output: After swapping characters at indices 6 and 8: javatpniot After swapping first and last characters: tavatpoinj Original String: javatpoint Method 3 (Using StringBuilder or StringBuffer)The technique utilizes the mutable StringBuilder or StringBuffer class, offering efficiency in string manipulations, especially for scenarios where multiple modifications are required in a string. It provides a dynamic and memory-efficient solution compared to immutable strings, allowing for in-place modifications. Filename: StringBuilderStringSwapper.java Output: After swapping characters at indices 3 and 8: javntpoiat After swapping first and last characters: tavatpoinj Original String: javatpoint Method 4 (Using XOR Operator)In this Method, characters are swapped using the XOR operator, which is a bitwise exclusive OR operation. It provides a concise way to exchange values without a temporary variable, making it an interesting and bitwise-efficient approach for swapping characters. Filename: StringSwapperXOR.java Output: After swapping characters at indices 3 and 8: javntpoiat After swapping first and last characters: tavatpoinj Original String: javatpoint |
Java, as a versatile programming language, offers developers various tools and constructs to manage and process data efficiently. Two of the most widely used mechanisms for working with data are Collections and Streams. Both serve distinct purposes and come with their own set of advantages and...
4 min read
Creating a Java body mass index (BMI) calculator necessitates putting multiple methods for calculating BMI using different formulas into practice. The Body Mass Index (BMI) is a tool used to determine an individual's body fat based on their height and weight. The modified BMI formula, the...
4 min read
Primitive data types in Java are predefined by the Java language and named as the reserved keywords. A primitive data type does not share a state with other primitive values. Java programming language supports the following eight primitive data types. Boolean data type byte data type int data type long...
5 min read
The java.nio.DoubleBuffer has array() method. The double array with support for this buffer is returned using the DoubleBuffer Class. Changes made to the contents of this buffer will also change the contents of the returned array. Before calling this function, invoke the hasArray() method to...
4 min read
Given an integer n, the task is to find a string of length n where every character appears an odd number of times. If n is odd, we can simply use one character, while if n is even, we can adjust one character to ensure all...
3 min read
In Java, the term "finalisation" describes the cleanup procedure that an object goes through before it is disposed of. The finalise() function, which comes from the java.lang.Object class, makes this procedure easier. Subclasses are meant to override the finalise() method in order to free up resources...
5 min read
The web crawler is basically a program that is mainly used for navigating to the web and finding new or updated pages for indexing. The crawler begins with a wide range of seed websites or popular URLs and searches depth and breadth to extract hyperlinks. The web...
10 min read
In Java, the Object class is the parent class of all the Java classes. Every Java class is a direct or indirect child of the Java Object class. Hence, every Java class extends the Object class. Therefore, we need not to write the following statement...
3 min read
In Java, the java.lang.NoClassDefFoundError is a class error that occurs when the Java Virtual Machine (JVM) is unable to load and initialize a class properly. This error is a runtime error, meaning that it happens when the program is running, not at compile time. This issue usually...
4 min read
The Boggle game is a popular word search puzzle where players attempt to find words hidden within a grid of letters. The goal is to trace paths through adjacent letters to form words according to predefined rules. In programming terms, solving the Boggle game involves...
14 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India