Extracting substring....

Using substr and concatenation, give a program containing a sequence of commands that will extract characters from input_string = "Four score and seven years ago our fathers"  to make output_string = "carefree and no tears". Then print output_string.


Here is my code:

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

int main()
{
string input_string = "Four score and seven years ago our fathers";
string output_string = input_string.substr(6, 1), (11, 1), (3,1), (9,1), (4,1), (11, 3), (4,1), (12,1), (1,1), (4,1), (37,1), (9,1), (11,1), (3,1), (5,1);
cout << input_string << output_string;
}


I am receiving these errors but not sure what it means:

8 52 expected ')' before numeric constant
8 52 expected unqualified-id before numeric constant

Help plz?
Last edited on
This line has incorrect syntax:
string output_string = input_string.substr(6, 1), (11, 1), (3,1), (9,1), (4,1), (11, 3), (4,1), (12,1), (1,1), (4,1), (37,1), (9,1), (11,1), (3,1), (5,1);

The first part is valid:
string output_string = input_string.substr(6, 1);

The rest of the line doesn't make any sense.


(by the way, you should also have #include <string> at the start of the program).
to extract "carefree and no tears" in my output stream using substr and concatenation, what would I do? What i was attempting to do (although it doesn't make sense in my code) is extract the column, and the number of letters in "Four score and seven years ago our fathers" to create "carefree and no tears".

How can I do this?
You need to build upon what you have, using the concatenation operator'+'.
Simple example of concatenation:
1
2
3
4
5
6
7
8
9
string one = "mess";
string two = "age";
string three = " in a ";
string four = "bottle";

one = one + two;
one += three + four;

cout << one << endl;

Output:
message in a bottle

Lines 6 and 7 above do more or less the similar things, but the += operator is more concise.

You need to use this concatenation operator multiple times with individual substrings such as input_string.substr(11, 1) or input_string.substr(22, 4) as required.

Actually, if you only need a single character, you could use input_string[11], but that may not be allowed in your project.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

int main()
{
	string input_string = "Four score and seven years ago our fathers";
	string output_string1 = input_string.substr(6, 1);
    string output_string2 = input_string.substr(11, 1);
    string output_string3 = input_string.substr(3,1);
    string output_string4 = input_string.substr(9,1);
    string output_string5 = input_string.substr(0,1);
    string output_string6 = input_string.substr(3, 1);
    string output_string7 = input_string.substr(9,1);
    string output_string8 = input_string.substr(9,1);
    string output_string9 = input_string.substr(4,1);
    string output_string10 = input_string.substr(11,3);
    string output_string11 = input_string.substr(4,1);
    string output_string12 = input_string.substr(12,1);
    string output_string13 = input_string.substr(1,1);
    string output_string14 = input_string.substr(4,1);
    string output_string15 = input_string.substr(37,1);
    string output_string16 = input_string.substr(9,1);
    string output_string17 = input_string.substr(11,1);
    string output_string18 = input_string.substr(3,1);
    string output_string19 = input_string.substr(5,1);
    
    
    // Now concatenate strings to make "carefree and no tears" //
    
    output_string1 =  output_string1 + output_string2 + output_string3 + output_string4 + output_string5
        + output_string6 + output_string7 + output_string8;
    output_string2 = output_string9 + output_string10 + output_string11;
    output_string3 = output_string12 + output_string13 +
        output_string14;
    output_string4 = output_string15 + output_string16 + output_string17 + output_string18 + output_string19;
    output_string1 += output_string2 + output_string3 + output_string4;
    
    

    cout << input_string<< endl;
    cout << output_string1 << endl;

}




With output :


Four score and seven years ago our fathers
careFree and no tears


any better way to do it?
Well, there's certainly no need for 20 strings.

1
2
3
4
5
    string input_string = "Four score and seven years ago our fathers";
    string output_string ;
    output_string += input_string.substr(6, 1);
    output_string += input_string.substr(11, 1);
    // ... 


You may notice that you're doing the same thing over and over here. I wonder if there is a way to make a loop out of this?
Topic archived. No new replies allowed.