turn string 90 degrees

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

// pad with spaces at the end to make all strings of the same size
void make_recatanglar( std::vector<std::string>& vec )
{
    std::size_t max_sz = 0 ;
    for( const std::string& str : vec ) if( max_sz < str.size() ) max_sz = str.size() ;
    for( std::string& str : vec ) str.resize( max_sz, ' ' ) ;
}

// invariant: valid col
void print_col( const std::vector<std::string>& vec, std::size_t col )
{
    for( const std::string& str : vec ) std::cout << str[col] ;
    std::cout << '\n' ;
}

void print_vertical( std::vector<std::string> vec )
{
    if( !vec.empty() )
    {
        make_recatanglar(vec) ;
        const auto ncols = vec.front().size() ;
        for( std::size_t col = 0 ; col < ncols ; ++col ) print_col( vec, col ) ;
    }
}

int main()
{
    std::size_t n ;
    std::cout << "enter number of strings: " ;
    std::cin >> n ;
    std::cin.ignore( 10000, '\n' ) ; // throw the new line away

    std::vector<std::string> lines ;
    std::string str ;
    while( lines.size() < n &&
           std::cout << "line #" << lines.size()+1 << "? " &&
           std::getline( std::cin, str ) ) { lines.push_back(str) ; }

    print_vertical( std::move(lines) ) ;
}
JLBorges thanks, everything works but there is a lot I can't use my code :D I'll try to write my code using this ))

how to write this differently ?

for( const std::string& str : vec )

and how to write so as not to use std::cin.ignore
Last edited on
> how to write this differently ? for( const std::string& str : vec )

1
2
3
4
5
for( std::size_t i = 0 ; i < vec.size() ; ++i )
{
    const std::string& str = vec[i] ;
    // ..
}



> and how to write so as not to use std::cin.ignore

1
2
// std::cin.ignore( 10000, '\n' ) ; // throw the new line away
{ std::string discard_this ; std::getline( std::cin, discard_this ) ; } // throw the new line away 
thanks for everything :D
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
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
   vector<string> matrix;
   string line;
   int maxLineLength = 0;

   cout << "Enter some lines (blank line to end)\n";
   while ( getline( cin, line ) && line != "" ) 
   {
      matrix.push_back( line );
      if ( maxLineLength < line.size() ) maxLineLength = line.size();
   }
   
   for ( int i = 0; i < maxLineLength; i++ )
   {
      for ( int j = 0; j < matrix.size(); j++ ) cout << ( i < matrix[j].size() ? matrix[j][i] : ' ' );
      cout << endl;
   }
}



With apologies to Bob Dylan:
Enter some lines (blank line to end)
How many roads must a man walk down
Before you call him a man
How many seas must a white dove sail
Before she sleeps in the sand
Yes, 'n' how many times must the cannon balls fly
Before they're forever banned
The answer, my friend, is blowin' in the wind
The answer is blowin' in the wind

HBHBYBTT
oeoeeehh
wfwfsfee
 o o,o  
mrmr raa
aeae'enn
n n n ss
yyys'tww
 o h hee
ruseherr
o e oy, 
acasw' i
dasl rms
sl emey 
 lmea  b
m upnffl
uhssyoro
sit  riw
tm iteei
  anivnn
aa  med'
  wter, 
mmhhs  i
aaie bin
nnt mas 
  esun t
w  asnbh
a dntele
l od do 
k v t ww
  e h ii
d   e nn
o s   'd
w a c   
n i a i 
  l n n 
    n   
    o t 
    n h 
      e 
    b   
    a w 
    l i 
    l n 
    s d 
        
    f   
    l   
    y   
Nice, a lot of different tricks in this thread. I was hung up on the word matrix, assuming he had something like char** or vector<vector<char>> or vector<string> etc ...
Topic archived. No new replies allowed.
Pages: 12