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;
}
#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;
}
Comments
Leave a comment