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