Page 298 - Computer_Science_F5
P. 298
The following are some examples that use the string operations strcpy( ), strcat( ),
strlen( ), strcmp( ), strchr( ), and strstr( ).
strcpy( ) Chapter Four: Object oriented programming with C++
The following Program Example 4.36 shows the use of a string function for copy.
FOR ONLINE READING ONLY
Program Example 4.36:
Using Strcpy( ) string function
Taking the string s1 and s2 in strcpy(s1,s2). This string function of C++ will copy
the string s2 into string s1.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char string1[]=”Hujambo”;
char string2[]=”Mwananzengo”;
strcpy(string1, string2);
cout << “The result of strcpy is : “ <<string1 << ‘\n’;
}
Output: The value of string2 (Mwananzengo) will be copied to string1 (Hujambo)
and string2 value will display (Mwananzengo) as shown in Figure 4.67.
Figure 4.67: Output of string2
strcat( )
The following Program Example 4.37 shows the use of the string function for
concatenation.
Program Example 4.37:
Using strcat( ) string function
Taking the string s1 and s2 in strcat(s1,s2). This string function of C++ will
concatenate the string s2 at the end of string s1.
289
Student’s Book Form Five
Computer Science Form 5.indd 289 23/07/2024 12:34

