Ascii Code Dev C++

  • The “Chapter 5 – #2: Characters for the ASCII Codes – Tony Gaddis – Starting Out With C” programming challenge comes from Tony Gaddis’ book, “Starting Out With C.” Problem Write a program that uses a loop to display the characters for the ASCII codes 0 through 127.
  • The following chart contains all 128 ASCII decimal (dec), octal (oct), hexadecimal (hex) and character (ch) codes.

Char ch = 'A'; you're setting the value of ch to whatever number your compiler uses to represent the character 'A'. That's usually the ASCII code for 'A' these days, but that's not required. You're almost certainly using a system that uses ASCII.

In cases, when the whole string needs to be converted to upper case or lower case, we can use these methods for the conversion:

  1. Changing the ASCII code of all characters.
  2. Using functions: toupper and tolower

Another interesting way would be to convert uppercase letters to lower case letters and lower one to upper. It is also given below, as an extra segment.

Method 1: Changing the ASCII code of all characters

Ascii Code Dev C Code

Logic:

The difference between the first character of uppercase and ASCII code of lower case character is 32, We add 32 to upper case letters in order to convert them to lower case, and we subtract 32 from lower case letters to convert them to upper case.

Algorithm to convert uppercase to lowercase:

  1. Check if the character is between A and Z i.e. it is a capital letter,
  2. If the character is a capital, we add 32 to it.
  3. Else, the character is already in lower case. Do nothing.

Algorithm for lowercase to uppercase:

  1. Check if the character is between ‘a’ and ‘z’ i.e. it is a lower case letter.
  2. If the character is a lower case letter, we subtract 32 from it.
  3. Else, the character is already in upper case. Do nothing.

Code:

Output:

Method 2 : Using toupper() and tolower()

Logic:

Ascii Code Dev C++

The predefined method toupper() and tolower() takes an integer as input.

It returns the same character converted, according to the upper or lower method used.

This method works in the similar manner to the first method. Difference is that, it is already defined in the library, so, the user does not need to write the same code each time he/she wants to convert the string.

Algorithm :

  1. Input the string, using the getline() method.
  2. We run a for loop, take each character of given string one by one.
  3. Next, we pass the character in toupper() or tolower() and store it in the same place of the string.
  4. Output the converted string.

Code:

Output:

Extra Segment: Upper to lower, and lower to upper(toggle case):

Logic:

Ascii codes for symbolsAscii Code Dev C++

We use the logic of method 1, here.

Algorithm:

  1. Input string
  2. Check if it is a capital letter
  3. If yes, convert it to small
  4. If no(it is a small letter), convert it to capital.

Code:

Ascii Code Dev C Free

Output:

Report Error/ Suggestion

Ascii Code For C++

Related Posts: