PROGRAM 7 : ENTER A STRING AND REPLACE ALL OCCURENCES OF VOWELS WITH THE ALPHABET FOLLOWING IT , INTO THE STRING.
Example:
{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string to replace its vowels:");
String str = sc.nextLine();
str = str.trim(); // WILL REMOVE ALL EXTRA SPACES BEFORE AND AFTER THE STRING
str = str.toUpperCase(); // WILL CHANGE ALL CHARACTERS TO UPPERCASE
int a = str.length();
char ch;
int b;
for(int i = 0 ; i <a ; i++){
ch = str.charAt(i); // EACH CHARACTER WILL BE CHECKED WHETHER IT IS A VOWEL OR NOT.
b = (int)ch; // WILL FIND THE ASCII VALUE OF CHARACTER
switch(ch) // A TYPE OF DECISION MAKING STATEMENT THAT CHECKS CHARACTER WITH EVERY CASE
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
b = b+1; // WILL INCREASE THE ASCII VALUE BY 1
str = str.replace(ch , (char)b); // WILL REPLACE ALL OCCURENCES OF THE VOWEL WITH NEXT ALPHABET IN ORDER
break;
default:
}
}
System.out.println("CHANGED STRING:");
System.out.println(str); // WILL OUTPUT THE CHANGED STRING
}
}
Example:
PROGRAM:
import java.util.Scanner;
public class replacingvowels2{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string to replace its vowels:");
String str = sc.nextLine();
str = str.trim(); // WILL REMOVE ALL EXTRA SPACES BEFORE AND AFTER THE STRING
str = str.toUpperCase(); // WILL CHANGE ALL CHARACTERS TO UPPERCASE
int a = str.length();
char ch;
int b;
for(int i = 0 ; i <a ; i++){
ch = str.charAt(i); // EACH CHARACTER WILL BE CHECKED WHETHER IT IS A VOWEL OR NOT.
b = (int)ch; // WILL FIND THE ASCII VALUE OF CHARACTER
switch(ch) // A TYPE OF DECISION MAKING STATEMENT THAT CHECKS CHARACTER WITH EVERY CASE
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
b = b+1; // WILL INCREASE THE ASCII VALUE BY 1
str = str.replace(ch , (char)b); // WILL REPLACE ALL OCCURENCES OF THE VOWEL WITH NEXT ALPHABET IN ORDER
break;
default:
}
}
System.out.println("CHANGED STRING:");
System.out.println(str); // WILL OUTPUT THE CHANGED STRING
}
}
No comments:
Post a Comment