This repository has been archived by the owner on May 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretty-printing.cpp
175 lines (164 loc) · 4.2 KB
/
pretty-printing.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
void prettyprinting(string str1, int l)
{
string str;
int countw = 1;
vector <string> v1;
for(int i= 0; i<str1.length();i++)
{
if(str1[i]==' ')
{
v1.push_back(str);
str = "";
countw+=1; // countw has number of words in the line.
}
else
{
str.append(str1.begin()+i,str1.begin()+i+1);
}
}
v1.push_back(str);
str="";
vector<string>::iterator iter;
int countl = 0;
vector<string> v2;
vector<string>::iterator iter2;
vector<string>::iterator iter3;
int nwords = 0;
iter = v1.begin();
while(iter!=v1.end())
{
//cout<<*(iter)<<"\n"; // Printing for testing :)
if(countl + (*iter).length()<= l)
{
v2.push_back(*(iter));
countl = countl + (*iter).length();
iter++;
countl +=1;
nwords +=1;
if(iter == v1.end())
{
cout<<"\"",
cout<<*(v2.end()-1);
for(int i=(*(v2.end()-1)).length(); i<l;i++)
{
cout<<" ";
}
cout<<"\"";
}
}
else
{
if(nwords>1)
{
int spaceavail = l - countl + nwords;
int quo = spaceavail / (nwords-1);
int rem = spaceavail % (nwords-1);
if(rem!=0)
{
int temp = rem;
cout<<"\"";
for(iter2 = v2.begin();iter2!=v2.end();iter2++)
{
cout<<*(iter2);
iter3 = iter2+1;
if(iter3!=v2.end())
{
for(int i=0;i<quo;i++ )
{
cout<<" ";
}
}
if(temp>0)
{
cout<<" ";
temp = temp - 1;
}
}
v2.erase(v2.begin(),v2.end());
cout<<"\""<<", ";
countl = 0;
nwords = 0;
}
else
{
cout<<"\"";
for(iter2 = v2.begin();iter2!=v2.end();iter2++)
{
cout<<*(iter2);
iter3 = iter2+1;
if(iter3!=v2.end())
{
for(int i=0;i<quo;i++ )
{
cout<<" ";
}
}
}
v2.erase(v2.begin(),v2.end());
cout<<"\""<<", ";
countl = 0;
nwords = 0;
}
}
else
{
int spaceavail = l - countl + nwords;
int quo = spaceavail / (nwords);
cout<<"\"";
cout<<*(v2.begin());
for(int i= 0; i<quo;i++)
{
cout<<" ";
}
v2.erase(v2.begin(),v2.end());
cout<<"\""<<", ";
countl = 0;
nwords = 0;
}
}
}
}
int main()
{
vector<int> count1;
string s1;
getline(cin,s1);
int l;
scanf("%d",&l);
if(l > s1.length())
{
cout<<"\""<<s1<<" \"";
}
else if(l==s1.length())
{
cout<<"\""<<s1<<"\"";
}
else
{
prettyprinting(s1, l);
}
return 0;
}