Write short pieces of code that demonstrate how each of the

Write short pieces of code that demonstrate how each of the following functions work. Both the code and the output should clearly show what the functions do. For example, the following code could be used to demonstrate the strlen() function:

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char test_string[] = ``Test String\'\';
cout << ``The strlen() function returns \'\';
cout << ``the length of a string\'\'<< endl;
cout << ``E.g. the length of the string: *\'\';
cout << test_string << ``* is \'\' << strlen(test_string);
cout << ``.\'\' << endl;
return 0;
}

(The *\'s are put in to make it clear where the string begins and ends. Some people prefer the use of a vertical bar | to mark the begining and end of a string.)

Turn in your code and the output demonstrating the function\'s use.

I. C string functions:- Demonstrate the following functions from the cstring library that apply to character arrays:-

1. strlen (length)
2. strcpy (string copy)
3. strcat (string concatenation)
4. strcmp (string comparison)
5. strstr (substring)

II. C++ string operators:- Demonstrate the following functions from the string class library that apply to objects (variables) of type string:-

1. length

2. = (string copy)
3. + (string concatenation)
4. == (string comparison)
5. find

NOTE:-
You may finnd using a search engine useful to learn more about these functions/methods. The web sites (www.cplusplus.com) and (en.cppreference.com) both provide useful examples of many C++ topics.

Solution

#include <iostream>
#include <cstring>
#include <conio.h>
using namespace std;
int main()
{
char test_string[] = \"Test String\";
cout << \"The strlen() function returns \";
cout << \"the length of a string\"<< endl;
cout << \"E.g. the length of the string: *\"<<endl;
cout << test_string << \" * is \" << strlen(test_string)<<endl;
cout << \".\" << endl;
////////
char string1[] = \"This is sample string\";
char string2[] = \"This is another sample string\";
char str2[]=\"another\";
cout <<\"1. strlen (length):\\t\"<<strlen(string1)<<endl;
cout <<\"2. strcpy (string copy): \"<<strcpy(string1,string2)<<endl;
cout <<\"2. Now the string1 changed here: \"<<string1<<endl;

cout <<\"3. strcat (string concatenation):\\t\"<<strcat(string1,string2)<<endl;

cout <<\"4. strcmp (string comparison):\"<<strcmp(string1,string2)<<endl;
/*
if Return value < 0 then it indicates string1 is less than string2.

if Return value > 0 then it indicates string2 is less than string1.

if Return value = 0 then it indicates string1 is equal to string2.
*/
cout <<\"5. strstr (substring)\"<<strstr(string1,string2)<<endl;

// in c++

string string3 = \"This is sample string\";
string string4 = \"This is another sample string\";
string str5=\"another\";
cout<<endl<<\"in c++\"<<endl<<endl;
cout <<\"1. length--sizeof()\"<<sizeof(string3)<<endl;
cout <<\"2. = (string copy):\"<<strcpy(string3,string4)<<endl;
/*strcpy(string1, string2);
Copies string s2 into string s1*/
cout <<\"3. + (string concatenation):\"<<strcat(string3, string4)<<endl;
/*
strcat(string1, string2);
Concatenates string string2 onto the end of string string1.
*/
cout <<\"4. == (string comparison):\"<<strcmp(string3, string4)<<endl;
/* strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.*/
cout <<\"5. find:\"<<str5.find(str2)<<endl;
getch();
return 0;
}

Write short pieces of code that demonstrate how each of the following functions work. Both the code and the output should clearly show what the functions do. Fo
Write short pieces of code that demonstrate how each of the following functions work. Both the code and the output should clearly show what the functions do. Fo

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site