Professional Documents
Culture Documents
String
C++ provides following two types of string representations:
- The C-style character string.
- The string class type introduced with Standard C++.
In the above code, str is a string and it holds 4 character. Although, "C++" has 3
character, the null character \0 is added to the end of the string automatically.
Index 0 1 2 3
Variable C + + \0
C++ compiler automatically places the '\0' at the end of the string when it
initializes the array.
1
Data Structure Lecture-2
Like arrays, it is not necessary to use all the space allocated for the string. For example:
char str[10] = "C++";
#include <iostream>
void main()
{
char str[100];
2
Data Structure Lecture-2
Notice that, in second example only "Hello" is displayed instead of "Hello C++". It
is because the extraction operator >> considers a space has a terminating character.
#include <iostream>
void main()
{
char str[100];
cin.getline(str, 100);
Output:
To read the text containing blank space, cin.getline function can be used. This
function takes two arguments. { cin.getline (arg1, arg2) }
First argument is the name of the string (address of first element of string) and
second argument is the maximum size of the array.
3
Data Structure Lecture-2
#include <string>
#include<iostream>
#include <string>
return 0;
}
<iostream>
4
Data Structure Lecture-2
#include <string>
int main () {
return 0;
}
When the above code is compiled and executed, it produces result something as follows −
str3 : Hello
str1 + str2 : HelloWorld
str3.size() : 10
The + operator can be used between strings to add them together to make a new string. This
is called concatenation:
Example:
In the example above, we added a space after firstName to create a space between Ahmed
and Ali on output. However, you could also add a space with quotes (" " or ' '):
Example:
5
Data Structure Lecture-2
Example:
int x = 10;
int y = 20;
int z = x + y; // z will be 30 (an integer)
Example:
string x = "10";
string y = "20";
string z = x + y; // z will be 1020 (a string)
Example:
string x = "10";
int y = 20;
string z = x + y;
Example:
Example:
You can access the characters in a string by referring to its index number inside square
brackets [].
6
Data Structure Lecture-2
Example:
Note: String indexes start with 0: [0] is the first character. [1] is the second character, etc.
Example:
- To change the value of a specific character in a string, refer to the index number, and use
single quotes:
Example:
7
Data Structure Lecture-2
Pointer
Example of Pointers:
Pointer Operators:
There are two special operators that are used with pointers:
* : value at address …
Operator, returns the value of the variable located at the address specified by its operand.
If ptr is a pointer variable, then ∗ptr gives you the content of the location pointed to by ptr.
8
Data Structure Lecture-2
The following code fragment assigns a value to the location pointed to by the
pointer:
int *ptr;
*ptr = 100;
(*ptr)++;
9
Data Structure Lecture-2
Example:
void main()
{
int *ptr, num; // Step 1
(*ptr)++; // Step 4
(*ptr)--; //Step 5
10