Welcome to one of the most important articles you’ll read as a beginner coder! When it comes to coding, strings are a very important concept, no matter what programming language you’re using. Think of strings as a displayed message. This message can be virtually any sequence of characters from your keyboard, such as letters, numbers, and symbols.
If you are a beginner and want to learn to code from scratch, check out our FREE 30-minute introduction course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes
Let’s start off with a basic string. If you want to test out some coding in real time, hop over to rextester.com. This website will allow you to code in the main window, and test it out instantly. Type in the following:
Lets break down this piece of code. The
std:: means that the program is drawing from the standard C++ library. You’ll have to use this syntax every time you declare a string. The
string means that this particular piece of code is going to be a string.
At this point, we really haven’t done much. We should give the string a name:
The string now has a name! Now, this still doesn’t actually do anything yet. The compiler will just know that a certain string,
my_string, exists. It can now be referred to later in the code.
In order for this string to be really useful, we have to assign in a value. Let’s do that.
std::string my_string = "mammoth interactive";
Now, the string has the attached value of “mammoth interactive”. Any time my_string is printed, it will print the phrase “mammoth interactive”. Congratulations! You just declared your first string.
In rextester.com, the main coding screen should have something like this in it:
int main ()
std:cout << "Hello, world!\n";
In order to test our string, replace the
"Hello, world!\n" with
my_string. Scroll down to the bottom and press “Run It”.
You will see the text “mammoth interactive” come out.
Want to learn more? Sign up for our FREE 30-minute introduction course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes. Happy coding!