Strings 2
Strings 2
Strings
• A string is a sequence of characters, and can be the empty string
• In C++, a string has “double quotes”, not single quotes:
• “this is a string”
• ‘this is not a string’
• In C++, there are two types of strings;
i. C strings (char arrays), inherited from the C language.
ii. C++ strings (string objects), which is part of the standard C++
library. A more modern approach to manipulating strings.
C strings (char arrays)
• Although the string type described here may be a bit “old fashioned,”
it is still widely used and is an integral part of the C++ language.
• It leverages on the concepts of arrays and the char data type.
• For example, if we want to represent the string "Hello World“ in a C++
program, we can define an array of characters with 12 indexed
variables.
• 11 for the 11 letters in “Hello World” (including the space in between
the words). Plus one for the character ‘\0’, which serves as an end
marker.
C strings (char arrays)
• The character ‘\0’ is called the null character and is used as an end
marker because it is distinct from all the “real” characters.
• The end marker determines when the program should stop reading
the content of the array, therefore the end of the string
• A string stored in this way (as an array of characters terminated with
‘\0’) is called a C string.
• For example
C strings (char arrays)
• The previous example can also be expressed like this:
char text[12] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '\0’};
• Note the presence of the end marker ‘\0’, as that is what makes it a
C-string
C strings (char arrays)
• A C-string variable is an array, so it has indexed variables that can be
used just like those of any other array.
• So in the previous example, we can manipulate the individual indexes
of the array.
• For example, we can replace the space in between the “Hello World”
simply by accessing the index and giving it a new value.
• text[5] = ‘-’;
Common C strings functions
• C string provides some functions which can be used to perform
common related activities with strings. They are in the cstring library.
Some of them include:
• Copying: strcpy (Target_String, Src_String)
• Comparison: strcmp(Target_String, Src_String)
• Length: strlen(Src_String)
• Concatenation: strcat(Target_String, Src_String)
String concatenation
Standard C++ string
• The class string is defined in the library whose name is also , and the
definitions are placed in the std namespace.
• The class string is often thought of as a modern replacement for C
strings.
• The class string allows you to treat string values and string
expressions very much like values of a simple type. You can use the =
operator to assign a value to a string variable, and you can use the +
sign to concatenate two strings.
• For example, if a = “Hello” and b = “ World”, then c = a + b will be
“Hello World”.
Standard C++ string
• C++ strings can also be created using a constructor. For example,
• string text(“Hello”) is the same as string text = “Hello”.
• With the C++ strings, we can also take in value from the user with cin.
Standard C++ string
• In the previous program, entering a string with space in between will
not be fully read, only the first word will be read into the variable.
• C++ provides a special function for dealing with this kind of situation.
The getline function.
Common Standard C++ strings functions
• Standard C++ string provides some functions which can be used to
perform common related activities with strings. Some of them
include:
• Searching: str.find(str1)
• Concatenation: str1 += str2
• Insertion: str.insert(pos, str2);
• Removal: str.remove(pos, length);
Concatenation with Append
Using cin function
Using getline function
Thank you