I was exploring this code which gives a count of vowels and consonants, but didn't understand this else if (ch >= 'a' && ch <= 'z') line of code. Please tell me what's the logic behind it.
import java.util.Scanner;
public class Vowels {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter string");
String str = sc.nextLine();
int vowl = 0;
int conso = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowl++;
} else if (ch >= 'a' && ch <= 'z') {
conso++;
}
}
System.out.println(vowl);
System.out.println(conso);
}
}
A benefit of chars is that you can operate with them like if they were integers.
For example, you can do you this as well 'a' + 3 = 'd'
Meaning that 'a' < 'd' = true.
notice the if statement catches all vowels
whats ever is not a vowel will either be a capital letter, a number, a special character or consonants
else if (ch >= 'a' && ch <= 'z')
this checks if its not a vowel does it atleast fall in the range of small letter 'a'-'z' and is not a special charecter or a number.( we knonw its not a vowel but is it in the ascii range 26=a -51=z)
refer to the ASCII table to understand the range comparison
The comparison of characters the way it is done can create confusion, as you can see from Java: Character comparison.
Basically #TDG is correct by saying that ch is checked to be between 'a' and 'z', and thus the check might be translated as "is ch a lower case character?"
The tricky part is that depending on the language people use the expectation can be different, especially since language specific characters are not taken into account. In German language, 'รถ' would definitely qualify as lower case character but is not in the range of the check. The complexity may get evident by studying the Unicode code charts.
The best check is to use Character.isLowerCase().
char is a character that represented by a number which is the index of the character in the ASCII/unicode table, since the the alphabet characters are arranged in order in the ASCII table, the following code checks if the ch is in the range of the lowercase alphabet characters representation which is 97 to 122 in the table.
using (int) ch you can see the decimal value of the character and can compare it with the index in the ASCII table.
you can see the ASCII table here:https://www.asciitable.com/
Related
I came across a code which checks whether a character is between 'a' and 'z' case insensitive. However, I don't understand what the line after that is doing which is:
alphabets[c - 'a']++;
Could someone please explain this code to me?
alphabets = new int[26];
for (int i = 0; i < str.length(); i++)
{
char c = str.charAt(i);
if ('a' <= c && c <= 'z')
{
alphabets[c - 'a']++; // what does this do?
}
}
This code counts the number of times every lower-case letter appears in the strings. alphabets is an array where the first (i.e., index 0) index holds the number of as, the second the amount of bs, etc.
Subtracting a from the character will produce the relative index, and then ++ will increment the counter for that letter.
A char in Java is just a small integer, 16 bits wide. Generally speaking, the values it holds are the values that Unicode [aside: Java does not represent characters as "ASCII"] assigns to characters, but fundamentally, chars are just integers. Thus 'a' is the integer 0x0061, which can also be written as 97.
So, if you have value in the range 'a' to 'z', you have a value in the range 97 to 122. Subracting 'a' (subtracting 97) puts it in the range 0 to 25, which is suitable for indexing the 26-element array alphabets.
There is a code like this :
boolean isValid(int ch) {
if(ch < '1' | ch > '7' & ch != 'q') return false;
else return true;
}
class HelpClassDemo {
...
do {
choice=(char) System.in.read();
} while(!hlpobj.isValid(choice));
}
That's the question :
Why we used int ch in isValid(int ch) in spite of choice's type is char?
Shouldn't we use char ch ? If we should use int ch why there is a code like this : (ch < '1' | ch > '7' & ch != 'q')
Isn't ch<1 or ch>7 logical ? I know it's a quite simple question but I'm confused about this.
A int type as a wider range than a char type (see this data type range table). IMO, receiving a char as an int provides you with some kind of overflow protection (points of view on this are welcome) but it is not something I'd do as I'd use the right data type instead.
Now, when you compare
if(ch < '1' | ch > '7' & ch != 'q') return false;
you are implicitly casting '1' and '7' to int type, which is perfectly valid.
And finally, regarding
Isn't ch<1 or ch>7 logical ? I know it's a quite simple question but
I'm confused about this.
It is logical, but it's not the same.
If you do ch > '1' && ch < '7', you are comparing ch to the ASCII value of 7, which is 55, and value of 1, which is 48. Basically, you are making sure that ch is a char between 2 and 6, both inclusive.
But if you do ch > 1 && ch < 7, you are comparing integers and validating that the char represented by ch is between 2 and 6 (both inclusive). This char is very likely to be not-human-readable.
If you want to make them equivalent, you wold have to compare according to the appropiate char value, like
if (ch > 48 && ch < 55)
How do I get the numerical value/position of a character in the alphabet (1-26) in constant time (O(1)) without using any built in method or function and without caring about the case of the character?
If your compiler supports binary literals you can use
int value = 0b00011111 & character;
If it does not, you can use 31 instead of 0b00011111 since they are equivalent.
int value = 31 & character;
or if you want to use hex
int value = 0x1F & character;
or in octal
int value = 037 & character;
You can use any way to represent the value 31.
This works because in ASCII, undercase values are prefixed with 011, and uppercase 010 and then the binary equivalent of 1-26.
By using the bitmask of 00011111 and the AND operand, we covert the 3 most significant bits to zeros. This leaves us with 00001 to 11010, 1 to 26.
Adding to the very good (self) answer of Charles Staal.
Assuming ascii encoding following will work. Updated from the kind comment of Yves Daoust
int Get1BasedIndex(char ch) {
return ( ch | ('a' ^ 'A') ) - 'a' + 1;
}
This will make the character uppercase and change the index.
However a more readable solution (O(1)) is:
int Get1BasedIndex(char ch) {
return ('a' <= ch && ch <= 'z') ? ch - 'a' + 1 : ch - 'A' + 1;
}
One more solution that is constant time but requires some extra memory is:
static int cha[256];
static void init() {
int code = -1;
fill_n (&cha[0], &cha[256], code);
code = 1;
for(char s = 'a', l = 'A'; s <= 'z'; ++s, ++l) {
cha[s] = cha[l] = code++;
}
}
int Get1BasedIndex(char ch) {
return cha[ch];
}
We can get their ASCII values and then subtract from the starting character ASCII(a - 97, A - 65)
char ch = 'a';
if(ch >=65 && ch <= 90)//if capital letter
System.out.println((int)ch - 65);
else if(ch >=97 && ch <= 122)//if small letters
System.out.println((int)ch - 97);
Strictly speaking it is not possible to do it portably in C/C++ because there is no guarantee on the ordering of the characters.
This said, with a contiguous sequence, Char - 'a' and Char - 'A' obviously give you the position of a lowercase or uppercase letter, and you could write
Ord= 'a' <= Char && Char <= 'z' ? Char - 'a' :
('A' <= Char && Char <= 'Z' ? Char - 'A' : -1);
If you want to favor efficiency over safety, exploit the binary representation of ASCII codes and use the branchless
#define ToUpper(Char) (Char | 0x20)
Ord= ToUpper(Char) - 'a';
(the output for non-letter character is considered unspecified).
Contrary to the specs, these snippets return the position in range [0, 25], more natural with zero-based indexing languages.
The method Character.isLetter(Char c) tells whether the character is a unicode letter. What if I want to check for English letters (a-zA-Z) without regex.
Easy
char c = ...;
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
//english letter
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
This is a bit more trickier than I thought..
I am creating a java program that is encrypted:
the input is 'a' and shift the letter by 5 which goes to 'f'
which is simple in ASCII but when I get to the letter z i want the program to loop back around to 'a' and start over if that makes sense just i have no idea where to start!
The key is modulus division:
char c;
c = (char)((c - 'a' + 5) % 26 + 'a');
c - 'a' gives you the number of the letter 0-25, which is then shifted up by 5 and the remainder after dividing by 26 is then added to 'a' to give up back the character for the letter.
Use mod % operator.
char translated = (char) ('a' + (charOriginal -'a' + 5) % ('z' - 'a' + 1));
Here it is :
public char encrypt(char c)
{
return Character.isLowerCase(c) ? (char)((c - 'a' + 5) % 26 + 'a') : (char)((c - 'A' + 5) % 26 + 'A');
}
I've edited my post, now it checks lower/upper case.
If you don't and minus 'a' for uppercase, it won't work. For example, encrypt('Y') would have returned '^' instead of 'D'.
You could try something like this:
/**
* Shifts a letter 5 letters, if the char is a letter,
* other wise (if a number or symbol) just returns the char.
* Jumps back to 'a' or 'A' when it goes past 'z' or 'Z'.
*/
public char shift5(char letter) {
char letterToReturn = letter;
if(letterToReturn >= 'a' && letter <= 'z') {
// letter is lowercase
letterToReturn = shiftLetter(letterToReturn , 5);
} else if(letter >= 'A' && letter <= 'Z') {
// letter is uppercase
letterToReturn = shiftLetter(letterToReturn , 5);
}
return letterToReturn;
}
/**
* Shifts a letter to the next letter the specified amount of times.
* Jumps back to 'a' or 'A' when it goes past 'z' or 'Z'.
*/
public char shiftLetter(char letter, int amountToShift) {
char letterToReturn = letter;
for (int i = 1; i <= amountToShift; i++) {
letterToReturn ++;
if(letterToReturn == (char)((int)'z' + 1) {
// letter has gone past 'z', so change to 'a'
letterToReturn = 'a'
} else if(letterToReturn == (char)((int)'Z' + 1) {
// letter has gone past 'Z', so change to 'A'
letterToReturn = 'A'
}
}
return letterToReturn;
}
This code handles lowercase, uppercase, and characters that aren't letters.