Jun 22
Sunday
C++
Fundamental Data Type String

In this tutorial you shall learn how to use

1. String Data Type

You should immediately notice that the declaration and assignment of a string is similar to int and doubles which you learned previously.

Declaring and Initializing the String

First make sure you have the appropriate headers in the file where you plan to use string objects.

#include <string>
using namespace std;



How to declare and initialize strings:

string student = "Diego";    // Declare and initialize to Diego
student = "Talk Binary"; //Assign student to Talk Binary
 

Notice how strings are encapsulated within “”‘’s. If used without them, it would mean you would want to assign the value of that variable of that name to your string variable.

From the terminal…

cin >> student; //Reads up to the space
getline(cin, student); //Reads up to pressed Enter
 


You may also concatenate strings

string first = "Talk";
string last = "Binary";
string name = first + " " + last; //What happens if we exclude the " " ?
cout << name; //Outputs Talk Binary.
 


Substrings


We can find the length of a string using the length() function and also print out a substring from a string.

string name = "Talk Binary";
cout << name.length() << endl; //Outputs 11 (Counts the space as a character).
cout << name.substr(3,5); //Starts at position 3, and prints 5 characters "k Bin"
 


The position is determined as follows.



You may also use the following…

cout << name[3]; //Will output the character in position 3.
 


Remember, the best way to learn something, is to practice! Therefore, start practicing!

Programming Challenges

Using what we know, we’ll make a small program.

1. Read in a first name, and last name, and assign them to a first and last string variable respectively.

  • a. Find the length of both and assign their lengths to two different int variables respectively.
  • b. Concatenate the first and last name into one string variable.
  • c. Using the substr function, and your two int variables holding length, extract the first and last name from your last string variable that has the first and last name concatenated.




If you have any questions, comments, or concerns please comment below!

Not yet Rated!1 Star2 Stars3 Stars4 Stars5 Stars



4 Responses to “ Fundamental Data Type String ”

  1. Well, I would’t call string a fundamental data type. There are many languages that don’t contain string data type and are doing just fine (C for example).

  2. True, since its simply an array of characters but I had it engraved in to my mind as being “fundamental” since it was one of the first thing they taught us. I’ll probably end up changing the name then.

  3. Andy Amaya
    Jul 2, 2008
    Reply

    Hey Diego, this is missing something that would probably cause a headache to a beginner. Don’t for get that in order to use the string Data type as you are describing it they must add

    #include // This
    #include // or this
    using namespace std; // but definitely this if they want to
    // use it how you are explaining other wise they could use it like this

    std:: string new_string = “what”; // if
    using namespace std;
    // is not used.

  4. Thanks for mentioning it. I added it into the tutorial!


Post a Comment