ASCII Code is magic to do String Operations.ย

#### What is ASCII?

๐กASCII is a common data encoding type used for computer-to-computer electronic communication.
ASCII assigns standard numeric values to letters, numerals, punctuation marks, and other characters used in computers.

#### ๐กWhat is the purpose of ASCII codes?

The computer doesn’t understand English, Hindi, or any language. But computers do understand only 0 and 1. 0 means False & 1 means true.
So every char have an ASCII code, that further covert to a binary number (0, 1 format) and that the Computer use for processing.

#### ๐กWhat happens When we press a number key?

If you press 2 from the keyboard, then the keyboard sends the value 10 (binary format of 2) to the main memory which will further be evaluated in secondary memory, and then the output will be 2 in the monitor.

#### ๐กWhat happens when we press a Char key?

When we press “a”, the ASCII value will be 097 which is equivalent to 01100001 which is processed by Computer memory.

๐ NOTE- Numbers don’t have ASCII value, only Characters have ASCII value. And ASCII Code is of Single character only.

``````Example -
'a' have ASCII Code but 'ab' does not have ascii code.
'1' have ASCII Code but 1 (number) does not have.
All symbols have ASCII Code.``````

‘1’ ย – this is string & 1 – this is number.

#### ๐กHow to find ASCII Code in JavaScript?

We use the Inbuilt String method charCodeAt to get the Char code of any character of a string.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt

#### ๐ป ASCII Code of English Capital Letters-

```function asciiValuesOfCapsChar() {
console.log('ascii Values Of Capital Letters:');
let alphabets = 'ABCDEFGHIJKLMNOPQUERSTUVWXYZ';
for (let i = 0; i < alphabets.length; i++) {
console.log(alphabets[i], alphabets.charCodeAt(i));
}
}
```
``A 65, B 66, C 67, D 68, E 69, F 70, G 71, H 72, I 73, J 74, K 75, L 76, M 77, N 78, O 79, P 80, Q 81, U 85, E 69, R 82, S 83, T 84, U 85, V 86, W 87, X 88, Y 89, Z 90``

#### ๐ป ASCII Code of English Small Letters-

```function asciiValuesOfSmallChar() {
console.log('ascii Values Of Small Letters:');
let alphabets = 'abcdefghijklmnopquerstuvwxyz';
for (let i = 0; i < alphabets.length; i++) {
console.log(alphabets[i], alphabets.charCodeAt(i));
}
}
asciiValuesOfSmallChar();
```
``a 97, b 98, c 99, d 100, e 101, f 102, g 103, h 104, i 105, j 106, k 107, l 108, m 109, n 110, o 111, p 112, q 113, u 117, e 101, r 114, s 115, t 116, u 117, v 118, w 119, x 120, y 121, z 122``

#### โHow to find the ASCII Code of a given Char?

```function findAsciiOfChar() {
console.log('findAsciiOfChar :');
console.log('z', 'z'.charCodeAt())
console.log("a", 'a'.charCodeAt())
console.log('1', '1'.charCodeAt())
}
findAsciiOfChar();
```

#### ๐ป ASCII Code of Numbers In String Format. (‘0’, ‘1’, ‘2’…..’9′)

```function asciiCodeOfNumberChars() {
console.log('asciiCodeOfNumberChars :');
for (let i = 0; i < 10; i++) {
console.log(i, i.toString().charCodeAt())

}
}
asciiCodeOfNumberChars();
```
`` '0' 48,  '1' 49,  '2' 50,  '3' 51,  '4' 52,  '5' 53,  '6' 54,  '7' 55,  '8' 56,  '9' 57``

ASCII Code is magic to do String Operations.ย

๐กObservation between Capital Letters and Small Letters ASCII Codes-

```A = 65  a = 97   Difference = 32 (97-65)
B = 66  b = 98   ...
C = 67  c = 99   ..
...
..
Z = 90  z = 122  Difference = 32 (122-90)
```

๐ The difference from ‘A’ to ‘a’ is 32. This means every Capital Letter and Small Letter has a difference of 32.

#### ๐กBinary representation of a Character

```char ch = '9';
ASCII Code of '9' = 57

Char takes 1 Byte means 8 bits. So binary representaiton of '9' is 00111001
```

Lets Solve a Toggle String problem

Toggle every char of string, Cap to small and small to caps Without using any lowercase or uppercase method.

๐ Approach1 – Using ASCII Range of Capital Letters and Small Letters

As per the discussion, we know that Capital letters have an ASCII range of 65 to 90. And the difference between Capital letters and the small letter has 32.

```function toggleStringUsingAsciiRange(str) {
let ans = '';
for (let i = 0; i < str.length; i++) {
let code = str[i].charCodeAt();

// If char in range of Captial Letter's ascii values.
if (code >= 65 && code <= 90) { // A = 65 & Z = 90
code += 32; // convert to small
} else {
code -= 32; // convert to capital
}
ans += String.fromCharCode(code)
}
return ans;
}
```
``````toggleStringUsingAsciiRange('JSMount');

output > jsmOUNT``````

๐ Approach 2 – Solve the same problem without using Range.

```8 bit representation of Character-

32                          32
A = 65 = 0 1 0 0 0 0 0 1    a = 97 = 0 1 1 0 0 0 0 1
B = 66 = 0 1 0 0 0 0 1 0    b = 98 = 0 1 1 0 0 0 1 0
C = 67 = 0 1 0 0 0 0 1 1    c = 99 = 0 1 1 0 0 0 1 1
...
..
Z = 90 = 0 1 0 1 1 0 1 0   z = 122 = 0 1 1 1 1 0 1 0
```

๐ก Observations-

• All bits of Capital Letters and Small Letters have the same bit at their respective position instead of the 2^5 (32) position.
•  In the Capital letter, 2^5 position has 0. In the Small letter, 2^5 position has 1.
• So If we have to convert Capital to Small or vice-versa then we have to toggle 2^5 bit.
• Capital to Small > 0 to 1. Small to Capital > 1 to 0

By Using XOR Binary operation, we can easily obtain the toggle value.

Let’s do XOR with 1-

```1 ^ 1 = 0       0 ^ 1 = 0
11 ^ 11 = 00    00 ^ 11 = 00
```

And we have to toggle 2^5 bit position so we will do XOR with 10000

```function toggleStringUsingXOR(str) {
let ans = '';
for (let i = 0; i < str.length; i++) {
let code = str[i].charCodeAt();
ans += String.fromCharCode(code ^ 32); // Or we can write same: code ^ (1 << 5)
}
console.log(ans);

}
toggleStringUsingXOR('ABCDabcd')
```

#### โ Return uppercase array

```function toUpper(A) {
for (let i = 0; i < A.length; i++) {
let code = A[i].charCodeAt();
if (code >= 97 && code <= 122) { // char code of a = 97  and z = 122.
code -= 32; // capital letters have -32 char code from upper case letter.
}
A[i] = String.fromCharCode(code);
}
return A;
}

```
``toUpper(['Z', '*', 'z', ':', 'a', 'A']); // ['Z', '*', 'Z', ':', 'A', 'A']``

ASCII Code is magic to solve string problems

Array Representation of Binary Tree | Full Tree & Complete Binary Tree

Graphs in Data Structure, Types & Traversal with BFS and DFS, Algorithms

Traversing 2 D array with BFS & DFS Algorithm in JavaScript

HashTable Data Structure in JavaScript with Add Delete & Search Algorithms

Trie Data Structure โ Insert Search Delete & Print Program with JavaScript

JavaScript String Permutation Program with nice explanation

Fibonacci series in JavaScript | Four different ways

Backspace String Compare Leetcode Problem & Solution

Trapping Rain Water Leetcode Problem Solution

Representation of Negative Binary Numbers & 2โs Complement

Find Factors of a Number โ JavaScript Program

LeetCode โ Game of Life Problem | Solution with JavaScript