Reversing of char array using java
Hi everyone, in this blog post I will writing about reversing a char array using java. This program requires a char array as its input and it will return the reversed version of that char array.
The program which I have written to demonstrate reversing of char array is as follows:
The program which I have written to demonstrate reversing of char array is as follows:
package com.techjourney.methods;
public class ReversingCharArray {
public static void main(String args[])
{
char firstArray[] = new char[]{'G','i','l','c','h','r','i','s','t'};
char secondArray[] = new char[]{'D','r','a','v','i','d'};
System.out.println(" First array before reversing - ");
System.out.println(firstArray);
System.out.println(" First array after reversing - ");
charReversal(firstArray);
System.out.println(firstArray);
System.out.println(" Second array before reversing - ");
System.out.println(secondArray);
System.out.println(" Second array after reversing - ");
charReversal(secondArray);
System.out.println(secondArray);
}
public static char[] charReversal(char inputArray[])
{
if(inputArray==null)
{
throw new NullPointerException();
}
int lengthOfArray = inputArray.length;
int elementPosition = lengthOfArray-1;
char tempVariable;
for(int i=0;i<lengthOfArray/2;i++)
{
tempVariable=inputArray[i];
inputArray[i]=inputArray[elementPosition-i];
inputArray[elementPosition-i] = tempVariable;
}
return inputArray;
}
}
public class ReversingCharArray {
public static void main(String args[])
{
char firstArray[] = new char[]{'G','i','l','c','h','r','i','s','t'};
char secondArray[] = new char[]{'D','r','a','v','i','d'};
System.out.println(" First array before reversing - ");
System.out.println(firstArray);
System.out.println(" First array after reversing - ");
charReversal(firstArray);
System.out.println(firstArray);
System.out.println(" Second array before reversing - ");
System.out.println(secondArray);
System.out.println(" Second array after reversing - ");
charReversal(secondArray);
System.out.println(secondArray);
}
public static char[] charReversal(char inputArray[])
{
if(inputArray==null)
{
throw new NullPointerException();
}
int lengthOfArray = inputArray.length;
int elementPosition = lengthOfArray-1;
char tempVariable;
for(int i=0;i<lengthOfArray/2;i++)
tempVariable=inputArray[i];
inputArray[i]=inputArray[elementPosition-i];
inputArray[elementPosition-i] = tempVariable;
}
return inputArray;
}
}
In the above program, the function 'charReversal' reverses the input array. It does the reversing by the following steps:
- Finds the number of characters in the array and assigns it to variable 'lengthOfArray'.
- Iterate a loop from 0 to lengthOfArray/2.
- In the loop interchange the variables between first location and last location using a temp variable.
- Increment the pointer from first location and decrement the pointer from last location and continue the loop.
The output of the following program is as follows;
First array before reversing -
Gilchrist
First array after reversing -
tsirhcliG
Second array before reversing -
Dravid
Second array after reversing -
divarD
Gilchrist
First array after reversing -
tsirhcliG
Second array before reversing -
Dravid
Second array after reversing -
divarD
The length of arrays have odd as well as even values. This example was taken to demonstrate the program works for all the input values.
I hope everyone understood this program very well. Please do comment if you have any doubts or other queries.
Thank you everyone for reading the post.
Comments
Post a Comment