0% found this document useful (0 votes)
2 views

ASCII

The document provides C++ code examples that demonstrate the use of ASCII values, including printing ASCII values of characters, converting between uppercase and lowercase, creating patterns with ASCII characters, checking character types, and converting ASCII values to characters in a specified range. Each example includes code snippets and explanations of their functionality. The document serves as a reference for a CS100 midterm exam.

Uploaded by

rayyan.cems
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

ASCII

The document provides C++ code examples that demonstrate the use of ASCII values, including printing ASCII values of characters, converting between uppercase and lowercase, creating patterns with ASCII characters, checking character types, and converting ASCII values to characters in a specified range. Each example includes code snippets and explanations of their functionality. The document serves as a reference for a CS100 midterm exam.

Uploaded by

rayyan.cems
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Here are some C++ code examples that use ASCII values.

You can take


these as references for your CS100 midterm:

1. Printing ASCII values of characters:

#include <iostream>

using namespace std;

int main() {

char ch;

cout << "Enter a character: ";

cin >> ch;

// Print the ASCII value of the character

cout << "The ASCII value of '" << ch << "' is " << int(ch) << endl;

return 0;

This code takes a character input and prints its ASCII value using int(ch).

2. Converting a character to uppercase and lowercase using ASCII


values:

#include <iostream>

using namespace std;

int main() {

char ch;

cout << "Enter a character: ";

cin >> ch;

if (ch >= 'a' && ch <= 'z') {

// Convert to uppercase

char upperCh = ch - 32; // ASCII difference between lowercase and


uppercase
cout << "Uppercase: " << upperCh << endl;

else if (ch >= 'A' && ch <= 'Z') {

// Convert to lowercase

char lowerCh = ch + 32; // ASCII difference between uppercase and


lowercase

cout << "Lowercase: " << lowerCh << endl;

} else {

cout << "Not an alphabet character!" << endl;

return 0;

This example demonstrates converting between uppercase and lowercase


characters using ASCII values. It uses the fact that there is a fixed
difference between their ASCII values.

3. Printing a pattern with ASCII values (e.g., printing a rectangle


of characters):

#include <iostream>

using namespace std;

int main() {

int rows, cols;

cout << "Enter the number of rows and columns: ";

cin >> rows >> cols;

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

// Printing ASCII characters from 'A' to 'Z' in a pattern

cout << char('A' + (i + j) % 26) << " ";


}

cout << endl;

return 0;

This code creates a pattern of characters from the ASCII range 'A' to 'Z'
and wraps around using modulo arithmetic.

4. Checking if a character is a digit, letter, or special character:

#include <iostream>

using namespace std;

int main() {

char ch;

cout << "Enter a character: ";

cin >> ch;

if (ch >= '0' && ch <= '9') {

cout << "It is a digit." << endl;

else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {

cout << "It is a letter." << endl;

else {

cout << "It is a special character." << endl;

return 0;

}
This code uses ASCII values to check if the character entered is a digit,
letter, or special character.

5. Converting ASCII values to characters in a range:

#include <iostream>

using namespace std;

int main() {

for (int i = 65; i <= 90; i++) { // ASCII values from 'A' to 'Z'

cout << char(i) << " ";

cout << endl;

for (int i = 97; i <= 122; i++) { // ASCII values from 'a' to 'z'

cout << char(i) << " ";

cout << endl;

return 0;

This example prints all uppercase letters (A to Z) and lowercase letters (a


to z) by converting their ASCII values to characters.

Good luck with your exam! Let me know if you need anything else.

You might also like