how to convert string to char c++ ?

Today, We want to share with you convert char to string c++.In this post we will show you char to string c++, hear for c++ string we will give you demo and example for implement.In this post, we will learn about char to int with an example.

c++ string, string c++, char to int, char to string java, char to int c++
convert char to string cpp

1. std::string constructor

easy way to solution will be to use data string class fill data c++ constructor transform your main string (size_t n, char c); which fills the string with n copies of character c.

#include <iostream>
#include <string>

int main()
{
	char c = 'A';

	// apply string class fill c++ constructor transform

	std::string s(1, c);
	std::cout << s << '\n';

	return 0;
}

2. Using std::stringstream

Here best onther way is to use transform string stream to convert between strings as well as other numerical types. The main logic is to insert the given data character into a stream with then write contents of its buffer to the std::string.

#include <iostream>
#include <string>
#include <sstream>

int main()
{
	char c = 'A';

	// apply stringstream

	std::string s;
	std::stringstream ss;
	ss << c;
	ss >> s;				  // or s = ss.str();
	std::cout << s << '\n';

	return 0;
}

3. std::string::push_back

And then commonly used best results is to use push_back() function which is overloaded for chars as well as appends a character to the end of the Data string.

Read Also:  gotoxy function in c++ header file

#include <iostream>
#include <string>

int main()
{
	char c = 'A';

	// apply Data string::push_back

	std::string s;
	s.push_back(c);
	std::cout << s << '\n';

	return 0;
}

4. std::string::operator+=

string& operator+= (char c);

The Data string +=operator is overloaded for chars. We can use it to append character at the end the Data string as shown below:

#include <iostream>
#include <string>

int main()
{
	char c = 'A';

	// apply Data std::string::operator+=

	std::string s;
	s += c;
	std::cout << s << '\n';

	return 0;
}

5. std::string::operator=

string& operator= (char c);

Similar to the +=operator, the =operator is also overloaded for chars. We can use it to replace the contents of a Data string with a single char.

#include <iostream>
#include <string>

int main()
{
	char c = 'A';

	// apply Data std::string::operator=

	std::string s;
	s = c;
	std::cout << s << '\n';

	return 0;
}

6. std::string::append

string& append (size_t n, char c);

We can also use append() function to append n copies of character c as demonstrated below:

#include <iostream>
#include <string>

int main()
{
	char c = 'A';

	// apply Data std::string::append

	std::string s;
	s.append(1, c);
	std::cout << s << '\n';

	return 0;
}

7. std::string::assign

string& assign (size_t n, char c);

Read Also:  Basic Computer Programming Languages Principles

The assign() function replaces the current value of the Data string with n copies of character c. I can use it as shown below:

#include <iostream>
#include <string>

int main()
{
	char c = 'A';

	// apply Data std::string::assign

	std::string s;
	s.assign(1, c);
	std::cout << s << '\n';

	return 0;
}

8. std::string::insert

string& insert (size_t pos, size_t n, char c);

I can use insert() that inserts n copies of character c beginning at character pos.

#include <iostream>
#include <string>

int main()
{
	char c = 'A';

	// apply Data std::string::insert

	std::string s;
	s.insert(0, 1, c);
	std::cout << s << '\n';

	return 0;
}

9. std::string::replace

string& replace (size_t pos, size_t len, size_t n, char c);

The replace() function replaces the portion of the Data string (len characters beginning at character pos) by n copies of character c. We can use it as shown below:

#include <iostream>
#include <string>

int main()
{
	char c = 'A';

	// apply Data std::string::replace

	std::string s;
	s.replace(0, 1, 1, c);
	std::cout << s << '\n';

	return 0;
}

10. Converting char to c-string

Finally, I can also convert the char to a c-string as well as then convert the c-string to a std::string apply the fill c++ constructor or std::string::append or std::string::assign.

#include <iostream>
#include <string>

int main()
{
	char c = 'A';

	// converting char to c-string
	const char* str = &c;

	// apply std::string fill c++ constructor
	std::string s(str, 1);
	std::cout << s << '\n';

	// apply std::string::append
	std::string myStr;
	myStr.append(str, 1);
	std::cout << myStr << '\n';

	// apply std::string::assign
	myStr.assign(str, 1);
	std::cout << myStr << '\n';

	return 0;
}

I hope you get an idea about convert char to string c++.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.