Write a program to accept a word and convert it into lowercase if it is in uppercase and display the new word by replacing only the vowels with characters following it using scanner class ,example:computer=cpmpvtfr (in java)
Answers
public class StringOp
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the word: ");
String str = sc.next(); //str stores original input
str = str.toLowerCase(); //str is now converted to Lower Case
/* ASCII CODES LIST:
* a - 97
* e - 101
* i - 105
* o - 111
* u - 117
*/
String newstr = ""; //newstr is the New String. It will store the modified word
for(int i=0;i<str.length();i++)
{
int ch = (int)str.charAt(i); //ch is the ASCII value of each character of str
if((ch==97)||(ch==101)||(ch==105)||(ch==111)||(ch==117)) //Condition for checking if ch corresponds to a vowel
{
ch++;
newstr += (char)ch;
}
else
{
newstr += (char)ch;
}
}
System.out.println("New String: "+newstr);
}
}
Answer:
import java.util.*;
public class Lowercase
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a word: ");
String st = in.nextLine();
st = st.toLowerCase();
String newSt = "";
int l = st.length();
for (int i = 0; i < l; i++)
{
char ch = st.charAt(i);
if (st.charAt(i) == 'a' ||
st.charAt(i) == 'e' ||
st.charAt(i) == 'i' ||
st.charAt(i) == 'o' ||
st.charAt(i) == 'u')
{
char nextC = (char)(ch + 1);
newSt = newSt + nextC;
}
else
{
newSt = newSt + ch;
}
}
System.out.println(newSt);
}
}
Explanation: