C++ Program to Find ASCII Value of a Character

Updated on December 3, 2024
Find ascii value of a character header image

Introduction

In C++, the ASCII value of a character can be found using a very straightforward method. ASCII, which stands for American Standard Code for Information Interchange, assigns a unique numerical value to each character and symbol to facilitate text processing and data storage. This is particularly useful in various programming situations, such as sorting, searching, or even encryption algorithms.

In this article, you will learn how to efficiently determine the ASCII value of characters in C++. By understanding and implementing this operation, you will enhance your problem-solving skills in handling string and character data.

Finding the ASCII Value of a Character

Example 1: Using a Single Character

  1. Declare a character variable.

  2. Prompt the user to enter a character.

  3. Display the ASCII value using type casting.

    c++
    #include <iostream>
    using namespace std;
    
    int main() {
        char c;
        cout << "Enter a character: ";
        cin >> c;
        cout << "ASCII Value of " << c << " is " << int(c) << endl;
        return 0;
    }
    

    In this example, the user inputs a character stored in variable c. The int() cast converts the character to its corresponding ASCII value, which is then displayed.

Example 2: ASCII Value in a String

  1. Initialize a string variable.

  2. Iterate over each character of the string.

  3. Output the ASCII value of each character.

    c++
    #include <iostream>
    using namespace std;
    
    int main() {
        string s;
        cout << "Enter a string: ";
        getline(cin, s);
        for (char c : s) {
            cout << "ASCII Value of " << c << " is " << int(c) << endl;
        }
        return 0;
    }
    

    This code handles a string input, processing each character within a loop to output its ASCII value. The getline(cin, s) function is used to take a whole line of text, allowing spaces to be included in the input string.

Example 3: Validating ASCII Range

  1. Examine characters to check if they are within the ASCII range.

  2. Report if any non-ASCII character is found.

    c++
    #include <iostream>
    using namespace std;
    
    int main() {
        char c;
        cout << "Enter a character: ";
        cin >> c;
        int ascii = int(c);
        if (ascii >= 0 && ascii <= 127) {
            cout << "ASCII Value of " << c << " is " << ascii << endl;
        } else {
            cout << "Character is not in the ASCII range." << endl;
        }
        return 0;
    }
    

    This snippet introduces a simple validation check. It ensures the character entered is within the valid ASCII range (0-127). This is important for accurately interpreting characters, especially in environments that may process extended ASCII or Unicode characters.

Conclusion

Retrieving the ASCII value of characters in C++ is a fundamental skill that serves many practical coding purposes. The examples provided demonstrate quick ways to convert characters to their ASCII numeric values, handle strings, and ensure data integrity by checking ASCII validity. Implement these methods in your C++ projects to effectively manage and manipulate character data, ultimately resulting in robust and reliable applications.