
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Swap Pair of Characters in Java
In this article, we'll learn how to swap pairs of characters in a Java string. We start with a simple method that converts the string into a character array, allowing us to swap characters directly. Then, we'll explore an object-oriented approach where the swapping logic is encapsulated in a separate method. Both approaches highlight key Java concepts such as string manipulation and method encapsulation.
Problem Statement
Write a Java program to swap a pair of characters. Below is a demonstration of the same ?
Input
Input string: Java program
Output
The string after swapping is: Javg proaram
Steps for basic character swapping in a string
Following are the steps for basic character swapping in a string ?
- START
- Declare a string input_string with the value "Java program".
- Convert the string into a character array named character.
- Identify the character's positions to swap (3rd and 7th from the end).
- Swap the characters using a temporary variable temp.
- Convert the character array back into a string.
- Display the new string after swapping.
- STOP.
Java program to swap pairs of characters
Here, we bind all the operations together under the main function ?
public class SwapCharacter { public static void main(String args[]) { String input_string = "Java program"; System.out.println("The string is defined as: " + input_string); int i = 3, j = input_string.length() - 4; char character[] = input_string.toCharArray(); char temp = character[i]; character[i] = character[j]; character[j] = temp; String result = new String(character); System.out.println("\nThe string after swapping is: " + result); } }
Output
The string is defined as: Java program The string after swapping is: Javg proaram
Code Explanation
In this example, the program starts by defining a string input_string with the value "Java program". The toCharArray() method is used to convert this string into a character array, which allows manipulation of individual characters. The program identifies the characters' positions to be swapped?3rd and 7th from the end?using array indices. A temporary variable temp holds one character while the swap is performed. After swapping, the character array is converted back into a string using the new String(character) constructor. Finally, the modified string is printed. This example makes it straightforward and focused on basic string manipulation.
Steps to swap pairs of characters using object-oriented approach
Following are the steps to swap pairs of characters using object-oriented approach ?
- START
- Define a static method swap() that accepts a string and two positions, and returns a modified character array.
- Convert the string into a character array within the swap() method.
- Swap the characters at the given positions using a temporary variable.
- Return the modified character array from the method.
- Declare a string input_string with the value Java program.
- Call the swap() method with input_string and the character's positions to be swapped.
- Print the modified character array returned by the swap() method.
- End the program.
Java program to swap pairs of characters using object-oriented approach
Here, we encapsulate the operations into functions exhibiting object-oriented programming ?
public class SwapCharacter { static char[] swap(String input_string, int i, int j) { char character[] = input_string.toCharArray(); char temp = character[i]; character[i] = character[j]; character[j] = temp; return character; } public static void main(String args[]) { String input_string = "Java program"; System.out.println("The string is defined as: " + input_string); System.out.println("\nThe string after swapping is: "); System.out.println(swap(input_string, 3, input_string.length() - 4)); } }
Output
The string is defined as: Java program The string after swapping is: Javg proaram
Code Explanation
In this example, the program follows object-oriented principles by using encapsulation in the swapping logic in a static method swap(). This method takes the input string and the positions of the characters to be swapped as parameters. Inside swap(), the string is converted into a character array to facilitate character manipulation. The characters at the specified positions are swapped using a temporary variable temp, and the modified character array is returned. The main method calls swap() with the string "Java program" and the specified indices. The modified character array is printed directly in the main method. This approach makes the code reusable and modular, although it still does not involve any loops. The use of a static method demonstrates a basic application of object-oriented programming principles in Java.