In this tutorial we will talk more about strings. Strings are just bunch of a text that we need to print. That text is inside double quotes.
#include <iostream>
using namespace std;
int main() {
cout << "This is first string";
cout << "This is second string";
return 0;
}
When we run this code, this will be result:
This is first stringThis is second string
Process returned 0 (0x0) execution time : 0.019 s
Press any key to continue.
Because we don't have endl at the end of instructions, string are concatenated.
#include <iostream>
using namespace std;
int main() {
cout << "This is first string \n";
cout << "This is done using endl" << endl;
cout << "This is done using endl" << endl;
cout << "This is second string \n";
return 0;
}
After we run this code we can notice that having \n at the end of instruction also do job of that endl does. But we will use endl; mostly.
Having whitespaces verticaly will not influence end result, only \n and endl; instructions. Don't forget semicolons, they are important.
Result:
This is first string
This is done using endl
This is done using endl
This is second string
Process returned 0 (0x0) execution time : 0.050 s
Press any key to continue.
To get to new empty line we can use "\n" on dedicated line. Also, you can have empty string (just quotes) and with endl; that will be printed too:
#include <iostream>
using namespace std;
int main() {
cout << "This is first string \n";
cout << "\n";
cout << "This is done using endl" << endl;
cout << "This is done using endl" << endl;
cout << "" << endl;
cout << "This is second string \n";
return 0;
}
Result:
This is first string
This is done using endl
This is done using endl
This is second string
Process returned 0 (0x0) execution time : 0.317 s
Press any key to continue.
We can also style our output a bit using tabs, aligning quotes verticaly. Special simbols like underlines are also normal characters, there's no problem printing them:
#include <iostream>
using namespace std;
int main() {
cout << "This is first string " << endl;
cout << "--------------------------- " << endl;
cout << "This is done using endl " << endl;
cout << "This is done using endl " << endl;
cout << "___________________________ " << endl;
cout << "This is second string " << endl;
return 0;
}
Result:
This is first string
---------------------------
This is done using endl
This is done using endl
___________________________
This is second string
Process returned 0 (0x0) execution time : 0.051 s
Press any key to continue.
Strings are simple things.
No comments:
Post a Comment