-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRecursion-7
More file actions
137 lines (131 loc) · 4.43 KB
/
Recursion-7
File metadata and controls
137 lines (131 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
Q1. Write a program to merge 2 strings alternately using recursion starting from the first input string.
i/p=abcd
efgh
o/p=aebfcg>h
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println("Enter the first string: ");
String a = scn.nextLine();
System.out.println("Enter the second string: ");
String b = scn.nextLine();
System.out.print(merge(a, b));
}
public static String merge(String a, String b){
String ans = "";
if(a.length() == 0){
ans += b;
return ans;
}
if(b.length() == 0){
ans += a;
return ans;
}
ans += a.substring(0, 1);
ans += b.substring(0, 1);
ans += merge(a.substring(1, a.length()), b.substring(1, b.length()));
return ans;
}
}
Q2. Given a string, find its first uppercase letter and return the remaining substring, starting from the
uppercase letter.
Explanation=
Input: collegeWallah
Expected Output: Wallah
import java.util.*;
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.println("Enter the input string: ");
String s = scn.nextLine();
System.out.println(rec(s, 0));
}
public static String rec(String s, int idx) {
if(idx == s.length()) return "";
if(Character.isUpperCase(s.charAt(idx))) return s.substring(idx);
return rec(s, idx+1);
}
}
Q3. Gicen a string` count total number of consonants in it. A consonant is an English alphabet character that is not cowel (a` e` i` o ani u). Examples of constants are b` c` i` f` ani g.
i/p=pwskills
o/p=7
import java.util.*;
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.println("Enter the input string: ");
String s = scn.nextLine();
System.out.println(totalConsonants(s, s.length()));
}
public static int totalConsonants(String str, int n) {
if (n == 1) {
if(isconsonant(str.charAt(0)))
return 1;
else
return 0;
}
if(isconsonant(str.charAt(n - 1)))
return totalConsonants(str, n - 1) + 1;
else
return totalConsonants(str, n - 1);
}
public static boolean isconsonant(char ch){
ch = Character.toLowerCase(ch);
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'){
return false;
}
return true;
}
}
Q4. Gifen a string, return the number of lowercase characters in it using recursion.
i/p=CollegeWallah
o/p=11
import java.util.*;
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.println("Enter the input string: ");
String str = scn.nextLine();
System.out.println(totallowercase(str, 0));
}
public static int totallowercase(String str, int idx) {
if(idx == str.length()-1){
if(str.charAt(idx) >= 97 && str.charAt(idx) <= 122){
return 1;
}
else{
return 0;
}
}
if(str.charAt(idx) >= 97 && str.charAt(idx) <= 122){
return totallowercase(str, idx+1) + 1;
}
else{
return totallowercase(str, idx+1);
}
}
}
Q5. Given a string, recursively implement atoi() or Integer.parseInt() methou on it without actually using
the methou.
i/p=298
o/p=298
import java.util.*;
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.println("Enter the input string: ");
String str = scn.nextLine();
System.out.println(rec(str, str.length()-1));
}
public static int rec(String str, int n) {
if (n == 0) {
return str.charAt(0) - '0';
}
return (rec(str, n - 1)*10 + str.charAt(n) - '0');
}
}