-
Notifications
You must be signed in to change notification settings - Fork 0
Strings and IO
Derek Snider edited this page May 19, 2026
·
1 revision
string s = "hello world";
cout << s << endl;string is backed by C++ std::string. Strings are mutable and grow automatically.
cout << "hello " << x << endl;
std::cout << "qualified" << std::endl;Chain multiple values with <<. Works with strings, integers, floats, and char *.
puts("hello"); // print string + newline
putchar('h'); // print single character
puti(42); // print integer
printf("x = %d\n", x); // formatted output (via #include <stdio.h>)string name;
int age;
cout << "Enter name: ";
cin >> name;
cout << "Enter age: ";
cin >> age;
cout << "Hello, " << name << ", age " << age << endl;cin >> var reads whitespace-delimited tokens from stdin. Works with string, int, and double.
Chained input:
string a;
string b;
cin >> a >> b;stringstream ss;
ss << "value: " << 42;
string result;
result = ss.str();
cout << result << endl; // value: 42ofstream out;
string fname = "output.txt";
out.open(fname);
out << "Hello!" << endl;
out.close();ifstream in;
string fname = "output.txt";
in.open(fname);
string line;
while (in.good()) {
getline(in, line);
if (in.good())
cout << line << endl;
}
in.close();ofstream out;
string fname = "output.txt";
out.open(fname);
defer out.close(); // auto-close at scope exit
out << "data" << endl;madc strings can be manipulated with functions from any namespace:
string s = " Hello, World! ";
php::trim(s); // "Hello, World!"
perl::lc(s); // "hello, world!"
python::title(s); // "Hello, World!"
ruby::squeeze(s); // collapse duplicates
rust::contains(s, "World"); // 1
string encoded;
js::btoa(encoded, s); // base64- Data Types — all types including containers
- Namespaces — 100+ string and array functions
- ← Back to Home