Skip to content

Instantly share code, notes, and snippets.

@Parassharmaa
Last active November 26, 2016 12:18
Show Gist options
  • Select an option

  • Save Parassharmaa/92da85ae782f5e4965137ae0d3571490 to your computer and use it in GitHub Desktop.

Select an option

Save Parassharmaa/92da85ae782f5e4965137ae0d3571490 to your computer and use it in GitHub Desktop.
Cpp program to divide the string in equal parts and sort out the substrings
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
class str_op {
string s;
public:
str_op(string str) {
s = str;
}
str_op() {
string st;
cout<<"Input String: ";
cin>>st;
s = st;
}
vector<string> str_fun();
void str_print(vector<string>);
};
int s_div(int nn) {
int s=1;
for (int i=2;i<nn;i++) {
if(nn%i==0) {
s=i;
break;
}
}
return s;
}
int main() {
str_op s;
vector<string> str_arr = s.str_fun();
s.str_print(str_arr);
return 0;
}
vector<string> str_op::str_fun() {
int n = s.length();
int mult = s_div(n);
vector<string> s_arr;
for (int i=0;i<n;i+=mult) {
s_arr.push_back(s.substr(i,mult));
}
sort(s_arr.begin(), s_arr.end());
return s_arr;
}
void str_op::str_print(vector<string> v) {
for(vector<string>::const_iterator i = v.begin();i!=v.end();++i) {
cout<<*i<<endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment