ASCII
š§ What is ASCII?
- ASCII stands for American Standard Code for Information Interchange.
- It assigns numerical values (0ā127) to characters, including:
- Letters (
AāZ,aāz) - Digits (
0ā9) - Punctuation (
!,@, etc.) - Control characters (like newline
\n, tab\t)
- Letters (
š¤ Character Encoding in C++
- C++
chartypes use ASCII values by default (in most systems). - A
charis just a small integer, so:
char c = 'A';
int val = c; // val = 65`š” Important ASCII Facts for Strings/Chars in C++
| Concept | Explanation |
|---|---|
'A' to 'Z' | ASCII values from 65 to 90 |
'a' to 'z' | ASCII values from 97 to 122 |
'0' to '9' | ASCII values from 48 to 57 |
| Case conversion | 'a' - 'A' == 32, so you can flip case with +/- 32 |
| Character checks | Use isalpha(c), isdigit(c), islower(c), etc. from <cctype> |
| Sorting/comparison | You can compare chars directly: 'a' < 'b', 'A' < 'a' |
| Looping through alphabet | Use ASCII values: for (char c = 'a'; c <= 'z'; ++c) |
Stringify š¢
The most efficient way to convert a digit x ā {0, 9} into a character is by adding its integer value to the ASCII value of '0', like so:
string ans = "";
int x = 5;
ans += x + '0';`Think of x + '0' as:
āGive me the ASCII value of
'0', then addxto get the ASCII value of the digit character.ā
This is much faster than the more familiar alternative using to_string:
`string ans = ""; int x = 5; ans = to_string(x) + ans;`
ā
Use x + '0' when working with single digits in performance-sensitive code, especially in tight loops or when building strings character by character.
UsingĀ stringifyĀ (by adding a character to ā0ā) is faster thanĀ to_stringĀ in C++ because:
The Overhead ofĀ to_string: TheĀ to_stringĀ function is a more complex operationĀ that:
- Creates a new stringĀ object
- Handles memory allocation
- Performs the actual conversion from integer to string
- Handles various edge cases and formatting
Simple Character Addition: TheĀ stringifyĀ approach usingĀ remainder +Ā ā0āĀ is much simpler because:
- It directly converts a single digit (0-9) to its ASCII character equivalent
- Itās a single arithmetic operation
- No memory allocation is needed
- No string object creationĀ is required
String Comparison with <, >, == in C++
In modern C++, std::string supports direct comparison using relational operators, just like numbers:
std::string a = "apple";
std::string b = "banana";
if (a < b) std::cout << "apple comes before banana\n";
if (a != b) std::cout << "they are different\n";ā Supported Operators:
| Operator | Meaning | Example |
|---|---|---|
== | Equal | "abc" == "abc" ā true |
!= | Not equal | "abc" != "xyz" ā true |
< | Lexicographically less than | "apple" < "banana" |
> | Lexicographically greater than | "zebra" > "ant" |
<= | Less than or equal | "abc" <= "abc" |
>= | Greater than or equal | "cat" >= "ant" |
š Lexicographical Order:
- Comparison is done character by character using ASCII values.
- Stops at the first differing character.
- If one string is a prefix of the other, the shorter one is considered smaller:
"app" < "apple" // true