This C++ Program which prints the ASCII table from 0 to 127. The program uses a while loop to go through every ASCII character from 0 to 127. ASCII character from 0 to 31 are control characters and are printed specially using an array of character strings, the characters from 32 to 126 are printed by using the character representation of the integer value and the character representation of 127 is printed using the character string “DEL”.
Here is source code of the C++ program which prints the ASCII table from 0 to 127. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Print ASCII table (0 - 127)
*/
#include<iostream>
#include<iomanip>
using namespace std;
char const* character[] = {"", "", "", "", "", "", "", "",
"\\a","\\b","\\t","\\n","\\v","\\f","\\r", "",
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", ""};
int main()
{
char c;
int row;
cout << " ASCII Table" << endl << "=============" << endl;
for(int i = 0; i < 16; i++)
{
row = i;
while (row <= 127) {
if (row < 32)
cout << setfill('0') << setw(2) << setbase(16)
<< row << " = " << setw(3) << setfill(' ')
<< character[i] << " | ";
else if (row >= 32 && row < 127)
{
c = row;
cout << setfill('0') << setw(2) << setbase(16)
<< row << " = " << setw(3) << setfill(' ')
<< c << " | ";
}
else
cout << setfill('0') << setw(2) << setbase(16)
<< row << " = " << setw(3) << setfill(' ')
<< "DEL" << " | ";
row = row + 16;
}
cout << endl;
}
}
cout << "!" << endl;
}
$ g++ main.cpp $ ./a.out ASCII Table ============= 00 = | 10 = | 20 = | 30 = 0 | 40 = @ | 50 = P | 60 = ` | 70 = p | 01 = | 11 = | 21 = ! | 31 = 1 | 41 = A | 51 = Q | 61 = a | 71 = q | 02 = | 12 = | 22 = " | 32 = 2 | 42 = B | 52 = R | 62 = b | 72 = r | 03 = | 13 = | 23 = # | 33 = 3 | 43 = C | 53 = S | 63 = c | 73 = s | 04 = | 14 = | 24 = $ | 34 = 4 | 44 = D | 54 = T | 64 = d | 74 = t | 05 = | 15 = | 25 = % | 35 = 5 | 45 = E | 55 = U | 65 = e | 75 = u | 06 = | 16 = | 26 = & | 36 = 6 | 46 = F | 56 = V | 66 = f | 76 = v | 07 = | 17 = | 27 = ' | 37 = 7 | 47 = G | 57 = W | 67 = g | 77 = w | 08 = \a | 18 = \a | 28 = ( | 38 = 8 | 48 = H | 58 = X | 68 = h | 78 = x | 09 = \b | 19 = \b | 29 = ) | 39 = 9 | 49 = I | 59 = Y | 69 = i | 79 = y | 0a = \t | 1a = \t | 2a = * | 3a = : | 4a = J | 5a = Z | 6a = j | 7a = z | 0b = \n | 1b = \n | 2b = + | 3b = ; | 4b = K | 5b = [ | 6b = k | 7b = { | 0c = \v | 1c = \v | 2c = , | 3c = < | 4c = L | 5c = \ | 6c = l | 7c = | | 0d = \f | 1d = \f | 2d = - | 3d = = | 4d = M | 5d = ] | 6d = m | 7d = } | 0e = \r | 1e = \r | 2e = . | 3e = > | 4e = N | 5e = ^ | 6e = n | 7e = ~ | 0f = | 1f = | 2f = / | 3f = ? | 4f = O | 5f = _ | 6f = o | 7f = DEL |
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Check C++ Books
- Practice Programming MCQs
- Practice Computer Science MCQs
- Check Programming Books
- Check Computer Science Books