0% found this document useful (0 votes)
48 views1 page

Programming Fundamentals

The document provides two methods for reversing data: a recursive approach for reversing a string and a collection-based method for reversing an array of integers. The string reversal method checks for null or empty strings and uses recursion to build the reversed string. The array reversal method utilizes the Collections.reverse method to reverse the order of elements in the array and prints the result.

Uploaded by

Jaganath S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views1 page

Programming Fundamentals

The document provides two methods for reversing data: a recursive approach for reversing a string and a collection-based method for reversing an array of integers. The string reversal method checks for null or empty strings and uses recursion to build the reversed string. The array reversal method utilizes the Collections.reverse method to reverse the order of elements in the array and prints the result.

Uploaded by

Jaganath S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Reverse A String

Code:
Using recursion and substring:
 private static String reverse(String str)
    {
        // base case: if the string is null or empty
        if (str == null || str.equals("")) {
            return str;
        }
 
        // last character + recur for the remaining string
        return str.charAt(str.length() - 1) +
                reverse(str.substring(0, str.length() - 1));
    }
 

Reverse A Number

Using collection :
static void reverse(Integer myArray[])
    {
        Collections.reverse(Arrays.asList(myArray));
        System.out.println("Reversed Array:" + Arrays.asList(myArray));
    }

You might also like