0% found this document useful (0 votes)
89 views

Recursion

This document contains code snippets demonstrating different string manipulation techniques in Java, including: 1) Recursively reversing a string by concatenating the last character and reversing the remaining substring. 2) Iteratively reversing a string by splitting on spaces and concatenating substrings in reverse order. 3) Additional examples show substring usage, string indexing, and reversing strings with or without spaces.

Uploaded by

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

Recursion

This document contains code snippets demonstrating different string manipulation techniques in Java, including: 1) Recursively reversing a string by concatenating the last character and reversing the remaining substring. 2) Iteratively reversing a string by splitting on spaces and concatenating substrings in reverse order. 3) Additional examples show substring usage, string indexing, and reversing strings with or without spaces.

Uploaded by

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

import java.util.

Map;
import org.apache.commons.lang.ArrayUtils;
public class Main {
public static void main(String[] args) {
String[][] countries = { { "United States", "New York" }, { "United Kingdom"
, "London" },
{ "Netherland", "Amsterdam" }, { "Japan", "Tokyo" }, { "France", "Paris"
} };
Map countryCapitals = ArrayUtils.toMap(countries);
System.out.println("Capital of Japan is " + countryCapitals.get("Japan"));
System.out.println("Capital of France is " + countryCapitals.get("France"));
}
}
==============================================
Java String array
public class JavaStringArrayTests2
{
private String[] toppings = {"Cheese", "Pepperoni", "Black Olives"};
// our constructor; print out the String array here
public JavaStringArrayTests2()
{
// new `for` loop
for (String s: toppings)
{
System.out.println(s);
}
}
// main kicks everything off.
// create a new instance of our class here.
public static void main(String[] args)
{
new JavaStringArrayTests2();
}
}
=============================================================
Given two sorted arrays, merge them such that the elements are not repeated
Eg 1: Input:
Array 1: 2,4,5,6,7,9,10,13
Array 2: 2,3,4,5,6,7,8,9,11,15
Output:
Merged array: 2,3,4,5,6,7,8,9,10,11,13,15
import java.util.Arrays;
public class Two{
public static void main(String args[]) throws Exception {
int a[] = {10, 20, 30};
int b[]= {6,9, 14, 11};
int res[]=new int[a.length+b.length];
System.arraycopy(a,0, res, 0, a.length);

System.arraycopy(b,0,res,a.length, b.length);
Arrays.sort(res);
System.out.println(Arrays.toString(res));
} }
===============================================================
4. Find if a String2 is substring of String1. If it is, return the index of the
first occurrence. else return -1.
Eg 1:Input:
String 1: test123string
String 2: 123
Output: 4
Eg 2: Input:
String 1: testing12
String 2: 1234
Output: -1
String Str = new String("test123string");
String SubStr1 = new String("123");
String SubStr2 = new String("Sutorials");
/* System.out.print("Found Index :" );
System.out.println(Str.indexOf( 't' ));
System.out.print("Found Index :" );
System.out.println(Str.indexOf( 'o', 5 ));*/
System.out.print("Found Index :" );
System.out.println( Str.indexOf( SubStr1 ));
/* System.out.print("Found Index :" );
System.out.println( Str.indexOf( SubStr1, 15 ));
System.out.print("Found Index :" );
System.out.println(Str.indexOf( SubStr2 )); */
=========================================================================
http://examples.javacodegeeks.com/core-java/lang/string/java-string-reverse-exam
ple/
================================================================================
====
================================================================================
=============
String str = "Java";
String reverseStr = reverseRecurive(str);
System.out.println("Normal String is : " + str + " \nReverse String is : "
+reverseStr);
}

public static String reverseRecurive(String str) {

if (str.length() <= 1) {
return str;

return reverseRecurive(str.substring(1)) + str.charAt(0);


================================================================================
==
public class Two{
public static void main(String args[]) {
String str = "Java is program";
String reverseStr = Reverse(str);
System.out.println("Normal String is : " + str + " \nReverse Strin
g is : "+reverseStr);
}

private static String Reverse(String str) {


char charArray[] = str.toCharArray();
for (int i = 0; i <str.length(); i++){
if(charArray[i] == ' ')
return Reverse(str.substring(i + 1)) + str.substring(0,
i) + " ";
}
return str + " ";
} }
=============================================================
public class Two{
public static void main(String args[]) {
String str = "Java is program ";
// System.out.println ( str.substring(0,5));
String reverseStr = Reverse(str);
System.out.println("Normal String is : " + str + " \nReverse Strin
g is : "+reverseStr);
}

private static String Reverse(String str) {


// System.out.println("during iterations " +str);
char charArray[] = str.toCharArray();
for (int i = 0; i <str.length(); i++){

if(charArray[i] == ' ')


{

System.out.println(i+"is " +str);


//System.out.println("iterating : "+str.substrin
g(i + 1) + str.substring(0, i) + " ");
return Reverse(str.substring(i + 1) ) + str.substring(0,
i) +" ";
//is program +java+""
//program + is
}
}
return str ;
} }
=============================================================
1st call reverseRecurive("Java") : will return reverseRecurive("ava") + "J"
2nd call reverseRecurive("ava") : will return reverseRecurive("va") + "a"
3rd call reverseRecurive("va") : will return reverseRecurive("a") + "v"
4th call reverseRecurive("a") : will return "a"
3rd call will return : reverseRecurive("a") + v =
a + v
2nd call
will return : reverseRecurive("va") + a = a + v + a
1st call
will return : reverseRecurive("ava") + J = a + v + a + J
================================================================================
=============
String str = "Java";
String reverseStr = reverseRecurive(str);
System.out.println("Normal String is : " + str + " \nReverse String is : "
+reverseStr);
}

public static String reverseRecurive(String str) {

if (str.length() <= 1) {
return str;
}

return reverseRecurive(str.substring(1)) + str.charAt(0);


================================================================================
==
public class Two{
public static void main(String args[]) {
String str = "Java is program";
String reverseStr = Reverse(str);
System.out.println("Normal String is : " + str + " \nReverse Strin

g is : "+reverseStr);
}

private static String Reverse(String str) {


char charArray[] = str.toCharArray();
for (int i = 0; i <str.length(); i++){
if(charArray[i] == ' ')
return Reverse(str.substring(i + 1)) + str.substring(0,
i) + " ";
}
return str + " ";
} }
================================
public class Three {
public static void main(String[] args) {
// TODO Auto-generated method stub

}
}

You might also like