all.
I think I can only understand one way to make count[128] mark the string's characters' occurences:
if s string characters are all lowercase, then it comes to :
for(char c: s.toCharArray()) count[c-'a']++;
If they are all uppercase,
for(char c: S.toCharArray()) count[c-'A']++;
But I can't make sense how does this way to mark the character directly into int indexes?
for(char c: s.toCharArray()) count[c]++;
From my understanding, is it count['A'] making sense?
The int index represents that character's position in the ANSI character table.
Each character actually a number and each number represents a symbol. Have a look at this table:
Here : Character index [97-122] represents [A-Z] and [65-90] represents [a-z], also [48-57] represents ['0'-'9']. such as
'A' = 65
'B' = 66
'C' = 67
...
...
'Z' = 90
So count['a'] actually count[97], count['A'] actually count[65].
Related
I have a capital letter defined in a variable string, and I want to output the next and previous letters in the alphabet. For example, if the variable was equal to 'C', I would want to output 'B' and 'D'.
One way:
String value = "C";
int charValue = value.charAt(0);
String next = String.valueOf( (char) (charValue + 1));
System.out.println(next);
Well if you mean the 'ABC' then they split into two sequences a-z and A-Z, the simplest way I think would be to use a char variable and to increment the index by one.
char letter='c';
letter++; // (letter=='d')
same goes for decrement:
char letter='c';
letter--; // (letter=='b')
thing is that the representation of the letters a-z are 97-122 and A-Z are 65-90, so if the case of the letter is important you need to pay attention to it.
If you are limited to the latin alphabet, you can use the fact that the characters in the ASCII table are ordered alphabetically, so:
System.out.println((char) ('C' + 1));
System.out.println((char) ('C' - 1));
outputs D and B.
What you do is add a char and an int, thus effectively adding the int to the ascii code of the char. When you cast back to char, the ascii code is converted to a character.
All the answers are correct but none seem to give a full explanation so I'll try. Just like any other type, a char is stored as a number (16-bit in Java). Unlike other non-numeric types, the mapping of the values of the stored numbers to the values of the chars they represent are well known. This mapping is called the ASCII Table. The Java compiler treats chars as a 16-bit number and therefore you can do the following:
System.out.print((int)'A'); // prints 65
System.out.print((char)65); // prints A
For this reason, the ++, -- and other mathematical operations apply to chars and provide a way to increment\decrement their values.
Note that the casting is cyclic when you exceed 16-bit:
System.out.print((char)65601); // also prints A
System.out.print((char)-65471); // also prints A
P.S. This also applies to Kotlin:
println('A'.toInt()) // prints 65
println(65.toChar()) // prints A
println(65601.toChar()) // prints A
println((-65471).toChar()) // prints A
just like this :
System.out.printf("%c\n",letter);
letter++;
I have seen various comparisons that you can do with the charAt() method.
However, I can't really understand a few of them.
String str = "asdf";
str.charAt(0) == '-'; // What does it mean when it's equal to '-'?
char c = '3';
if (c < '9') // How are char variables compared with the `<` operator?
Any help would be appreciated.
// What does it mean when it's equal to '-'?
Every letter and symbol is a character. You can look at the first character of a String and check for a match.
In this case you get the first character and see if it's the minus character. This minus sign is (char) 45 see below
// How are char variables compared with the < operator?
In Java, all characters are actually 16-bit unsigned numbers. Each character has a number based on it unicode. e.g. '9' is character (char) 57 This comparison is true for any character less than the code for 9 e.g. space.
The first character of your string is 'a' which is (char) 97 so (char) 97 < (char) 57 is false.
String str = "asdf";
String output = " ";
if(str.charAt(0) == '-'){
// What does it mean when it's equal to '-'?
output= "- exists in the first index of the String";
}
else {
output="- doesn't exists in the first index of the String";
}
System.out.println(output);
It checks if that char exists in index 0, it is a comparison.
As for if (c < '9'), the ascii values of c and 9 are compared. I don't know why you would check if ascii equivalent of c is smaller than ascii equivalent of '9' though.
If you want to get ascii value of any char, then you can:
char character = 'c';
int ascii = character;
System.out.println(ascii);
str.charAt(0) == '-'; returns a boolean , in this case false.
if (c < '9') compares ascii value of '3' with ascii value of '9' and return boolean again.
str.charAt(0) == '-'
This statement returns a true if the character at point 0 is '-' and false otherwise.
if (c < '9')
This compares the ascii value of c with the ascii value of '9' in this case 99 and 57 respectively.
Characters are a primitive type in Java, which means it is not a complex object. As a consequence, every time you're making a comparison between chars, you are directly comparing their values.
Java characters are defined according to the original unicode specification, which gives each character a 16-bit value. These are the values that Java is comparing when you are comparing something like c>'3' or str.charAt(0) == '-'.
I have a quick question relating to ASCII and encoding. I am looking to take the input from a user - for example: "cat" - and turn it into a code. The code is as follows:
All lower case letters are converted to capital letters.
The first letter in the encoded message is stored as it's ASCII code value.
All subsequent letters are represented as the offset between the current letter and the previous letter.
For example: "cat" = 67 -2 19 since "C" = 67, "A" is -2 letters away from "C", and "T" is 19 letters away from "A". Any help would be greatly appreciated!
Scanner input = new Scanner(System.in);
String s = input.next().toUpperCase();
int ascii = s.charAt(0);
System.out.println(ascii);
for (int i = 1; i < s.length(); i++) {
ascii = s.charAt(i - 1);
System.out.println(s.charAt(i) - ascii);
}
To convert to capital letters there is the toUpperCase() method which returns the string with all capital letters.
To get the ASCII code of a letter you can simply create an integer and assign it the desired character (in this case the first letter of the string, so int ascii = s.charAt(0);).
To get the offsets you can use a for-loop starting from 1 (the second letter) where you get the ASCII code of the previous character and subtract it from the current one.
I want to know why char ch =5; (for example)
is not error ? but if I print
System.out.println(Character.isDigit(ch));
// output
false
it will be false ?
Because 5 is an integer literal that can be converted to a char. It is not the character '5' however.
A character is represented by two bytes in memory. Java converts 5 to a character.
'5' is not the 6th character (its hexadecimal code is 35 and not 5) in the ASCII table and is thus not a "digit".
try this example :
char ch = 97;
JOptionPane.showMessageDialog(null,"ch = "+ch);
The answer would be : ch = a
It simply won't give an error even though 97 is without (' ') because 97 represent the ASCII code for the character 'a' so it's not a digit , and that's why you are getting false as a result.
if you give ch = 5, it's automatically covert to char based on ASCII value.
In Currency.java file there is a line.
private static final int A_TO_Z = ('Z' - 'A') + 1;
What means is this? I didn't see this before. What is A_TO_Z's value and why it using 'Z' instead number.
With this expression you are treating chars as ints, using character's Unicode value instead of the character itself.
'Z' - 'A' + 1
Will become
90 - 65 + 1 (=26)
'Z' is a char with an integral value of 90.
'A' is a char with an integral value of 65.
90 - 65 + 1 = 26
Nasty. 'A' is the char literal for ASCII value of A (65 in decimal). 'Z' is 90. So A_TO_Z is 26, the number of letters in the English alphabet.
Characters have numeric values according to their value in the character table. That expression exploits the fact that all the letters form A to Z have consecutive values in the underlying encoding table thus subtracting the first value from the last ( + 1) gives the length of the English alphabet. The actual numerical values are unimportant in this case and the code is more or less self-explainable to the reader. In case the used encoding spreads the letters differently, the expression will become incorrect.