• (2 Pages)
  • +
  • 1
  • 2

C++ Strings A beginner's introduction to strings in C++ Rate Topic: ***** 7 Votes

#1 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Posted 01 February 2008 - 06:48 AM

*
POPULAR

An introduction to strings using the standard C++ string tools

Preface
This article is aimed at complete beginners, who need no more experience in using the C++ language other than simple programs involving arithmetic with integers, and interaction with the user through cin and cout. Here, strings are introduced as a beginners' topic, where they belong, rather than as a more advanced topic, which is, unfortunately where many C++ books and tutorials seem to place them. Behind the scenes, strings may not be trivial beasts, but C++ hides all that away from the programmer, allowing many common problems to be solved with just a few keystrokes.

For those with a 'C' background, or who have been taught strings in C++ the 'old fashioned' way, then its time to hang up that knowledge and learn a new, better way to think about strings. The way that the 'C' language handles strings is considered poor practice for a C++ program, and certainly not beginners' territory. For this reason, 'C' string handling will not be discussed further.


Introduction
Natural languages are based upon words and punctuation, connected to form sentences and paragraphs. In computer science jargon, these are usually referred to as strings. Computer programming languages all need to represent natural language in some form or another, either to interact with users, record data to a file, send data to a printing device, etc. This article defines what is really meant by string in C++, and presents an introduction to the way strings are used in C++ programs.


Strings as raw data
Here's a familiar example of a program which contains a string. In this program, that string is used to send the user a message
#include <iostream>

using namespace std;

int main()
{
	cout << "Hello, World";
} 


The text enclosed in double quotes is known in C++ jargon as a string literal. The term "literal" implies that the string is 'raw' data (representing natural language). It may not be modified at any time, and the data is hard-wired into the program.

Just for clarity, C++ is very explicit about what constitutes a string literal; Only words and characters which appear in-between double quotes "". All other symbols and characters in the above program are a part of the program's own syntax, which are translated by the compiler to generate machine-code instructions.


C++ Strings
Here is a new example containing another kind of string, a C++ String. Sometimes these are known as string variables or string objects, to differentiate between these and string literals, but also to differentiate them from the way that strings are used in the 'C' programming language.
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string hello = "Hello, World";
	cout << hello;
} 

There are some new elements to this program; The first is
#include <string>
This instruction informs the compiler that the program needs to use the <string> library. This library contains a whole bunch of pre-fabricated tools which make the programmer's life easier when handling string data. Any program using C++ Strings must add that line somewhere.

Here is the instruction which creates the C++ string.
string hello = "Hello, World";
The left-hand-side of the = symbol is the same string literal from the first program, "Hello, World". On the right-hand-side, is a C++ string object which has a variable name of hello.

Quote

Note - Just to dispel any confusion, The term variable takes a different meaning in computer science than the common English language definition. In this context, a variable is a name for an area of memory. the usage by computer scientists of the term variable derives from its mathematical meaning, where a 'variable' is a name/identifier for an unknown or changable component of an algebraic expression



Behind the scenes, the program creates a C++ string variable called hello. This variable owns an area in memory, to which the data, "Hello, World" is copied into. the 'hello' variable allows the programmer to access, modify or delete any of the data stored in this memory. What actually goes on behind the scenes is unimportant, however, unlike a string literal, the data stored in memory is modifiable, or mutable. The possibilities which are opened up by this fact are far more interesting.

Quote

Note - The term mutable derives from the latin word for "change", which is 'muto'. C and C++ programmers often refer to objects as being mutable if the programmer is allowed by the language to change its stored data. If the language prevents the programmer from making any alterations, then that object or data is said to be immutable.



Strings as input
Often times, a programmer might want to ask the user to input a string, such as the their name, the name of a file, details about a book, etc. C++ strings are well equipped for this task.
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string your_name;
	cout << "Hello, What is your name? ";
	cin >> your_name;
	cout << "Pleased to meet you, " << your_name;
} 

Again, the program creates a C++ string, but initially leaves it empty. After sending the user a message, the program calls upon cin to help out.
cin >> your_name;
The program should wait for the user to type their name and press enter, then output a message directed at them, "Pleased to meet you".

Output:
Hello, What is your name? Bench
Pleased to meet you, Bench 

There's a hidden problem, however. If the user enters their full name, cin only recognises the first part of their name, the rest is left alone.

Output:
Hello, What is your name? Joe Bloggs
Pleased to meet you, Joe

What happens here is a direct result of one of the shortcomings of the >> symbol, which is used for reading data from the user. The program only reads from the user up until the first 'white space' character. A white space character is usually either a space, a tab, a carriage return, or a new line. This is problematic; especially if the next action the program does is asks the user for some different input - the program retrieves the user's second name instead - suddenly, a simple program just got messy!

The solution is to force the program to read everything up until the point when the user hit 'return' or 'enter'. C++ has the perfect tool for the job, called getline which, as its name suggests, retrieves an entire line of text.
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string full_name;
	cout << "Hello, What is your name? ";
	getline( cin, full_name );
	cout << "Pleased to meet you, " << full_name;
} 

getline is a function. The whens/whys/hows of functions are are beyond the scope of this article; for the moment, a function is just a name which performs some task. In this case, the task performed by getline, is to use some behind-the-scenes faerie magic involving cin and full_name to take a line of text from the user, and dump that text into the memory owned by the full_name variable.


Modifying string data
Often, strings stored in memory need changing in some way, in order to suit a specific purpose to solve a particular problem. the area of software development surrounding strings is usually referred to as String manipulation, an extremely large, expansive topic within software development which could easily fill a book on its own. Though the purpose of this article is just to demonstrate some very simple, easy techniques to fulfil some simple tasks.


Concatenation
Concatenation is a long word which simply means 'adding strings together'. So far, all the examples have shown blank strings being filled from other strings, and from the user, but sometimes, strings aren't blank, and a problem requires the existing string to be kept intact, with a little bit more added to the end.

Suppose the "what is your name" program were to work differently, asking for the user to type their forename and surname separately, but then needed the two names meshed together as a single full name string. The answer to the problem is as simple as the problem itself - C++ strings allow the + symbol to be used to concatenate two or more strings together seamlessly.

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string forename, surname;
	cout << "Hello, what is your first name? ";
	getline(cin, forename);
	cout << "Hello, " << forename << ", what is your surname? "; 
	getline(cin, surname);

	string full_name = forename + " " + surname;
	cout << "Pleased to meet you, " << full_name;
} 


This program works well. The next bit of code shows that this technique has a 'danger' sign hanging over it. Due to historical reasons from the 'C' programming language, the + symbol, when used to concatenate string literals has a rather nasty little "gotcha", which isn't at all obvious to the casual reader skimming over a program, but will throw up the most confusing compiler error.
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string full_name = "Joe " + "Bloggs";
	cout << "Hello, " << full_name;
} 
Compiler: error C2110: '+' : cannot add two pointers

Uh-oh! In plain english, that means the compiler doesn't understand what the program intends to do; or, more accurately, it thinks the program is trying to do something completely different, something which the compiler knows is bad for your health. it doesn't realise that the program merely wants to concatenate "Joe " and "Bloggs" together.

But here's the reason why - "Joe " and "Bloggs" are string literals. Forget the fact that there is a string variable on the other side of the = sign. The compiler has to process instructions in a specific order, and the first thing it sees is "Joe " + "Bloggs".
string literals cannot be modified, ever, which means the programmer must be careful when using + to concatenate strings, that at least one of the strings is a C++ string object.

On the plus side, the compiler is there all the way to report errors (Even if the errors are cryptic), and there are many other ways to do this correctly. One neat way is to concatenate strings one-at-a-time, using += instead
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string full_name;
	full_name += "Joe ";	 
	full_name += "Bloggs";
	cout << "Hello, " << full_name;
} 


Concatenation is a one-way process. Being that concatenation uses the + and += operators, it might seem logical that the 'subtraction' operator -, does the opposite. Alas, there is no such operation which allows the programmer to 'subtract' anything from a string in this way.


Strings as character sequences
Thus far, strings have been treated as complete entities, although, in reality, strings are actually sequences of characters. Many problems involving strings are interested with specific characters, and the positions where those characters occur in a string.

C++ provides complete un-hindered access to characters in a string through the subscript operator. the subscript operator is a pair of square brackets, which takes an integer, representing a character's position in a string.
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string hello = "Hello, World!";
	cout << "The first character of " << hello
		 << " is " << hello[0];
} 

Output: The first character of Hello, World! is H

The first character in a string is always position zero. This seemingly illogical idea comes from the fact that computers always start counting from zero. If there's ever a golden rule which all programmers need to recite in their sleep, its this one about computers counting 0,1,2,3 etc.
- Since the first character in a string is position zero, logic dictates that the last character in a string must be at a position 'one less' than the string's length. For "Hello, World!", which is 13 characters long, its last character will appear at position 12.
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string hello = "Hello, World!";
	cout << "The last character of " << hello
		 << " is " << hello[12];
} 
The notion of beginning to count from zero seems unnatural at first, but get used to it - Counting from zero upwards is a recurring theme in C++. When the programmer gets it wrong and begins accessing positions which exist beyond the boundaries of the string, the compiler can't help, and the program will display all kinds of weird behaviour.


How long is a string?
Since finding the string's upper boundary is clearly important, C++ has another neat tool for the job, a function called size() to find the number of characters in a string. The programmer already knows that the string starts at position zero, so with knowledge of how to obtain the string's size, the above program can be re-factored to work with a string of any length.
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string hello = "Hello, World!";
	int last = hello.size() - 1;
	cout << "The last character of " << hello
		 << " is " << hello[last];
} 

Output : The last character of Hello, World! is !

Unlike the getline function before, which stood on its own, size() is a part of the 'hello' string object itself. This is denoted by a period . which appears in-between the string variable name hello and size(). string objects are complicated beasts behind the scenes, which store all kinds of features that aren't available to other types of data; There's no use memorising them all, though some are useful to have at hand.


Changing characters within a string
Since string objects are mutable (modifiable), then it follows that individual characters within the string ought to be just as easily changeable. The subscript operator provides direct access to the memory area where a particular character is stored, as a consequence of this, any character within a string object can be modified in much the same way as a normal variable.
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string hello = "Hello, World!";
	hello[0] = '*';
	cout << hello;
} 

Output: *ello, World!

The notion of adding the [ ] symbol to the left-hand side of the = may look a little odd, however, remember the name hello on its own identifies the entire string. The subscript operator tells the compiler to locate a particular character within the string instead, and allows that character's memory location to be directly referenced.

Quote

Note - lone character values always appear in single quotes. This is for the compiler to differentiate between a character value and a string literal. Characters and strings are like apples and oranges from the compiler's perspective; mixing up the two will generally result in the compiler throwing out an error.



Summary : Putting it all together
Here's a program which combines together a few of the techniques discussed in this article. The program asks the user to type his/her full name, and then tells the user what the middle character of their name is (If their name has an even number of characters, then the program automatically rounds down)
#include <iostream>
#include <string>

using namespace std;

int main()
{
	cout << "Please enter your full name: ";
	string full_name;
	getline( cin, full_name );
	int middle = full_name.size() / 2;

	string greeting = "Hello " + full_name + ", ";
	cout << greeting << "the middle character of your name is "
		 << full_name[middle] << " at position "
		 << middle;
} 



This article has introduced strings, covering literals and C++ string objects, whilst demonstrating a number of commonly useful techniques to crack simple string problems. The reader should now be equipped with knowledge of how to create C++ strings and fill them with data, how to add strings together, and how to manipulate individual characters within a string.
- the C++ string library is far more powerful than shown by the short examples here. When combined with other features of the C++ language, strings can enable a programmer to do some remarkably interesting things.



References
The C++ Programming Language - Bjarne Stroustrup
The C++ Standard Library - Nicolai Jossutis
Accelerated C++ - Andrew Koenig & Barbara E. Moo

Is This A Good Question/Topic? 20
  • +

Replies To: C++ Strings

#2 <m0ng00se>  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-May 09

Posted 22 June 2009 - 08:06 AM

Thanks, this helped me a lot!
Was This Post Helpful? 0
  • +
  • -

#3 macom21  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 24-January 10

Posted 27 January 2010 - 10:13 AM

thank you for leading me to the strings tutorial.
Was This Post Helpful? 1
  • +
  • -

#4 genomega  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 3
  • Joined: 17-May 10

Posted 17 May 2010 - 02:03 PM

A refresher on the basics is always useful, thanks.
Was This Post Helpful? 0
  • +
  • -

#5 Kutlar  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 29
  • Joined: 04-March 10

Posted 02 July 2010 - 08:12 PM

Good tutorial on strings, I've been reading a pile of texts lately on C++, and believe it or not, you're tutorial explained basic string manipulation the best :) Thanks
Was This Post Helpful? 1
  • +
  • -

#6 bennitto  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 64
  • Joined: 28-November 09

Posted 06 September 2010 - 09:02 AM

I wish there was a way of giving you 10 rep points for this.

the book I am using teaches strings in the "old fashioned" way and this has certainly been a refreshing break from the tutorial I am following.
Was This Post Helpful? 1
  • +
  • -

#7 Guest_Acetolyne*


Reputation:

Posted 09 September 2010 - 02:19 AM

Thank you very useful info helping me get started with using C++ and increasingmy programming skills. The tutorial is straight foward and explains many things I feel are left out in conventional tutorials. Much appriciated
Was This Post Helpful? 0

#8 Chevon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 39
  • Joined: 25-September 10

Posted 26 September 2010 - 10:45 AM

wow this is great makes everything very clear now.
Was This Post Helpful? 0
  • +
  • -

#9 yarvin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 23-April 09

Posted 27 December 2010 - 04:16 PM

Awesome! This is a great refresher for me! Thank you! =]
Was This Post Helpful? 0
  • +
  • -

#10 PexMech  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 24-February 11

Posted 24 February 2011 - 07:10 PM

:bigsmile: Reminds me of something...

"No Strings attached"




Thanks for a great tutorial!
Was This Post Helpful? 0
  • +
  • -

#11 Severy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 4
  • Joined: 04-November 09

Posted 25 February 2011 - 04:40 PM

Very nice article.. learned a few things a moment ago :)

Thanks!
Was This Post Helpful? 1
  • +
  • -

#12 flybob1979  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-August 11

Posted 06 August 2011 - 04:57 PM

I've learned alot from this tutorial thank you.
I'm new to programming but from reading this tutorial i have a question, can strings be used as a form of array?

This post has been edited by flybob1979: 06 August 2011 - 04:58 PM

Was This Post Helpful? 0
  • +
  • -

#13 grocerygrip  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-August 11

Posted 10 August 2011 - 12:02 PM

Thank you very much!! This basic string tutorial is more insightful than the school book!
Was This Post Helpful? 0
  • +
  • -

#14 Torasko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-February 12

Posted 21 February 2012 - 08:29 AM

Perfect tutorial, thanks a bunch!
Was This Post Helpful? 0
  • +
  • -

#15 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,376
  • Joined: 31-December 10

Posted 22 February 2012 - 10:49 AM

View Postflybob1979, on 06 August 2011 - 06:57 PM, said:

I've learned alot from this tutorial thank you.
I'm new to programming but from reading this tutorial i have a question, can strings be used as a form of array?

To a certain degree you can treat C++ strings as an array of characters. There is also a member function that returns the contents of the string as a C-style NULL-terminated string so it can be used in function calls that expect to see a const char*, and that function is std::string::c_str(). You could also make an array of strings. Here is a small example showing how to use a string as an array:
#include <iostream>
#include <string>
using namespace std;

void printCString(const char* s)
{
    cout << s << endl;
}

int main()
{
    string name;
    cout << "Enter name> ";
    cin >> name;
    
    cout << "Hello ";
    printCString(name.c_str());        // pass the function a C-style string using the c_str() member function.
    
    // Loop through the array 1 character at a time and print it out to the screen:
    cout << endl;
    for(unsigned int i = 0; i < name.size(); ++i)
        cout << name[i];
    cout << endl;
    
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2