0% found this document useful (0 votes)
39 views3 pages

String Sheet

Uploaded by

ahmedshawku5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views3 pages

String Sheet

Uploaded by

ahmedshawku5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

String sheet

1) Write a program to read the string from the user and print its length, then compare
it with text “SoftwareTestingHelp”, then add .com to the text “SoftwareTestingHelp”,
finally print length of text as shown in the following scenario:

Input the string: test sentence

String entered is: test sentence

Length of the string str is: 13

Two strings are not equal

New str1 after adding .com: SoftwareTestingHelp.com

str new length: 23

Solution:

#include <iostream>

#include <string>

using namespace std;

int main() {

string str;

string target = "SoftwareTestingHelp";

// Input the string

cout << "Input the string: ";

getline(cin, str);
// Print the entered string

cout << "String entered is: " << str << endl;

// Calculate and print the length of the string

cout << "Length of the string str is: " << str.length() << endl;

// Compare with target string

if (str == target) {

cout << "Two strings are equal" << endl;

else {

cout << "Two strings are not equal" << endl;

// Append ".com" to target string and print the new string and its length

string str1 = target + ".com";

cout << "New str1 after adding .com: " << str1 << endl;

cout << "str1 new length: " << str1.length() << endl;

return 0;

}
2) What is the output of the following code:

#include <iostream>
using namespace std;
int main() {
char *ptr,*ptr2,*ptr3;
char Str[] = "Have a nice day";
ptr = Str;
ptr += 7;
cout << ptr<<endl;
ptr2 = ptr+1;
cout << ptr2;
return 0;
}

Answer:
nice day
ice day

You might also like