Anyone knows...??!!

So basically I was new in c++
Anyone know how to make string we input become like this:
Ex: input: abc def ghi
The output is:
adg
beh
cfi

Like that... can someone help me with what function or what can be like that.
Thx~
You could do that in a multitude of ways, the seemingly simplest may be something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
using namespace std;

int main()
{
        cout << "Enter adg beh cfi: ";
	string p1, p2, p3; //p meaning "part"
	cin >> p1 >> p2 >> p3;
	string concatenate = p1 + '\n' + p2 + '\n' + p3;
	cout << concatenate << endl;
	return 0;
}


Of course, if you just wanted it in function form, there are again other methods, including the above and passing p1, p2, p3 and returning it to a new string.

Hope that helps.
@Yawzheek
Read the task carefully. This task is not to concatenate three strings.
Read the task carefully. This task is not to concatenate three strings.


First, it's better than your original post (which you've been advised against) reading, "Send me a PM."

Second, the task isn't outlined well at all, so there's absolutely nothing wrong with my solution. Is it supposed to be one string split three times? How many characters per split? Are there certain characters that should initiate a newline? Is it three strings with formatting? Are we starting with these as literals, or are we accepting input?

The instructions aren't clear enough to make a definitive statement as to what is and isn't allowed, and as a result, I'm free to interpret it in any way that appears to solve the problem.

Going back to the above: THAT'S why you were told not to solicit through private messages; maybe you have some insight the rest of us would like to see.
The instructions aren't clear enough to make a definitive statement as to what is and isn't allowed, and as a result, I'm free to interpret it in any way that appears to solve the problem.

No, it is more than clear. If you have any problems about it, go back to study C++ for a few more months until you understand this simple homework assignment.
Well since you seem to be going to great lengths to be less than useless by providing criticism whilst offering no actual solutions, OP, if you require one string input, and we're assuming you need newlines where spaces are present and nothing more, you can format it into another string:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
	string input;
	getline(cin, input);
	string formatted;

	for (size_t i = 0; i != input.size(); ++i) {
		if (isspace(input[i])) {
			formatted += '\n';
			continue;
		}
		formatted += input[i];
	}

	cout << formatted << endl;

	return 0;
}


That's assuming you want to use that string again later and it must come in as a single string.

If that's not important, and all you need is output, you could try:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
	string input;
	getline(cin, input);
	
	for (size_t i = 0; i != input.size(); ++i) {
		if (isspace(input[i])) {
			cout << endl;
			continue;
		}
		cout << input[i];
	}
	cout << endl;

	return 0;
}


I'm not a fan of continue statements, but they get the job done, if nothing else. If you want to pass the string to functions, you can pass the first with your input string and a target string reference, and for the second, you can just pass the input string.
Please test your code first before posting or you will waste your efforts.

Here is the output I got :
abc def ghi
abc
def
ghi


http://cpp.sh/4is5u
SakurasouBusters is absolutely right, Yawzheek. You need to read more carefully. OP's question is clear enough.

"123 456 789"
=== to matrix form ==>
123
456
789
=== transpose ==>
147
258
369
=== print ===>
"147
258
369"
SakurasouBusters sry I was post this on my phone cause I was on the way home but my output 100% same like u but the coding is different...

hellos,Yawzheek yeah hellos is correct I want the output is like that but I still lack of skill to do that. Btw thx for helping Yawzheek
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s1, s2, s3;
    cout << "Enter three strings (three characters for each string) : " << endl;
    cin >> s1 >> s2 >> s3;

    string result;
    result = result + s1[0] + s2[0] + s3[0] + '\n';
    result = result + s1[1] + s2[1] + s3[1] + '\n';
    result = result + s1[2] + s2[2] + s3[2];

    cout << "The result string : " << endl;
    cout << result << endl;
    
    return 0;
}
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
#include <iostream>
#include <string>
#include <vector>

int main( )
{
    std::cout << "input: ";
    
    std::size_t length{};
    std::vector<std::string> strings{};
    for( std::string input; std::cin >> input; ) {
        /*
        the first string entered determines the length of
        the rest of the strings
        */
        if( strings.empty( ) ) length = input.length( );
        
        if( input.length( ) != length ) break;
        
        strings.push_back( input );
    }
    
    std::cout << "\n" "output: \n";
    
    // outer loop is for each of the characters in user input
    for( std::size_t i{}; i < length; i++ ) {
        // the inner loop is for each element(user input) in the vector
        for( std::size_t j{}; j < strings.size( ); j++ ) {
            /*
            we want to loop through all the elements(j) and print
            the ith character
            */
            std::cout << strings.at( j ).at( i );
        }
        
        std::cout << "\n";
    }
}


input: abcd efgh ijkl a

output: 
aei
bfj
cgk
dhl
 
Last edited on
Topic archived. No new replies allowed.