R Programming
17 Best R Programming Books (2021 Update)
R is a programming language developed by Ross Ihaka and Robert Gentleman in 1993. The language...
Char is a C++ data type designed for the storage of letters. Char is an acronym for a character. It is an integral data type, meaning the value is stored as an integer. A char takes a memory size of 1 byte. It also stores a single character.
In this C++ tutorial, you will learn:
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.
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.
Here is the syntax for char declaration in C++:
char variable-name;
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:
char variable-name = 'value';
#include <iostream>
using namespace std;
int main() {
char grade = 'B';
cout << "I scored a: "<<grade;
return 0;
}
Output:
Here is a screenshot of the code:
Code Explanation:
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:
#include <iostream>
using namespace std;
int main() {
char ch;
cout << "Enter any character: ";
cin >> ch;
cout << "The ASCII Value of " << ch << " is " << int(ch);
return 0;
}
Output:
Here is a screenshot of the code:
Code Explanation:
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.
#include <iostream>
using namespace std;
int main() {
char x = 64, y = 66, z = 71;
cout << x;
cout << y;
cout << z;
return 0;
}
Output:
Here is a screenshot of the code:
Code Explanation:
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.
#include <iostream>
using namespace std;
int main() {
cout << "Type a sequence of characters: ";
char ch;
cin >> ch;
cout <<"The ASCII code of "<< ch << " is "<< int(ch) << '\n';
cin >> ch;
cout <<"The ASCII code of " << ch << " is "<< int(ch) << '\n';
return 0;
}
Output:
Here is a screenshot of the code:
Code Explanation:
There exist a number of ways that we can use to convert characters to strings.
Let's discuss them:
This can be done using the following syntax:
string st(int n,char x);
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.
#include<iostream>
#include <string>
using namespace std;
int main() {
string st(1, 'C');
cout << "The resulting string is : " << st;
return 0;
}
Output:
Here is a screenshot of the code:
Code Explanation:
The = and += operators are already overloaded with characters. The two can be used to convert a particular character to a string.
#include<iostream>
#include <string>
using namespace std;
int main() {
string st;
char b = 'B';
st = 'A';
st += b;
cout << "The resulting string is : " << st;
return 0;
}
Output:
Here is a screenshot of the code:
Code Explanation:
The std::string class comes with many overloaded functions that can help you convert characters into strings.
They include:
This 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.
It 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.
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.
The 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.
#include<iostream>
#include <string>
using namespace std;
int main() {
string st;
st.push_back('A');
cout << "push_back A returns : " << st << endl;
st = "";
st.append(1, 'C');
cout << "append C returns : " << st << endl;
st = "";
st.assign(1, 'D');
cout << "assign D returns : " << st << endl;
st.insert(0, 1, 'E');
cout << "insert single character returns : " << st << endl;
return 0;
}
Output:
Here is a screenshot of the code:
#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.
#include<iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string st;
stringstream myst;
myst << 'A';
myst >> st;
cout << "The conversion of the single character returns the string: " << st;
return 0;
}
Output:
Here is a screenshot of the code:
Code Explanation:
R is a programming language developed by Ross Ihaka and Robert Gentleman in 1993. The language...
What is Apache Solr? Apache Solr is an open-source search server platform written in Java language...
SolarMovie is a website that allows you to watch movies online, free without any payment. The...
We have organized the most frequently asked Linux Interview Questions and Answers that help...
Audio Recording Software are programs designed to record any sound. These applications allow...
Download PDF 1) Mention what is Jenkins? Jenkins is an open source tool with plugin built for...