Q : reverse a string without using any loops (use recursion)
A : public static String Reverse(String strParam) {
if (strParam.length() == 1) {
return strParam;
} else {
return Reverse(strParam.substring(1)) + strParam.substring(0, 1);
}
}
Q: Write a algorithm to find out the substring in a string and find out the number of occurrences also
A: public static void findSubString(String input, String subStr) {
int occurance = 0;
int inp_len = input.length();
int sub_len = subStr.length();
for (int i = 0; i < inp_len; i++) {
System.out.println("Scanning Character : " + input.charAt(i));
int charsmatched = 0;
if (input.charAt(i) == subStr.charAt(0)) {
charsmatched++;
for (int k = 1; k < sub_len; k++) {
if ((i < (inp_len - sub_len)) && (input.charAt(i + k) == subStr.charAt(k))) {
charsmatched++;
}
if (charsmatched == sub_len) {
occurance++;
System.out.println("Substring found : " + occurance + " times");
}
}
}
}
System.out.println("Total ocurances : " + occurance);
}
Q: 4. Find out the nth number in a fibonnaci series.
A:
public static long fibonacci(int n) {
long sum = 0;
if (n < 0) {
System.out.println("Enter a positive number");
} else if (n == 0) {
sum += 0;
return sum;
} else if (n == 1) {
sum += 1;
} else {
sum = fibonacci(n - 1) + fibonacci(n - 2);
}
return sum;
}
Q: 5. Sentence is given , u have to reverse words
A: public static void rvrsWords(String input) {
System.out.println("Original : " + input);
String output = " ";
if (input != null) {
String[] array = input.split(" ");
int len = array.length;
for (int i = 0; i < (len / 2); i++) {
String temp = array[i];
array[i] = array[len - i - 1];
array[len - i - 1] = temp;
output = " " + array[i];
}
System.out.println("Reversed Array : ");
for (int i = 0; i < (len); i++) {
System.out.println(array[i] + " ");
}
}
}
A 2 : private static String reverseString(String string) {
String reverse="",temp="";
for(int i=0;ichar c= string.charAt(i);
if(c==' ' && temp.length()>0){
reverse = temp + " " + reverse;
temp="";
}else{
temp += c;
}
}
reverse = temp + " " + reverse;
return reverse;
}
Q: Given array A[i] = i+1 for i = 0 to N-1
create array O[i] = product of all elements of array A except A[i] for i = 0 to N-1
eg: for N = 3, O[2] = A[0]*A[1]
O[1] = A[0]*A[2] (we cant multiple with A[1])
package geeks.threads;
public class Product {
public static void main(String[] args) {
int n =5;
int[] a= new int[n] ;
for(int i = 0 ; i < n ; i++) {
a[i] = i+1;
System.out.println(a[i]);
}
Product p = new Product();
p.product(a, 3);
}
public void product(int[] a, int x ) {
int product = 1;
for(int i = 0; i < a.length ; i++) {
if(!(i ==x)) {
product *= a[i];
}
}
System.out.println("Product is : "+ product);
}
}
A : public static String Reverse(String strParam) {
if (strParam.length() == 1) {
return strParam;
} else {
return Reverse(strParam.substring(1)) + strParam.substring(0, 1);
}
}
Q: Write a algorithm to find out the substring in a string and find out the number of occurrences also
A: public static void findSubString(String input, String subStr) {
int occurance = 0;
int inp_len = input.length();
int sub_len = subStr.length();
for (int i = 0; i < inp_len; i++) {
System.out.println("Scanning Character : " + input.charAt(i));
int charsmatched = 0;
if (input.charAt(i) == subStr.charAt(0)) {
charsmatched++;
for (int k = 1; k < sub_len; k++) {
if ((i < (inp_len - sub_len)) && (input.charAt(i + k) == subStr.charAt(k))) {
charsmatched++;
}
if (charsmatched == sub_len) {
occurance++;
System.out.println("Substring found : " + occurance + " times");
}
}
}
}
System.out.println("Total ocurances : " + occurance);
}
Q: 4. Find out the nth number in a fibonnaci series.
A:
public static long fibonacci(int n) {
long sum = 0;
if (n < 0) {
System.out.println("Enter a positive number");
} else if (n == 0) {
sum += 0;
return sum;
} else if (n == 1) {
sum += 1;
} else {
sum = fibonacci(n - 1) + fibonacci(n - 2);
}
return sum;
}
Q: 5. Sentence is given , u have to reverse words
A: public static void rvrsWords(String input) {
System.out.println("Original : " + input);
String output = " ";
if (input != null) {
String[] array = input.split(" ");
int len = array.length;
for (int i = 0; i < (len / 2); i++) {
String temp = array[i];
array[i] = array[len - i - 1];
array[len - i - 1] = temp;
output = " " + array[i];
}
System.out.println("Reversed Array : ");
for (int i = 0; i < (len); i++) {
System.out.println(array[i] + " ");
}
}
}
A 2 : private static String reverseString(String string) {
String reverse="",temp="";
for(int i=0;i
if(c==' ' && temp.length()>0){
reverse = temp + " " + reverse;
temp="";
}else{
temp += c;
}
}
reverse = temp + " " + reverse;
return reverse;
}
Q: Given array A[i] = i+1 for i = 0 to N-1
create array O[i] = product of all elements of array A except A[i] for i = 0 to N-1
eg: for N = 3, O[2] = A[0]*A[1]
O[1] = A[0]*A[2] (we cant multiple with A[1])
public class Product {
public static void main(String[] args) {
int n =5;
int[] a= new int[n] ;
for(int i = 0 ; i < n ; i++) {
a[i] = i+1;
System.out.println(a[i]);
}
Product p = new Product();
p.product(a, 3);
}
public void product(int[] a, int x ) {
int product = 1;
for(int i = 0; i < a.length ; i++) {
if(!(i ==x)) {
product *= a[i];
}
}
System.out.println("Product is : "+ product);
}
}
No comments:
Post a Comment