ProgramsC++

String Conversion Programs

C++ type conversion programs: char, int, double, string, and char arrays converted back and forth with casts, stoi, stod, to_string, and stringstream.

11 programs · open one, then the next

Start with Convert Char to String
  1. 01Convert Char to StringBeginnerTurn a char into a std::string in C++ three ways: the string(1, ch) constructor, assignment, and push_back. Shows which forms compile and why string(ch) fails.Open example →
  2. 02Convert Integer to CharBeginnerConvert an integer code like 65 into its character 'A' in C++ with a C-style cast or static_cast. Also covers turning digit 7 into '7' by adding '0'.Open example →
  3. 03Convert String to IntegerBeginnerConvert a numeric string like "12345" into an int in C++ with stoi or stringstream. Learn which one throws on bad input and how to check a failed stream read.Open example →
  4. 04Convert Integer to StringBeginnerConvert an int to a std::string in C++ with to_string or a stringstream. See when each is handy, how to pad with zeros, and why char(num) is not the same thing.Open example →
  5. 05Convert String to DoubleBeginnerConvert text like "123.456" to a double in C++ with stod or stringstream. Covers exceptions on bad text, locale-free parsing, and why a digit can look off.Open example →
  6. 06Convert Double to StringBeginnerConvert a double to a string in C++ with to_string and with a stringstream using fixed and setprecision. Explains why to_string(123.456) gives 123.456000.Open example →
  7. 07Convert String to CharBeginnerGet a single character out of a C++ string with [], at(), or c_str(). Learn which one checks the index, what at() throws, and why an empty string needs care.Open example →
  8. 08Convert Int to DoubleBeginnerConvert an int to a double in C++ by plain assignment, static_cast, or a C-style cast. Shows why 123 prints without .0 and how casting fixes integer division.Open example →
  9. 09Convert Double to IntBeginnerConvert a double to an int in C++: truncate with a cast, round down with floor, or round to nearest with round. Compares results for 123.789 and negatives.Open example →
  10. 10Convert String to ArrayIntermediateCopy a C++ string into a char array with strcpy, into a vector<char>, or with a manual loop. Learn why the array needs room for the '\0' terminator.Open example →
  11. 11Convert Array to StringBeginnerTurn a null-terminated char array into a std::string in C++ with assignment, the constructor, or assign(). Explains why the '\0' at the end is required here.Open example →