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; }
Enter a character: a
ASCII value of 'a' is 97.
C Program to Print ASCII value of a String.
- For each character, the program uses the ASCII encoding table to convert it to its corresponding ASCII value.
//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; }
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
No comments:
Post a Comment