Rotating Sentences

Write your question here.
INPUT / OUTPUT
As input to my program, I will be given a maximum of 15 sentences, each not
exceeding 100 characters long. Legal characters include: newline, space, any
punctuation characters, digits, and lower case or upper case English letters. The input
file starts with an integer indicating the number of lines that the input file contains.
The output of the program should have the last sentence printed out vertically in the
leftmost column; the first sentence of the input would subsequently end up at the
rightmost column.
Sample Input (rotate.txt)
2
Rene Decartes once said,
"I think, therefore I am."
Sample Output
"R
Ie
n
te
h
iD
ne
kc
,a
r
tt
he
es
r
eo
fn
oc
re
e
s
Ia
i
ad
m,
.
"
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

here's m code can someone help me deal with these?
coz it still prints the case number for my program 
#include<iostream>
#include<string.h>
#include<fstream>

using namespace std;
int main()
{
	freopen("Rotation.txt","r",stdin);

	int i,j,len,maxlen,k;
	char s[100][100];
 	i=0;
	maxlen=0;
	memset(s,0,sizeof(s));


	while (gets(s[i]))
{
	len=strlen(s[i]);
	if (len>maxlen) 
	maxlen=len;
	i++;
}
	for (j=0;j<maxlen;j++)
{
	for (k=i;k>=0;k--)
{
	if (s[k][j]==0)
		cout<<" ";
	else 
		cout<<s[k][j];
}
	cout<<"\n";
}
return 0;
} 
Last edited on
Hi, since you are using C++, why not using a std::vector<std::string>? Probably it would simplify things a bit...
This is a possible solution:

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
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    string s1 = "Rene Decartes once said, ";
    string s2 = "\"I think, therefore I am.\"";
    vector<string> vec;
    vec.push_back(s1);
    vec.push_back(s2);

    unsigned int max_length = 0;
    for(string s : vec) {
        if(s.length() > max_length)
            max_length = s.length();
    }

    for(unsigned int l=0; l<max_length; ++l) {
        for(int i=vec.size()-1; i >= 0; --i) {
            cout << vec[i][l];
        }
        cout << endl;
    }

    return 0;
}
I'm a newbie in c++ programming sir but thank you minomic, but sir my proffesor asked me to have an input from the file, and that means that the program would depend from the file red. thanks again minomic
Last edited on
Yes I know... I left that part to you and showed just how to print the strings. So you have to fill the vector of strings (reading from the file), then you can use my code to print.
Topic archived. No new replies allowed.