mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-02-04 12:50:12 +08:00
docs: update the whole repository
* fix some bugs * delete duplicate files * format code
This commit is contained in:
@@ -1,46 +1,46 @@
|
||||
package Others;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
* This method produces a reversed version of a string
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
* @author Unknown
|
||||
*/
|
||||
class ReverseString
|
||||
{
|
||||
|
||||
/**
|
||||
* This method reverses the string str and returns it
|
||||
* @param str String to be reversed
|
||||
* @return Reversed string
|
||||
*/
|
||||
public static String reverse(String str){
|
||||
if(str.isEmpty() || str == null) return str;
|
||||
|
||||
char arr[] = str.toCharArray();
|
||||
for(int i = 0, j = str.length() - 1; i < j; i++, j--){
|
||||
class ReverseString {
|
||||
|
||||
/**
|
||||
* This method reverses the string str and returns it
|
||||
*
|
||||
* @param str String to be reversed
|
||||
* @return Reversed string
|
||||
*/
|
||||
public static String reverse(String str) {
|
||||
if (str.isEmpty() || str == null) return str;
|
||||
|
||||
char arr[] = str.toCharArray();
|
||||
for (int i = 0, j = str.length() - 1; i < j; i++, j--) {
|
||||
char temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
}
|
||||
return new String(arr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Main Method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
* @throws IOException Exception thrown because of BufferedReader
|
||||
*/
|
||||
public static void main(String args[]) throws IOException
|
||||
{
|
||||
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
|
||||
System.out.println("Enter the string");
|
||||
String srr=br.readLine();
|
||||
System.out.println("Reverse="+reverse(srr));
|
||||
br.close();
|
||||
}
|
||||
return new String(arr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Main Method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
* @throws IOException Exception thrown because of BufferedReader
|
||||
*/
|
||||
public static void main(String args[]) throws IOException {
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
System.out.println("Enter the string");
|
||||
String srr = br.readLine();
|
||||
System.out.println("Reverse=" + reverse(srr));
|
||||
br.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user