Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This lab is going to require that you do some research. We have been using basic

ID: 3702431 • Letter: T

Question

This lab is going to require that you do some research. We have been using basic features of std::string for several weeks. You're going to teach yourself how to use some of the more advanced features. Write a program that demonstrates the use of these class methods:

length
clear
at
assign
insert
replace
find
substr

Hint: you will find everything you need, including examples, at www.cplusplus.com.

IF all you had to do was demonstrate the use of rfind, your program might look something like this:

Checklist

1. Project/solution named correctly
2. Correct comments at top
3. Consistent indentation (Use Edit/Advanced/Format Document)
4. Good variable names
5. Overall neat organization
6. Comments in code explain what's being done
7. #include "stdafx.h" in cpp files (or, suppress pch)

make a visual studio program:

#include "stdafx.h"

#include <iostream>

#include <string>

using namespace std;

Explanation / Answer

#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

// Main menu to selection operations
int menu()
{
// To store choice
int choice;
// Displays options
cout<<" 1. Length of String. ";
cout<<" 2. Clear String. ";
cout<<" 3. Char at index position. ";
cout<<" 4. Assign String. ";
cout<<" 5. Insert String. ";
cout<<" 6. Replace String. ";
cout<<" 7. Find String. ";
cout<<" 8. Find Sub String. ";
cout<<" 9. Exit. ";
// Accepts choice
cout<<" Enter your choice: ";
cin>>choice;
// Return choice
return choice;
}// End of function

// Function to display Assignment menu and returns choice
int subMenuAssign()
{
// To store choice
int choice;
// Displays options
cout<<" 1 Assign sub string from a position to number of characters.";
cout<<" 2 Assign number of characters from a string constant.";
cout<<" 3 Assign a character number of times.";
// Accepts choice
cout<<" Enter your choice: ";
cin>>choice;
// Return choice
return choice;
}// End of function

// Function to display insert menu and returns choice
int subMenuInsert()
{
// To store choice
int choice;
// Displays options
cout<<" 1 String to insert from specified position.";
cout<<" 2 String to insert from specified position, starting index position, number of characters.";
cout<<" 3 Character to insert from specified position, number of times.";
// Accepts choice
cout<<" Enter your choice: ";
cin>>choice;
// Return choice
return choice;
}// End of function

// main function definition
int main()
{
std::string stringData;
string temp;
int from, to, start, found = -1;
char ch;
// Loops till user choice is not 9
do
{
// Calls the main menu and does the operation as per the return choice
switch(menu())
{
// Display length
case 1:
// Clears standard input
fflush(stdin);
cout<<" Enter a string data: ";
std::getline (std::cin, stringData);
cout<<" Length of the string: "<<stringData.length();
break;
case 2:
// Clears string
stringData.clear();
cout<<" After clearing the string: "<<stringData;
break;
case 3:
// Clears standard input
fflush(stdin);
cout<<" Enter a string data: ";
std::getline (std::cin, stringData);
cout<<" Enter the index position to see the character: ";
cin>>from;
cout<<" Character "<<stringData.at(from)<<" at index position "<<from;
break;
case 4:
// Calls the function to select insert option
switch(subMenuAssign())
{
case 1:
// Clears standard input
fflush(stdin);
cout<<" Enter a string data: ";
std::getline (std::cin, stringData);
cout<<" Enter the from index position: ";
cin>>from;
cout<<" Enter number of characters to assign: ";
cin>>to;
temp.assign(stringData, from, to);
cout<<" Result String: "<<temp;
// Clears standard input
temp.clear();
break;
case 2:
cout<<" Given Constant: Check this replacement.";
cout<<" Enter number of characters to assign: ";
cin>>to;
temp.assign("Check this replacement.", to);
cout<<" Result String: "<<temp;
// Clears standard input
temp.clear();
break;
case 3:
// Clears standard input
fflush(stdin);
cout<<" Enter a string data: ";
std::getline (std::cin, stringData);
cout<<" Enter number of characters to assign: ";
cin>>to;
cout<<" Enter the character to assign: ";
cin>>ch;
temp.assign(to, ch);
cout<<" Result String: "<<temp;
// Clears standard input
temp.clear();
break;
default:
cout<<" Invalid choice.";
break;
}// End of switch case
break;
case 5:
// Calls the function to select insert option
switch(subMenuInsert())
{
case 1:
// Clears standard input
fflush(stdin);
cout<<" Enter a string data: ";
std::getline (std::cin, stringData);
cout<<" Enter the string to insert: ";
std::getline(std::cin, temp);
cout<<" Enter the position: ";
cin>>from;
stringData.insert(from, temp);
cout<<" After insert: "<<stringData;
// Clears standard stringData
stringData.clear();
break;
case 2:
// Clears standard input
fflush(stdin);
cout<<" Enter a string data: ";
std::getline (std::cin, stringData);
cout<<" Enter the string to insert: ";
std::getline(std::cin, temp);
cout<<" Enter the from position: ";
cin>>from;
cout<<" Enter the starting index position: ";
cin>>start;
cout<<" Enter number of characters: ";
cin>>to;
stringData.insert(from, temp, start, to);
cout<<" After insert: "<<stringData;
// Clears standard stringData
stringData.clear();
break;
case 3:
// Clears standard input
fflush(stdin);
cout<<" Enter a string data: ";
std::getline (std::cin, stringData);
cout<<" Enter the character to insert: ";
cin>>ch;
cout<<" Enter from specified position: ";
cin>>from;
cout<<" Enter number of times: ";
cin>>to;
stringData.insert(from, to, ch);
cout<<" After insert: "<<stringData;
// Clears standard stringData
stringData.clear();
break;
default:
cout<<" Invalid choice!";
break;
}// End of switch case
break;
case 6:
// Clears standard input
fflush(stdin);
cout<<" Enter a string data: ";
std::getline (std::cin, stringData);
cout<<" Enter the insert position: ";
cin>>from;
cout<<" Enter the number of characters to replace: ";
cin>>to;
// Clears standard input
fflush(stdin);
cout<<" Enter the string to replace: ";
std::getline(std::cin, temp);
stringData.replace(from, to, temp);
cout<<" After replacement: "<<stringData;
// Clears standard stringData
stringData.clear();
break;
case 7:
// Clears standard input
fflush(stdin);
cout<<" Enter a string data: ";
std::getline (std::cin, stringData);
cout<<" Enter the string to find: ";
std::getline(std::cin, temp);
found = stringData.find(temp);
if (found != -1)
cout<<" String "<<temp<<" found "<<" at "<<found<<" index position.";
else
cout<<" String "<<temp<<" not found.";
break;
case 8:
// Clears standard input
fflush(stdin);
cout<<" Enter a string data: ";
std::getline (std::cin, stringData);
cout<<" Enter the starting index position: ";
cin>>from;
cout<<" Enter number of characters: ";
cin>>to;
cout<<stringData.substr(from, to);
break;
case 9:
exit(0);
default:
cout<<" Invalid choice!";
}// End of outer switch case
}while(1); // End of do while loop
return 0;
}// End of main function

Sample Output:

1. Length of String.
2. Clear String.
3. Char at index position.
4. Assign String.
5. Insert String.
6. Replace String.
7. Find String.
8. Find Sub String.
9. Exit.
Enter your choice: 1

Enter a string data: Check is data.

Length of the string: 14
1. Length of String.
2. Clear String.
3. Char at index position.
4. Assign String.
5. Insert String.
6. Replace String.
7. Find String.
8. Find Sub String.
9. Exit.
Enter your choice: 2

After clearing the string:
1. Length of String.
2. Clear String.
3. Char at index position.
4. Assign String.
5. Insert String.
6. Replace String.
7. Find String.
8. Find Sub String.
9. Exit.
Enter your choice: 3

Enter a string data: Check is data.

Enter the index position to see the character: 5

Character at index position 5
1. Length of String.
2. Clear String.
3. Char at index position.
4. Assign String.
5. Insert String.
6. Replace String.
7. Find String.
8. Find Sub String.
9. Exit.
Enter your choice: 3

Enter a string data: Check is data.

Enter the index position to see the character: 2

Character e at index position 2
1. Length of String.
2. Clear String.
3. Char at index position.
4. Assign String.
5. Insert String.
6. Replace String.
7. Find String.
8. Find Sub String.
9. Exit.
Enter your choice: 4

1 Assign sub string from a position to number of characters.
2 Assign number of characters from a string constant.
3 Assign a character number of times.
Enter your choice: 1

Enter a string data: Check is data.

Enter the from index position: 2

Enter number of characters to assign: 3

Result String: eck
1. Length of String.
2. Clear String.
3. Char at index position.
4. Assign String.
5. Insert String.
6. Replace String.
7. Find String.
8. Find Sub String.
9. Exit.
Enter your choice: 4

1 Assign sub string from a position to number of characters.
2 Assign number of characters from a string constant.
3 Assign a character number of times.
Enter your choice: 2

Given Constant: Check this replacement.
Enter number of characters to assign: 5

Result String: Check
1. Length of String.
2. Clear String.
3. Char at index position.
4. Assign String.
5. Insert String.
6. Replace String.
7. Find String.
8. Find Sub String.
9. Exit.
Enter your choice: 4

1 Assign sub string from a position to number of characters.
2 Assign number of characters from a string constant.
3 Assign a character number of times.
Enter your choice: 3

Enter a string data: Check is data.

Enter number of characters to assign: 5

Enter the character to assign: *

Result String: *****
1. Length of String.
2. Clear String.
3. Char at index position.
4. Assign String.
5. Insert String.
6. Replace String.
7. Find String.
8. Find Sub String.
9. Exit.
Enter your choice: 5

1 String to insert from specified position.
2 String to insert from specified position, starting index position, number of characters.
3 Character to insert from specified position, number of times.
Enter your choice: 1

Enter a string data: This is a demo.

Enter the string to insert: ok

Enter the position: 5

After insert: This okis a demo.
1. Length of String.
2. Clear String.
3. Char at index position.
4. Assign String.
5. Insert String.
6. Replace String.
7. Find String.
8. Find Sub String.
9. Exit.
Enter your choice: 5

1 String to insert from specified position.
2 String to insert from specified position, starting index position, number of characters.
3 Character to insert from specified position, number of times.
Enter your choice: 2

Enter a string data: This is a demo.

Enter the string to insert: try it

Enter the from position: 1

Enter the starting index position: 2

Enter number of characters: 3

After insert: Ty ihis is a demo.
1. Length of String.
2. Clear String.
3. Char at index position.
4. Assign String.
5. Insert String.
6. Replace String.
7. Find String.
8. Find Sub String.
9. Exit.
Enter your choice: 5

1 String to insert from specified position.
2 String to insert from specified position, starting index position, number of characters.
3 Character to insert from specified position, number of times.
Enter your choice: 3

Enter a string data: This is a demo.

Enter the character to insert: $

Enter from specified position: 2

Enter number of times: 4

After insert: Th$$$$is is a demo.
1. Length of String.
2. Clear String.
3. Char at index position.
4. Assign String.
5. Insert String.
6. Replace String.
7. Find String.
8. Find Sub String.
9. Exit.
Enter your choice: 6

Enter a string data: This is a demo.

Enter the insert position: 2

Enter the number of characters to replace: 3

Enter the string to replace: ok

After replacement: Thokis a demo.
1. Length of String.
2. Clear String.
3. Char at index position.
4. Assign String.
5. Insert String.
6. Replace String.
7. Find String.
8. Find Sub String.
9. Exit.
Enter your choice: 7

Enter a string data: This is a demo.

Enter the string to find: demo

String demo found at 10 index position.
1. Length of String.
2. Clear String.
3. Char at index position.
4. Assign String.
5. Insert String.
6. Replace String.
7. Find String.
8. Find Sub String.
9. Exit.
Enter your choice: 7

Enter a string data: This is a demo.

Enter the string to find: de

String de found at 10 index position.
1. Length of String.
2. Clear String.
3. Char at index position.
4. Assign String.
5. Insert String.
6. Replace String.
7. Find String.
8. Find Sub String.
9. Exit.
Enter your choice: 7

Enter a string data: This is a demo.

Enter the string to find: is

String is found at 2 index position.
1. Length of String.
2. Clear String.
3. Char at index position.
4. Assign String.
5. Insert String.
6. Replace String.
7. Find String.
8. Find Sub String.
9. Exit.
Enter your choice: 8

Enter a string data: This is a demo.

Enter the starting index position: 2

Enter number of characters: 5
is is
1. Length of String.
2. Clear String.
3. Char at index position.
4. Assign String.
5. Insert String.
6. Replace String.
7. Find String.
8. Find Sub String.
9. Exit.
Enter your choice: 8

Enter a string data: This is a demo.

Enter the starting index position: 5

Enter number of characters: 3
is
1. Length of String.
2. Clear String.
3. Char at index position.
4. Assign String.
5. Insert String.
6. Replace String.
7. Find String.
8. Find Sub String.
9. Exit.
Enter your choice: 9

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote