1. Write a function that replace vowels within the string star(*). For example v
ID: 3929217 • Letter: 1
Question
1. Write a function that replace vowels within the string star(*).
For example vowels are 'a','e','i','o','u' so if my string is "red is awesome!" the expected output should be "s*r* *s *ws*m*"
2. Write a function that takes an array and reverse the array elements.
For example if array[4] = {2, 5, 1, 6} expected output should be {6, 1, 5, 2}
3. Write a function that swaps the value of two variables WITHOUT the temporary variable.
4. Write a function that takes a large integer and add it's digit and returns the sum of digits.
For example int num = 12345 and the sum of the digits in num is 15
1. Write a function that replace vowels within the string star(*).
For example vowels are 'a','e','i','o','u' so if my string is "red is awesome!" the expected output should be "s*r* *s *ws*m*"
2. Write a function that takes an array and reverse the array elements.
For example if array[4] = {2, 5, 1, 6} expected output should be {6, 1, 5, 2}
3. Write a function that swaps the value of two variables WITHOUT the temporary variable.
4. Write a function that takes a large integer and add it's digit and returns the sum of digits.
For example int num = 12345 and the sum of the digits in num is 15
Explanation / Answer
ArrayOperation.java
import java.util.Arrays;
public class ArrayOperation {
public static void main(String[] args) {
String s = "red is awesome!";
System.out.println(replaceVowel(s));
int array[] = {2, 5, 1, 6} ;
reverseArray(array);
System.out.println(Arrays.toString(array));
swap(5, 4);
System.out.println("Sum of digits: "+sumDigits(12345));
}
public static int sumDigits(int n) {
int sum = 0;
do {
sum += n % 10;
} while ((n = n / 10) != 0);
return sum;
}
public static void reverseArray(int a[]){
for(int i = 0; i < a.length / 2; i++)
{
int temp = a[i];
a[i] = a[a.length - i - 1];
a[a.length - i - 1] = temp;
}
}
public static void swap(int a, int b){
a = a+b;
b = a-b;
a = a-b;
System.out.println("A = "+a+" B = "+b);
}
public static String replaceVowel(String s){
String newStr = "";
for(int i=0; i<s.length(); i++){
char ch = s.charAt(i);
if(ch=='a' ||ch=='e' ||ch=='i' ||ch=='o' ||ch=='a' ){
newStr = newStr + "*";
}
else{
newStr = newStr + ch;
}
}
return newStr;
}
}
Output:
r*d *s *w*s*m*!
[6, 1, 5, 2]
A = 4 B = 5
Sum of digits: 15
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.