Ascii Value Of A To Z In C++

4 min read Jul 03, 2024
Ascii Value Of A To Z In C++

ASCII Value of a to z in C++

The ASCII (American Standard Code for Information Interchange) table assigns numerical values to characters. In C++, you can access these values using the int data type. This article will guide you on how to find the ASCII values of lowercase letters 'a' to 'z' in C++.

Understanding ASCII Values

Every character, including letters, numbers, punctuation marks, and special symbols, has a unique ASCII value. Lowercase letters 'a' to 'z' have consecutive ASCII values starting from 97 ('a') and ending with 122 ('z').

Code Example

#include 

using namespace std;

int main() {
  // Iterate through lowercase letters 'a' to 'z'
  for (char ch = 'a'; ch <= 'z'; ch++) {
    // Print the character and its ASCII value
    cout << ch << ": " << int(ch) << endl;
  }
  return 0;
}

Explanation

  • #include <iostream>: This line includes the input/output stream library, which is essential for printing output to the console.
  • using namespace std;: This line lets you use elements from the std namespace directly without needing to prefix them with std::.
  • int main() { ... }: This is the main function of your C++ program where execution begins.
  • for (char ch = 'a'; ch <= 'z'; ch++) { ... }: This for loop iterates through all lowercase letters from 'a' to 'z'.
    • char ch = 'a';: Initializes the ch variable with the character 'a'.
    • ch <= 'z';: This condition checks if the current character is less than or equal to 'z'.
    • ch++: This statement increments the ch variable to the next character in the alphabet.
  • cout << ch << ": " << int(ch) << endl;: This line prints the character and its ASCII value to the console.
    • int(ch): This converts the character ch to its integer ASCII value.
    • endl: This inserts a new line after each output.

Output

The code will print the following output:

a: 97
b: 98
c: 99
...
x: 120
y: 121
z: 122

Conclusion

This example demonstrates how to find the ASCII values of lowercase letters 'a' to 'z' in C++. By using a simple loop and type casting, you can easily access and utilize ASCII values in your C++ programs.