What is Char?
What is ASCII?
Char Declaration
Syntax:
Printing ASCII Value
Printing Char Value
Inputting Chars
Converting Character to String
What is ASCII?
The char value is interpreted as an ASCII character. This is similar to how Boolean values are interpreted as being true or false. ASCII is an acronym for American Standard Code for Information Interchange. It defines a specific way of representing English characters as numbers. The numbers range between 0 and 127. For example, the character ‘a’ is equivalent to ASCII code 97.
Char Declaration
To declare a char variable in C++, we use the char keyword. This should be followed by the name of the variable. The variable can be initialized at the time of the declaration. The value of the variable should be enclosed within single quotes.
Syntax:
Here is the syntax for char declaration in C++: The variable-name is the name to be assigned to the variable. If a value is to be assigned at the time of declaration, you can use this syntax:
The variable-name is the name of the char variable. The value is the value to be assigned to the char variable.
Example 1:
Output:
Here is a screenshot of the code:
Code Explanation:
Including the iostream header file in our code to use its functions. Including the std namespace in our code in order to use its classes without calling it. Calling the main() function. The program logic should be added within the body of this function. Declare a character variable named grade. The variable has also been assigned a value of B. Notice the value of the variable is enclosed within single quotes. Print the value of the variable grade alongside other text on the console. The program must return value upon successful completion. End of the body of the main() function.
Printing ASCII Value
As stated above, each character is interpreted as ASCII character. It’s possible for you to get the ASCII value of any character. You simply pass the character to the int() function. This process is called type casting. Let’s demonstrate this:
Example 2:
Output:
Here is a screenshot of the code:
Code Explanation:
Including the iostream header file into our code in order to use its functions. Including the std namespace into our code in order to use its classes without calling it. Calling the main() function. The program logic should be added within the body of this function. Declare a char variable named ch. Print some text on the console. The text asks the user to enter a value for the variable ch. Read the user input from the keyboard and store it in the variable ch. Print some text on the console. The text will include the character you entered for variable ch, the ASCII value of this character, and other text. The program must return value upon successful completion. End of the body of the main() function.
Printing Char Value
Given an ASCII value, the C++ compiler can return the corresponding character. You declare a char variable and assign it an integer value. It will be converted to the corresponding character value.
Example 3:
Output:
Here is a screenshot of the code:
Code Explanation:
Including the iostream header file into the code. We will then use its functions without getting errors. Include the std namespace into the code. We will use its classes without calling it. Calling the main() function. The program logic should go into the body of this function. Declare three char variables x, y, and z. The three have been assigned integer values of 65, 66, and 67. These will be treated as ASCII values for characters. Print out the value of variable x on the console. Since x was declared as a char, the char with ASCII value of 65 will be returned, that is, A. Print out the value of variable y on the console. Since y was declared as a char, the char with ASCII value of 66 will be returned, that is, B. Print out the value of variable z on the console. Since z was declared as a char, the char with ASCII value of 67 will be returned, that is, C. The program must return value upon successful completion. The end of the body of the main() function.
Inputting Chars
We can use the std::cin function to read a char entered by the user via the keyboard. The std::cin will allow you to enter many characters. However, the character variable can hold only one character. This means only the first character entered will be extracted and stored in the character variable. The rest will remain in the buffer used by std::cin. To extract it, make subsequent calls to the std::cin.
Example 4:
Output:
Here is a screenshot of the code:
Code Explanation:
Including the iostream header file in our code to use its functions. Include the std namespace in our code to use its classes without calling it. Calling the main() function. The program logic should be added within the body of this function. Print some text on the console. Declare a character variable named ch. Read user input from the keyboard. The input will be stored in the variable ch. Since a user will type a character sequence like abc, only the first character, a, will be stored in variable ch. Printing the first character entered, its ASCII code and other text on the console. The ASCII code is determined by passing the character variable to the int() function. Read the next character that was entered by the user. The user won’t be required to enter a new character. It will instead read the second character that was entered, that is, b. Printing the second character entered, its ASCII code and other text on the console. The ASCII code is determined by passing the character variable to the int() function. The program must return value upon successful completion. End of the body of the main() function.
Converting Character to String
There exist a number of ways that we can use to convert characters to strings. Let’s discuss them:
#1: Using Constructor given by a String Class
This can be done using the following syntax: The parameter n denotes the size of the string that is to be generated. The parameter x is the character to convert to a string. The function returns a string.
Example 5:
Output:
Here is a screenshot of the code:
Code Explanation:
Including the iostream header file in our code to use its functions. Include the string header file in our code to use its functions. Include the std namespace in our code to use its classes without calling it. Calling the main() function. The program logic should be added within the body of this function. Convert the character “C” into a 1-length string and assign the resulting string to the variable st. Print the value of the string st on the console alongside other text. The program must return value upon successful completion. End of the body of the main() function.
#2) Using the std::string Operators = and +=
The = and += operators are already overloaded with characters. The two can be used to convert a particular character to a string.
Example 6:
Output:
Here is a screenshot of the code:
Code Explanation:
Include the iostream header file in our code to use its functions. Include the string header file in our code to use its functions. Include the std namespace in our code to use its classes without calling it. Calling the main() function. The program logic should be added within the body of this function. Create a string variable named st. Create a character named b with a value of B. Assign a value of A to the string named st. Use the += operator to convert characters into a string. Print the value of the string st on the console alongside other text. The program must return value upon successful completion. End of the body of the main() function.
#3: Using std::string Methods
The std::string class comes with many overloaded functions that can help you convert characters into strings. They include:
push_backThis function assigns a particular character to a string’s end. It is overloaded for characters.It takes the following syntax: void push_back(char ch)
The parameter ch is the character that is to be changed to a string. appendIt assigns many copies of a particular character to a string.The function takes the following syntax: string& append(size_t n,char ch)
The parameter n denotes the times that the character will be appended.
The parameter ch is the character to append to string.
assign This function replaces the current contents of the string with n copies of the specified character.It takes the following syntax:
string& assign(size_t n,char ch);
The parameter n denotes the total copies for the character.
The parameter ch is the character to copy into the string.
insertThe insert function inserts n copies of a character at the starting position of the string, as specified in the arguments. It takes the following syntax:
string& insert(size_t p,size_t n,char ch);
The p parameter denotes the position from the start where characters will be inserted.
The parameter n denotes the total copies for the character.
The parameter ch is the character to be insert in the string.
Example 7:
Output:
The parameter ch is the character to append to string.
The parameter ch is the character to copy into the string.
The parameter n denotes the total copies for the character.
The parameter ch is the character to be insert in the string.
Here is a screenshot of the code:
Include the iostream header file in our code to use its functions. Include the string header file in our code to use its functions. Include the std namespace in our code to use its classes without calling it. Calling the main() function. The program logic should be added within the body of this function. Create a string variable named st. Assign the character A to the end of the string. Print the value of the string st on the console alongside other text. The endl (end line) moves the cursor to the next line. Set the value of the string st to empty. Assign single character C to the string named st. Print the value of the string st on the console alongside other text. The endl (end line) moves the cursor to the next line. Set the value of the string st to empty. Replace contents for string st with single character D. Print the value of the string st on the console alongside other text. The endl (end line) moves the cursor to the next line. Insert single character E to the string named st from its first index. Print the value of the string st on the console alongside other text. The endl (end line) moves the cursor to the next line. The program must return value upon successful completion. End of the body of the main() function.
#4: Using std::stringstream To use this class to convert character to a string, insert the character into stream. They’ll be written to the string.
Example 8:
Output:
Here is a screenshot of the code:
Code Explanation:
Include the iostream header file in our code to use its functions. Include the string header file in our code to use its functions. Include the sstream header file in our code to use its functions. Include the std namespace in our code to use its classes without calling it. Calling the main() function. The program logic should be added within the body of this function. Create a string variable named st. Create a stream variable named myst. Insert the character A into stream object named myst. Convert the stream object into a string. Print the value of the string st on the console alongside other text. The endl (end line) moves the cursor to the next line. The program must return value upon successful completion. End of the body of the main() function.
Summary:
A char is a C++ data type used for the storage of letters. C++ Char is an integral data type, meaning the value is stored as an integer. It occupies a memory size of 1 byte. C++ Char only stores single character. Char values are interpreted as ASCII characters. ASCII is an acronym for American Standard Code for Information Interchange. It states a specific way of representing English characters in the form of numbers. To see the ASCII value of a character, we pass it to the int() function. To see the corresponding char value of ASCII value, we define the ASCII as a character.