按要求分解字符串,输入两个数M,N;M代表输入的M串字符串,N代表输出的每串字符串的位数,不够补0。例如:输入2,8, “abc” ,“123456789”,则输出为“abc00000”,“12345678“,”90000000”
#include<stdio.h>
#include<string>
#include<vector>
#include<iostream>
using namespace std;
vector<string> function( const char* InPutStr,int N){
vector<string> OutPut_V;
int n=strlen(InPutStr)/N;
int y=N-strlen(InPutStr)%N;
char *addZero=new char[N+1];
for(int i=0,count=0;i<strlen(InPutStr);++i){
if(count<n){
char* temp = new char[N+1];
for(int j=i,k=0;k<N;++j,++k)
temp[j-count*N]=InPutStr[j];
temp[N]='\0';
OutPut_V.push_back(temp);
count++;
i+=N-1;
}
else if(count==n){
addZero[i-count*N]=InPutStr[i];
}
}
for(int i=N-1;i>=N-y;--i)
addZero[i]='0';
addZero[N]='\0';
OutPut_V.push_back(addZero);
return OutPut_V;
}
int main(){
string InPutStr[]={"acxsajdhgdkfg","sdfjk"};
int k=0;
while(k<2){
vector<string> out = function(InPutStr[k].c_str(),8);
for(int i=0;i<out.size();++i)
cout<<out[i]<<endl;
++k;
}
return 0;
}
|