0% found this document useful (0 votes)
13 views15 pages

Strings

The document provides an overview of C++ strings, detailing how to declare, initialize, and manipulate string variables. It covers basic string operations such as concatenation, accessing characters, comparing strings, and using functions like getline, find, and substr. Additionally, it introduces character string functions for testing and manipulating character data.

Uploaded by

randsand97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views15 pages

Strings

The document provides an overview of C++ strings, detailing how to declare, initialize, and manipulate string variables. It covers basic string operations such as concatenation, accessing characters, comparing strings, and using functions like getline, find, and substr. Additionally, it introduces character string functions for testing and manipulating character data.

Uploaded by

randsand97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Strings

C++ Strings One of the most useful data types supplied in the C++ libraries is
the string. A string is a variable that stores a sequence of letters or other
characters, such as "Hello" or "May 10th is my birthday!". Just like the other
data types, to create a string we first declare it, then we can store a value in it.
string testString;
testString = "This is a string.";

We can combine these two statements into one line:


string testString = "This is a string.";

Often, we use strings as output, and cout works exactly like one would expect:
cout << testString << endl;

will print the same result as


cout << "This is a string." << endl;

In order to use the string data type, the C++ string header <string> must be
included at the top of the program. Also, you’ll need to include using
namespace std; to make the short name string visible instead of requiring the
cumbersome std::string. (As a side note, std is a C++ namespace for many
pieces of functionality that are provided in standard C++ libraries. For the
purposes of this class, you won't need to otherwise know about namespaces.)
Thus, you would have the following #include's in your program in order to use
the string type.

---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 1 ……………..………. Computer Programming
#include <string>
using namespace std;

example program:
#include <iostream>
#include <string>
using namespace std;

int main ()
{ string str1 = "Hello";
string str2 = "World";
string str3;
int len;

// copy str1 into str3


str3 = str1;
cout << "str3 : " << str3 << endl;

// concatenates str1 and str2


str3 = str1 + str2;
cout << "str1 + str2 : " << str3 << endl;

// total lenghth of str3 after concatenation


len = str3.size();
cout << "str3.size() : " << len << endl;
return 0;
}

---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 2 ……………..………. Computer Programming
When the above code is compiled and executed, it produces result something as
follows:
str3 :Hello
str1 + str2 : HelloWorld
str3.size() : 10

cin and strings


The extraction operator can be used on cin to get strings of characters in the
same way as with fundamental data types:

However, cin extraction always considers spaces (whitespaces, tabs, new-line...)


as terminating the value being extracted, and thus extracting a string means to
always extract a single word, not a phrase or an entire sentence.

To get an entire line from cin, there exists a function, called getline, that takes
the stream (cin) as first argument, and the string variable as second. For
example:

Notice how in both calls to getline, we used the same string identifier (mystr).

---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 3 ……………..………. Computer Programming
What the program does in the second call is simply replace the previous content
with the new one that is introduced.

Basic Operations
1. Counting the number of characters in a string:
The length method returns the number of characters in a string, including
spaces and punctuation. Like many of the string operations, length is a
member function, and we invoke member functions using dot notation. The
string that is the receiver is to the left of the dot, the member function we are
invoking is to the right, (e.g. str.length()). In such an expression, we are
requesting the length from the variable str.
example program:
#include <string>
#include <iostream>
using namespace std;

int main()
{ string small, large;
small = "I am short";
large = "I, friend, am a long and elaborate string indeed";

cout << "The short string is " << small.length( )


<< " characters." << endl;
cout << The long string is " << large.length( )
<< " characters." << endl;
return 0;
}

---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 4 ……………..………. Computer Programming
output:
The short string is 10 characters.
The long string is 48 characters.

2. Accessing individual characters:

Using square brackets, you can access individual characters within a string as if
it’s a char array. Positions within a string str are numbered from 0 through
str.length() - 1. You can read and write to characters within a string using ] [ .

example program:
#include <string>
#include <iostream>
using namespace std;

int main()
{ string test;
test = "I am Q the omnipot3nt";

char ch = test[5]; // ch is 'Q'


test[18] = 'e'; // we correct misspelling of omnipotent

cout << test << endl;


cout << "ch = " << ch << endl;
return 0;
}

output:
I am Q the omnipotent
ch = Q
---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 5 ……………..………. Computer Programming
Be careful not to access positions outside the bounds of the string. The square
bracket operator is not range-checked and thus reading from or writing to an
out-of-bounds index tends to produce difficult-to-track-down errors. There is
an alternate member function at(int index) that retrieves the character at a
position with the benefit of built-in range-checking, but it’s used much less
often.

3. Passing, returning, assigning strings:


C++ strings are designed to behave like ordinary primitive types with regard to
assignment. Assigning one string to another makes a deep copy of the character
sequence.

string str1 = "hello";


string str2 = str1; // makes a new copy
str1[0] = 'y'; // changes str1, but not str2

Passing and returning strings from functions clones the string. If you change a
string parameter within a function, changes are not seen in the calling function
unless you have specifically passed the string by reference.

4. Comparing two strings:


You can compare two strings for equality using the == and != operators.
Suppose you ask the user for his or her name. If the user is Julie, the program
prints a warm welcome. If the user is not Neal, the program prints the normal
message. Finally… if the user is Neal, it prints a less enthusiastic response.
example program:
#include <string>
#include <iostream>

---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 6 ……………..………. Computer Programming
using namespace std;
int main()
{ string myName = "Neal";
while (true) {
cout << "Enter your name (or 'quit' to exit): ";
string userName = getLine( );
if (userName == "Julie")
{ cout << "Hi, Julie! Welcome back!" << endl;
}
else if (userName == "quit") {
// user is sick of entering names, so let's quit
cout << endl; break;
} else if (userName != myName) {
// user did not enter quit, Julie, or Neal
cout << "Hello, " << userName << endl;
} else {
cout << "Oh, it’s you, " << myName << endl;
}
}
return 0;
}
output:
Enter your name (or 'quit' to exit): Neal
Oh, it's you, Neal
Enter your name (or 'quit' to exit): Julie
Hi, Julie! Welcome back!
Enter your name (or 'quit' to exit): Leland
Hello, Leland
Enter your name (or 'quit' to exit): quit
---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 7 ……………..………. Computer Programming
You can use <=, >, and >= to compare strings as well. These operators compare
strings lexicographically, character by character and are case-sensitive. The
following comparisons all evaluate to true: "A" < "B", "App" < "Apple",
"help" > "hello", "Apple" < "apple". The last one might be a bit confusing,
but the ASCII value for 'A' is 65, and comes before 'a', whose ASCII value is
97. So "Apple" comes before "apple" (or, for that matter, any other word that
starts with a lower-case letter).

5. Appending to a string:
C++ strings are wondrous things. Suppose you have two strings, s1 and s2 and
you want to create a new string of their concatenation. Conveniently, you can
just write s1 + s2, and you’ll get the result you’d expect. Similarly, if you want
to append to the end of string, you can use the += operator. You can append
either another string or a single character to the end of a string.
example program:
#include <string>
#include <iostream>
using namespace std;

int main()
{ string firstname = "Leland";
string lastname = " Stanford";

// concat the two strings


string fullname = firstname + lastname;
fullname += ", Jr"; // append another string
fullname += '.'; // append a single char

cout << firstname << lastname << endl;cout << fullname << endl;
---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 8 ……………..………. Computer Programming
return 0;
}

output:
Leland Stanford
Leland Stanford, Jr.

6. Searching within a string:


The string member function find is used to search within a string for a particular
string or character. A sample usage such as str.find(key) searches the receiver
string str for the key. The parameter key can either be a string or a character.
(We say the find member function is overloaded to allow more than one usage).
The return value is either the starting position where the key was found or the
constant string::npos which indicates the key was not found. Occasionally,
you’ll want to control what part of the string is searched, such as to find a
second occurrence past the first. There is an optional second integer argument to
find which allows you to specify the starting position; when this argument is
not given, 0 is assumed. Thus, str.find(key, n) starts at position n within str
and will attempt to find key from that point on. The following code should
make this slightly clearer:
example program:
#include <string>
#include <iostream>
using namespace std;

int main()
{ string sentence = "Yes, we went to Gates after we left the dorm.";

---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 9 ……………..………. Computer Programming
// finds the first "we"
int firstWe = sentence.find("we");
// finds "we" in "went"
int secondWe = sentence.find("we", firstWe+1);
// finds the last "we"
int thirdWe = sentence.find("we", secondWe+1);
int gPos = sentence.find('G');
int zPos = sentence.find('Z'); // returns string::npos

cout << "First we: " << firstWe << endl;


cout << "Second we: " << secondWe << endl;
cout << "Third we: " << thirdWe << endl;

cout << "Is G there? ";


cout << (gPos != string::npos ? "Yes!" : "No!")<< endl;
cout << "Is Z there? ";
cout << (wPos != string::npos ? "Yes!" : "No!") << endl;

return 0;
}

output:
First we: 5
Second we: 8
Third we: 28
Is G there? Yes!
Is Z there? No!

---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 10 ……………..………. Computer Programming
7. Extracting substrings:
Sometimes you would like to create new strings by extracting portions of a
larger one. The substr member function creates substrings from pieces of the
receiver string. You specify the starting position and the number of characters.
For example, str.substr(start, length) returns a new string consisting of the
characters from str starting at the position start and continuing for length
characters. Invoking this member function does not change the receiver string,
as it makes a new string with a copy of the characters specified.
example program:
#include <string>
#include <iostream>
using namespace std;

int main()
{ string oldSentence;
oldSentence = "The quick brown fox jumped WAY
over the lazy dog";
int len = oldSentence.length();
cout << "Original sentence:" << oldSentence << endl;
int found= oldSentence.find("WAY ");
string newSentence = oldSentence.substr(0, found);
cout << "Modified sentence: " << newSentence
<< endl;
newSentence += oldSentence.substr(found+ 4);
cout << "Completed sentence: " << newSentence << endl;
return 0;
}
output:
Original sentence: The quick brown fox jumped WAY over the lazy dog
---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 11 ……………..………. Computer Programming
Modified sentence: The quick brown fox jumped
Completed sentence: The quick brown fox jumped over the lazy dog
There are a couple of special cases for substr(start, length). If start is
negative, it will cause a run-time error. If start is past the end of the string, it
will return an empty string (e.g., ""). If length is longer than the number of
characters from the start position to the end of the string, it truncates to the end
of the string. If length is negative, then the behavior is undefined, so make sure
that length is always non-negative. If you leave off the second argument, the
number of characters from the starting position to the end of the receiver string
is assumed.

8. Modifying a string by inserting and replacing:


Finally, let’s cover two other useful member functions that modify the receiver
string. The first, str1.insert(start, str2), inserts str2 at position start within
str1, shifting the remaining characters of str1 over. The second,
str1.replace(start, length, str2), removes from str1 a total of length characters
starting at the position start, replacing them with a copy of str2. It is important
to note that these member functions do modify the receiver string.

example program:
#include <string>
#include <iostream>
using namespace std;

int main()
{ string sentence = "CS106B sucks.";
cout << sentence << endl;

// Insert "kind of" at position 8 in sentence


---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 12 ……………..………. Computer Programming
sentence.insert(7, "kind of ");
cout << sentence << endl;

// Replace the 10 characters "kind of su"


// with the string "ro" in sentence
sentence.replace(7, 10, "ro");
cout << sentence << endl;

return 0;
}
output:
CS106B sucks.
CS106B kind of sucks.
CS106B rocks.

Character String Functions


C++ provides several functions that allow you to test and manipulate character
data. The function prototypes are found in the header file name <cctype.h>.
Remember to add the line #include <cctype.h> in program that use these
functions. The table below lists and describes the character functions. Each
function expects one integer argument -the ASCII value of the character to be
tested. Each function returns a non-zero value (true) if the condition tested is
true and 0 (false) if the condition tested is false.

---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 13 ……………..………. Computer Programming
C++ Functions Description
isalpha(character) Returns a nonzero number if the character is a letter ('A' -
'Z', 'a' -'z'); otherwise it returns zero.
isalnum(character) Returns a nonzero number if the character is a letter ('A' -
'Z', 'a' -'z', or '0' -'9'; otherwise it returns zero.
isdigit(character) Returns a nonzero number if the character is digit (0
through 9); otherwise it returns a zero.
isspace(character) Returns a nonzero number if the character is a whitespace
(tab, space, newline); otherwise it returns a zero.
isupper(character) Returns a nonzero number if the character is uppercase;
otherwise it returns a zero.
islower(character) Returns a nonzero number if the character is lowercase;
otherwise it returns a zero.
toupper(character) Return the uppercase equivalent if the character is
lowercase; otherwise it returns the character unchanged.
tolower(character) Return the lowercase equivalent if the character is
uppercase; otherwise it returns the character unchanged.

---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 14 ……………..………. Computer Programming
The example below will convert each lowercase character of a string to
uppercase character and vice versa.
Example Output
#include<iostream> Enter your name:Phuong D. Nguyen
#include<string> The conversion is:pHUONG d.
#include<cctype> nGUYEN
using namespace std;
int main( ){
char name[20];
cout<<"Enter your name:\n ";
cin.getline(name,20);
for( int i = 0; i < strlen(name) ; i++)
{ if (islower(name[i]) )
//convert to uppercase
name[i] = toupper(name[i]);
else
//convert to lowercase
name[i] = tolower(name[i]);
}//Display the result
cout << "The conversion is:\n";
cout << name << endl;}

---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 15 ……………..………. Computer Programming

You might also like