Answers for "how to use string in c++"

C++
4

how can make string value in cpp

#include <string>
string hello= "hello you thre :)";
Posted by: Guest on June-07-2020
2

string in c++ stl

// let's I want to make a string of same char with specific length
// I can use string something like this
string(length,charecter);
//length - is the length of string we want;
//charecter - the charecter that we want;
// example if string(5,'a') --> 'aaaaa'
//if string(3,'g') --> 'ggg'
Posted by: Guest on November-01-2021
2

c++ string

#include <string>
#include <iostream>
#include <type_traits>
#include <cstring>

int main() {
  std::string str = "Hello, there";
  std::cout << std::boolalpha
  << str.capacity() << ", " << str.size() << ", " << std::strlen(str.data()) // 12, 12, 12
  << 'n' << std::is_same_v<std::string, std::basic_string<char>> // true
  << 'n' << str.front() + str.substr(1, 10) + str.back() // Hello there
  << 'n' << str[0] // H
  << 'n';
  
  str += "!"; 
  std::cout << str << 'n'; // Hello, there!
  str.erase(4, 4); // Hellhere!
  str.pop_back(); // Hellhere
  str.insert(4, " "); // Hell here
  std::cout << str << 'n'; // Hell here
  
}
Posted by: Guest on February-28-2021
3

string in cpp

// you want to include <string>
#include <string>
#include <iostream>

int main() 
{
  string helloWorld = "Hello World!"; // creating string and assigning
  std::cout << helloWorld;            // will output what you assigned it!
                                      /* you can also use strings with user 
                                      input (cin/getline)*/
  string namePerson{};
  getline(cin, namePerson); // getline allows for multi word input
  std::cout << namePerson;  // outputs name which person inputted
}
Posted by: Guest on October-20-2020
1

string c++

#include <string>
std::begin		| returns an iterator to the beginning of a container 
std::end		| returns an iterator to the end of a container 
std::size		| returns the length of string
std::to_string	| converts a number to string
std::stoi		| converts a string to a signed integer
std::getline 	| read data from an I/O stream into a string
std::swap  		| specializes the std::swap algorithm
std::empty 		| checks whether the container is empty
Posted by: Guest on June-05-2021
1

c++ string manipulation

/*
OUTPUT
String value: Programming    {where applicable: -31.05}
-----------------------------------------------------------------------------
|Programming|            << [No manipulation]
|         Programming|   << [Width:20][align:string_default=right]
|         Programming|   << [Width:20][align:right]
|Programming         |   << [Width:20][align:left]
|-31.05              |   << [Width:20][align:int_default=left]
|-              31.05|   << [Width:20][align:internal]
|.........Programming|   << [Width:20][align:default][fill:.]
|+++++++++Programming|   << [Width:20][align:default][fill:+]
|=========Programming|   << [Width:20][align:default][fill:=]
-----------------------------------------------------------------------------
*/

string sString = "Programming"; // Length = 11 
//NOTE: always place the settings before the actual string in cout

// width
cout << "|" << sString << "|" << "tt << [No manipulation]" << endl;
cout << "|" << setw(20) << sString << "|" << "t << [Width:20][align:string_default=right]n";

// alignment
cout << "|" << setw(20) << right << sString << "|" << "t << [Width:20][align:right]n";
cout << "|" << setw(20) << left << sString << "|" << "t << [Width:20][align:left]n";
cout << "|" << setw(20) << -31.05 << "|" << "t << [Width:20][align:int_default=left]n";
cout << "|" << setw(20) << internal << -31.05 << "|" << "t << [Width:20][align:internal]n";

// fill (HAVE to use single quotes in the setfill argument definition)
cout << "|" << setw(20) << setfill('.') << sString << "|" << "t << [Width:20][align:default][fill:.]n";
cout << "|" << setw(20) << setfill('+') << sString << "|" << "t << [Width:20][align:default][fill:+]n";
cout << "|" << setw(20) << setfill('=') << sString << "|" << "t << [Width:20][align:default][fill:=]n";
Posted by: Guest on June-02-2020

Browse Popular Code Answers by Language