Vous êtes sur la page 1sur 7

String

Introduction:

As we studied in the last session about Array, we could make a string from an array of
characters in order to deal with words (read from user, search about a wanted word ). We will show
this in detail in this session.

Also in this session, we will illustrate what is the meaning of string class and how to use it instead of
array of character.

The meaning of string class

- At first we must show a brief of Classes. They are an expanded concept of data structures
(Structures contain data members), but Classes can also contain functions as members.
- We will explain Classes in detail in higher level of C++ course.
- String class is also used to deal with words like array of characters (but there are some
difference we will illustrate them in this session).
- We must include the library which contains this class definition before using it in our code.

So it is clear that we can implement a string using

1) Array of characters.
2) String class.

Declaration

1) Array of characters.
char s[10];
s[10] = Ahmed;
char s[10] = Ahmed;
2) String class.

#include <string>
string s;
s = Ahmed;
#include <string>
string s = Ahmed;
Note that in Array of characters we must determine the size of this array, which expose us to lose in
memory (if we reserved more than size of array) or lose in data (if we reserved less than size of array).

Eng. Abdelrahman AboTaleb 1


Reading a string from user

1) Array of characters.
1) cin >> s;
2) cin.get (s , any_number);
3) cin.get (s , any_number , any_character);
4) #include <stdio.h>
gets (s);
We can use any of these four methods in order to read from user:

First method is used to enter only one word (As compiler finishes implementation of this code
when user enters a space button).
Second method is used to enter more than one word (As compiler finishes implementation of
this code when user enters number of characters that represents second parameter).
Third method: compiler finishes implementation of this code when user enters number of
characters that represents second parameter or enters the character that represents third
parameter.
Forth method same as first method.

2) String class.
1) cin >> s;
2) getline (cin , s);
We can use any of these two methods in order to read from user:

First method is used to enter only one word (As compiler finishes implementation of this code
when user enters a space button).
Second method is used to enter more than one word, only one line (As compiler finishes
implementation of this code when user enters an enter button).

Eng. Abdelrahman AboTaleb 2


Some string Functions

1) Array of characters.
i. strlen()
ii. strcpy()
iii. strcat()
iv. strcmp()

strcat (st)
It is used to return the length of a string.

Example (1):
Write a program to count the number of occurrence of character (m) in this string
(mohammed-mahmoud).
___________________________________
Solution:
We will use for loop to pass on string`s characters and use if condition to check if this
character similar to the required character. If true, we will increase our counter,and so
on.
Strlen is used in for loop to identify the break condition.
#include <iostream>
using namespace std;

int main()
{
char st[100], ch;
int count = 0;
cout << "Enter required char"<<endl;
cin >> ch;
cout << "Enter your statement"<<endl;
cin >> st;
for (int i = 0; i < strlen(st); i++)
{
if (st[i] == ch) count++;
}
cout << count << endl;

return 0;
}
Output:
5

strcpy (s1 , s2)


It is used to copy a string in another string.
Example (2):
Deduce the output of the following code
_________________

#include <iostream>
using namespace std;

Eng. Abdelrahman AboTaleb 3


int main()
{
char s1[] = "Mohammed ", s2[] = "Zayd ", s3[20], s4[20];
strcpy_s(s3, s1);
strcpy_s(s4, "Sayed ");
strcpy_s(s1, s2);
cout << s1 << s2 << s3 << s4 << endl;

return 0;
}
_________________
Solution:
Zayd Zayd Mohammed Sayed

strcat (s1 , s2)


It is used to concatenate s2 with s1 (i.e. s2 will be in the tail of s1).
Note that s2 won`t be changed.
Example (3):
Deduce the output of the following code
_________________
#include <iostream>
using namespace std;

int main()
{
char s1[20] = "Mohammed ", s2[] = "Zayd ";
strcat_s (s1 , s2);
cout << s1 << endl << s2 << endl;

return 0;
}
_________________
Solution:
Mohammed Zayd
Zayd

Note that if we declare s1 as char s1[] = "Mohammed ", s2[] = "Zayd ";
compiler will send you error message as max size of s1 will be 9, then compiler can`t
concatenate any character to s1.

strcmp (s1 , s2)


It compares s1 with s2 and returns
0 if s1 =s2
+ve value if s1 > s2.
-ve value if s1 < s2.
Note that strcmp compares the first character in s1 with the first character in s2 and the string
whose character has a higher ascii (search about ascii) is the larger string.
If s1 and s2 has the same first character, then strcmp will compare the second character and so
on.

Eng. Abdelrahman AboTaleb 4


Example (4):
Write a calculator program which user enters required operation in string type.
- Your calculator provides addition, subtraction, multiplication and division.
- Don`t divide by zero.
Soution:
#include <iostream>
using namespace std;

int main()
{
float x, y;
char op[10];
cout << "Enter your operation" << endl;
cout << "Hint: valid operators are add, sub, mul and div only" << endl;
cin >> x >> op >> y;
if (strcmp(op, "add") == 0)
cout << x + y << endl;
else if (strcmp(op, "sub") == 0)
cout << x - y << endl;
else if (strcmp(op, "mul") == 0)
cout << x * y << endl;
else if (strcmp(op, "div") == 0 && y != 0)
cout << x / y << endl;

return 0;
}

2) String class.

L = s.length ( );
= s. size ( );
This function will return the length of string s.

s1 = s2;
s1 = Ali;
We use operator (=) to clear contents of string s1 and copy contents of string s2 to string s1.
Or To put any string Ali to s1.

s1 = s1 + s2;
It is used to concatenate s2 with s1 (i.e. s2 will be in the tail of s1).

Example (5):
Deduce the output of the following code
_________________
#include <iostream>
#include <string>
using namespace std;

int main()

Eng. Abdelrahman AboTaleb 5


{
string s1 = "Mohammed ", s2 = "Zayd ";
cout << s1 + s2 << endl;

return 0;
}
_________________
Solution:
Mohammed Zayd

if (s1 == s2) . ;
if (s1 > s2) . ;
if (s1 < s2) . ;
We can use if condition to compare between two strings s1, s2.
Note that If condition compares the first character in s1 with the first character in s2 and the
string whose character has a higher ascii (search about ascii) is the larger string.
If s1 and s2 has the same first character, then If condition will compare the second character
and so on.

Example (6):
Deduce the output of the following code
_______________________
#include <iostream>
#include <string>
using namespace std;
int main()
{
string st;
string s1 = "Mohammed " , s2 = "Zayd ";
if (s1 > s2)
{
st = s1 + s2;
cout << st << endl;
}
else
{
st = s2 + s1;
cout << st << endl;
}
return 0;
}
______________________
Solution:
Zayd Mohammed

Example (7):
Write a calculator program which user enters required operation in string type.
- Your calculator provides addition, subtraction, multiplication and division.
- Don`t divide by zero.

Eng. Abdelrahman AboTaleb 6


Soution:

#include <iostream>
#include <string>
using namespace std;

int main()
{
float x, y;
string op;
cout << "Enter your operation" << endl;
cout << "Hint: valid operators are add, sub, mul and div only" << endl;
cin >> x >> op >> y;
if (op == "add")
cout << x + y << endl;
else if (op == "sub")
cout << x - y << endl;
else if (op == "mul")
cout << x * y << endl;
else if (op == "div" && y != 0)
cout << x / y << endl;

return 0;
}

Eng. Abdelrahman AboTaleb 7

Vous aimerez peut-être aussi