-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstrings.cc
More file actions
66 lines (54 loc) · 1.61 KB
/
Copy pathstrings.cc
File metadata and controls
66 lines (54 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Date: 2018-10-06
*
* Description:
* Basics operations with strings in C++.
*
* Compile:
* g++ -std=c++0x main.cc
*
* -std=c++0x is added to use standard C++11 features.
*
*/
#include "iostream"
using namespace std;
int main() {
// Print something
cout << "Hello World!" << endl;
// String can be combined using +
string str1 = "Happy";
string str2 = " Birthday!";
cout << str1 + str2 << endl; // Happy Birthday!
cout << str1[1] << endl; // 'a'
// Compare strings
cout << str1.compare(str2) << endl; // 1
cout << str2.compare(str1) << endl; // -1
cout << str1.compare(str1) << endl; // 0
// String assignment
string newString;
cout << newString.empty() << endl; // 1
newString.assign(str1);
cout << "newString is: " << newString << endl; // newString is: Happy
// To take input
string myString;
cout << "Enter a string: ";
getline(cin, myString); // 100
cout << "You entered string: " << myString << endl; // 100
// string to int, same as atoi(), stod() can be used to convert string to
// double
int num;
cout << "Enter a number: ";
getline(cin, myString); // 500
num = stoi(myString);
cout << "You entered number: " << num << endl; // 500
// To check string is empty or not
cout << myString.empty() << endl; // 0
// To find size of string
cout << myString.size() << endl; // 3, as 500 has 3 chars
// Other strings functions are:
// string.assign(string, startIndex, EndIndex)
// string.find(string, startIndex)
// string.insert(index, string)
// string.erase(startIndex, endIndex)
// string.replace(startIndex, endIndex, string)
}