Page 297 - Computer_Science_F5
P. 297
Computer Science Table 4.19: String library function Application
Library function
Find length of the string
strlen( )
Copy one string to another
strcpy( )
FOR ONLINE READING ONLY
strcat( )
strcmp( ) Combine two strings
Compare characters of two string
String operation
Reading and writing in a C++ program are controlled by ‘cin’ and ‘cout’. While
‘cin’ reads a string in a program, ‘cout’ is responsible for writting that string to
the console window. Program Example 4.35 of a C++ displays name and age. The
output is shown in Figure 4.66.
Program Example 4.35:
Using character to demonstrate string function
#include <iostream>
using namespace std;
int main()
{
char name[10];
int age;
cout<<”Enter your first name and age:”<<endl;
cin>> name>>age;
cout<<”You entered: “<<name<<” “<<age<<endl;
}
Output:
Figure 4.66: Output of the program
288
for Advanced Secondary Schools
Computer Science Form 5.indd 288 23/07/2024 12:34

