Answer to Question #239113 in C++ for JacksonCrews

Question #239113

Given a string, an integer position, and an integer length, all on separate lines, output the substring from that position of that length.

Ex: If the input is:

Fuzzy bear

3

4

the output is:

zy b

Note: Using a pre-defined string function, the solution can be just one line of code.

#include <iostream>

#include <string>

using namespace std;


int main() {

string strVal;

int posStart;

int choiceLen;

string newString;


getline(cin, strVal);

cin >> posStart;

cin >> choiceLen;


cout << newString << endl;

return 0;

}


1
Expert's answer
2021-09-19T02:54:11-0400
#include <iostream>
#include <string>
using namespace std;


int main()
{
	string strVal;
	int posStart;
	int choiceLen;
	string newString;
	getline(cin, strVal);
	cin >> posStart;
	cin >> choiceLen;
	// Using a pre-defined string function substr(m, n)
	newString = strVal.substr(posStart, choiceLen);
	cout << newString << endl;
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS