You can perform arithmetic with it.
int x {42};
x *= 2; // ok
std::string s {"Hello"};
s *= 2; // error: std::string::operator*= (int) is undefined
C++ already provides a string class in the C++ standard template library. #include<iostream> #include<string> int main() { using namespace std; string s {"Hello world!"}; cout << s << endl; }
int i = 0; while(str[i] != NULL){ i++; }
#include<iostream> #include<vector> #include<string> int main() { std::vector<std::string> names; for (int loop=0; loop!=10;) { std::cout << ++loop << " enter a name: "; std::string name; std::cin >> name; names.push_back (name); } }
There is no need to overload the plus operator to achieve this. It is already overloaded with this functionality. #include<iostream> #include<string> int main() { int x=40; int y=2; int z=x+y; std::cout<<x<<" + "<<y<<" = "<<z<<std::endl; std::string s1="Hello "; std::string s2="world!"; std::string s3=s1+s2; std::cout<<"""<<s1.c_str()<<"" + ""<<s2.c_str()<<"" = ""<<s3.c_str()<<"""<<std::endl; return(0); }
#include<iostream> #include<string> #include<algorithm> bool compare_no_case (const char a, const char b) { return tolower(a) < towlower (b); } int main() { std::string s {"This is the string to be sorted!"}; std::cout << "Unsorted:\t"" << s << ""\n"; std::sort (s.begin(), s.end(), compare_no_case); std::cout << "Sorted\t:"" << s << ""\n"; }
C++ already provides a string class in the C++ standard template library. #include<iostream> #include<string> int main() { using namespace std; string s {"Hello world!"}; cout << s << endl; }
int i = 0; while(str[i] != NULL){ i++; }
int a; -- variable definition"int a" -- string literal
The easiest way is to use a std::string object. #include<iostream> #include<string> int main() { std::string s("hello world"); std::cout<<s<<std::endl; } You can also use C-style character arrays if you don't need the full functionality of a std::string object.
#include<iostream> #include<vector> #include<string> int main() { std::vector<std::string> names; for (int loop=0; loop!=10;) { std::cout << ++loop << " enter a name: "; std::string name; std::cin >> name; names.push_back (name); } }
int min (int a, int b, int c) {if (a
void main() { int *x = malloc(sizeof(int) * 10); }
#include <iostream> #include <string> int main() { std::string myStr = ""; std::cout << std::endl << "Enter a string: "; std::cin >> myStr; system("PAUSE"); return 0; }
int max (int a, int b) { return a<b?b:a; } int max3 (int a, int b, int c) { return max (max (a, b), c); }
struct point { int x; int y; };
There is no need to overload the plus operator to achieve this. It is already overloaded with this functionality. #include<iostream> #include<string> int main() { int x=40; int y=2; int z=x+y; std::cout<<x<<" + "<<y<<" = "<<z<<std::endl; std::string s1="Hello "; std::string s2="world!"; std::string s3=s1+s2; std::cout<<"""<<s1.c_str()<<"" + ""<<s2.c_str()<<"" = ""<<s3.c_str()<<"""<<std::endl; return(0); }
#include #include #include int reverse(int i);char st[]="ven123kat";void main() {printf("\nThe string is: %s", st);reverse(0);printf("\nReversed string is: %s", st);getch();}int reverse(int i) {if (i