C Program to Find ASCII value of a Character.

ASCII (American Standard Code for Information Interchange) is a character encoding standard used to represent text and control characters in computers and other devices that use text. Each character in ASCII is represented by a unique 7-bit binary code, allowing computers to understand and communicate text-based information. 


The ASCII standard defines codes for various characters, including alphabets (both uppercase and lowercase), numbers, special symbols, and control characters.


Algorithm to find the ASCII value of a Character.

Step 1: Take an input character ch from the user.

Step 2: The character ch is automatically converted to its corresponding ASCII value since characters are internally represented as their ASCII codes.

Step 3: The ASCII value of the input character is stored in the integer variable asciiValue.

Step 4: Display the ASCII value of the input character.


C Program to Print the ASCII value of the character.

//C Program to print the ASCII value of a character
#include <stdio.h>

int main() {
    char ch;

    // Input a character from the user
    printf("Enter a character: ");
    scanf("%c", &ch);

    // Convert the character to its corresponding ASCII value
    int asciiValue = ch;

    // Display the ASCII value of the input character
    printf("ASCII value of '%c' is %d.\n", ch, asciiValue);

    return 0;
}
Output:
Enter a character: a
ASCII value of 'a' is 97.

The above program displays the original character and its ASCII value using the printf function. The %c format specifier is used to display the character, and %d is used to display the ASCII value (an integer).

Time Complexity: O(1)
Space Complexity: O(1)

C Program to Print ASCII value of a String.

Below are the steps to print ASCII values of all the characters present in the String.

Steps to do so:

Step 1: Take an input string from the user.
Step 2: Traverse each character of the string one by one.
  • For each character, the program uses the ASCII encoding table to convert it to its corresponding ASCII value.
Step 3: Store the ASCII value of each character into an integer variable.
Step 4: Display the character along with its ASCII value. 

Code Implementation:
//C Program to print ASCII value of a string
#include <stdio.h>

int main() {
    char str[100];
    
    // Input a string from the user
    printf("Enter a string: ");
    scanf("%[^\n]", str);

    // Traverse each character of the string
    for (int i = 0; str[i] != '\0'; i++) {
        // Convert the character to its corresponding ASCII value
        int asciiValue = str[i];

        // Display the character along with its ASCII value
        printf("Character: %c, ASCII value: %d\n", str[i], asciiValue);
    }

    return 0;
}
Output:
Enter a string: AlgoLesson
Character: A, ASCII value: 65
Character: l, ASCII value: 108
Character: g, ASCII value: 103
Character: o, ASCII value: 111
Character: L, ASCII value: 76
Character: e, ASCII value: 101
Character: s, ASCII value: 115
Character: s, ASCII value: 115
Character: o, ASCII value: 111
Character: n, ASCII value: 110

The above program uses a "for" loop to traverse each character of the string "str." The loop runs until it encounters the null character '\0' (end of the string), which marks the end of the string.

For each character, the program uses the ASCII encoding table to convert it to its corresponding ASCII value. The ASCII value of the character is stored in the integer variable "asciiValue."

Time Complexity: O(n) where is the size of the string
Space Complexity: O(1), no extra space is required.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS