Write a program in java to find the nearest vowel to the entered alphabet(Both in upper and lower case).(Don't use too advanced java and buffer reader)
Answers
Answered by
3
import java.util.Scanner;
public class Nearest_Vowel
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a character: ");
int asc = (int)sc.next().charAt(0);
/* Here
*
* sc.next() -> reads a String.
* sc.next().charAt(0) -> extracts the first character of that string
* (int)sc.next().charAt(0) -> gives the ASCII Value of that character
*
*/
/* ASCII CODE INFORMATION
*
* A-Z -> 65-90
* a-z -> 97-122
*
* A - 65
* E - 69
* I - 73
* O - 79
* U - 85
*
* a - 97
* e - 101
* i - 105
* o - 111
* u - 117
*
*
* There can be characters which can be equidistant from two Vowels. So, we need multiple runs of a loop.
*
*/
int VOW[] = {65,69,73,79,85}; //Array of ASCII Codes for Capital Vowels
int vow[] = {97,101,105,111,117}; //Array of ASCII Codes for Small Vowels
if((asc>=65)&&(asc<=90)) //Considering Capital Letter Sequence
{
int diff[] = new int[5]; //diff[] will contain the difference between input character and Vowels
for(int i=0;i<5;i++) //This loop calculates all elements of diff[] array
{
if(asc>VOW[i])
{
diff[i] = asc - VOW[i];
}
else
{
diff[i] = VOW[i] - asc;
}
}
int min = diff[0]; //min is the Minimum Difference
int pos = 0; //pos is the Array Position corresponding to Minimum Value of diff[]
for(int i=0;i<5;i++) //This loop finds minimum value in diff[]
{
if(diff[i]<min)
{
min = diff[i];
pos = i;
}
}
/* Now, some characters are equidistant from two vowels. So we need to make another loop to check. */
int pos2=-1; //pos2 will contain Array Position of another equidistant vowel, if present. Else it will be negative
for(int i=0;i<5;i++) //This loop finds if there is another diff[] element with same value as in min
{
if(i==pos)
{
continue;
}
else
{
if(diff[i]==min) //If another equidistant value is found, then Array Position is stored in pos2
{
pos2 = i;
}
}
}
if(pos2<0) //Negative pos2 means that there is no second vowel as the answer
{
System.out.println("Nearest Vowel is: "+((char)VOW[pos]));
}
else
{
System.out.println("Nearest Vowels are both: "+((char)VOW[pos])+" and "+((char)VOW[pos2]));
}
}
else if((asc>=97)&&(asc<=122)) //Considering Small Letter Sequence
{
int diff[] = new int[5];
for(int i=0;i<5;i++)
{
if(asc>vow[i])
{
diff[i] = asc - vow[i];
}
else
{
diff[i] = vow[i] - asc;
}
}
int min = diff[0];
int pos = 0;
for(int i=0;i<5;i++)
{
if(diff[i]<min)
{
min = diff[i];
pos = i;
}
}
int pos2=-1;
for(int i=0;i<5;i++)
{
if(i==pos)
{
continue;
}
else
{
if(diff[i]==min)
{
pos2 = i;
}
}
}
if(pos2<0)
{
System.out.println("Nearest Vowel is: "+((char)vow[pos]));
}
else
{
System.out.println("Nearest Vowels are both: "+((char)vow[pos])+" and "+((char)vow[pos2]));
}
}
else //Considering the case when input character is not an Alphabet
{
System.out.println("Not an Alphabet");
}
}
}
public class Nearest_Vowel
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a character: ");
int asc = (int)sc.next().charAt(0);
/* Here
*
* sc.next() -> reads a String.
* sc.next().charAt(0) -> extracts the first character of that string
* (int)sc.next().charAt(0) -> gives the ASCII Value of that character
*
*/
/* ASCII CODE INFORMATION
*
* A-Z -> 65-90
* a-z -> 97-122
*
* A - 65
* E - 69
* I - 73
* O - 79
* U - 85
*
* a - 97
* e - 101
* i - 105
* o - 111
* u - 117
*
*
* There can be characters which can be equidistant from two Vowels. So, we need multiple runs of a loop.
*
*/
int VOW[] = {65,69,73,79,85}; //Array of ASCII Codes for Capital Vowels
int vow[] = {97,101,105,111,117}; //Array of ASCII Codes for Small Vowels
if((asc>=65)&&(asc<=90)) //Considering Capital Letter Sequence
{
int diff[] = new int[5]; //diff[] will contain the difference between input character and Vowels
for(int i=0;i<5;i++) //This loop calculates all elements of diff[] array
{
if(asc>VOW[i])
{
diff[i] = asc - VOW[i];
}
else
{
diff[i] = VOW[i] - asc;
}
}
int min = diff[0]; //min is the Minimum Difference
int pos = 0; //pos is the Array Position corresponding to Minimum Value of diff[]
for(int i=0;i<5;i++) //This loop finds minimum value in diff[]
{
if(diff[i]<min)
{
min = diff[i];
pos = i;
}
}
/* Now, some characters are equidistant from two vowels. So we need to make another loop to check. */
int pos2=-1; //pos2 will contain Array Position of another equidistant vowel, if present. Else it will be negative
for(int i=0;i<5;i++) //This loop finds if there is another diff[] element with same value as in min
{
if(i==pos)
{
continue;
}
else
{
if(diff[i]==min) //If another equidistant value is found, then Array Position is stored in pos2
{
pos2 = i;
}
}
}
if(pos2<0) //Negative pos2 means that there is no second vowel as the answer
{
System.out.println("Nearest Vowel is: "+((char)VOW[pos]));
}
else
{
System.out.println("Nearest Vowels are both: "+((char)VOW[pos])+" and "+((char)VOW[pos2]));
}
}
else if((asc>=97)&&(asc<=122)) //Considering Small Letter Sequence
{
int diff[] = new int[5];
for(int i=0;i<5;i++)
{
if(asc>vow[i])
{
diff[i] = asc - vow[i];
}
else
{
diff[i] = vow[i] - asc;
}
}
int min = diff[0];
int pos = 0;
for(int i=0;i<5;i++)
{
if(diff[i]<min)
{
min = diff[i];
pos = i;
}
}
int pos2=-1;
for(int i=0;i<5;i++)
{
if(i==pos)
{
continue;
}
else
{
if(diff[i]==min)
{
pos2 = i;
}
}
}
if(pos2<0)
{
System.out.println("Nearest Vowel is: "+((char)vow[pos]));
}
else
{
System.out.println("Nearest Vowels are both: "+((char)vow[pos])+" and "+((char)vow[pos2]));
}
}
else //Considering the case when input character is not an Alphabet
{
System.out.println("Not an Alphabet");
}
}
}
QGP:
The code is complete and perfect. I have included comments wherever required
Similar questions