字符串截断输出

来源:互联网 发布:阿里云os recovery 编辑:程序博客网 时间:2024/06/05 21:09
/********************************************************************created:2014/11/28   13:00:00filename: E:\VC\Projects\HDU_0001\HDU_0001\string_cut.cppfile path:E:\VC\Projects\HDU_0001\HDU_0001file base:HDU_0001file ext:cppauthor:angiaoke*********************************************************************/#include<iostream>#include <string>using namespace std;void string_cut(string a, int m, int n) {int i = 0, j = 0;string *b = new string[m];    int t = m;while(t)                            {cin >> a;                                      //输入字符串b[i++] = a;                                  //将字符串存入数组b中t--;}cout << "the result is: " << endl;for(i = 0; i < m; i++)        {    int k = b[i].size();                       //k等于字符串长度  for(j = 0; j < k; j++)  {  if(j!=0 && j % n == 0)         //当j是n的倍数时换行{  cout<<endl;          }  cout<<b[i][j];                       //输出字符串中的字符         }  if(k % n != 0)                            //当n不能被k整除时,补0for(j = 0; j<n-k%n; j++)  cout << '0';  cout << endl;  }  }int main () {int m = 0, n = 0;string a;cout << "Please input m and n: ";cin >> m >> n;                               //分别代表行数和长度,要求输入m行,并将字符串截断成n长。if(m <= 0 || n <=0)                        //如果m或n小于等于0,就再次输入{cout << "m, n are invalid, please input again:";cin >> m >> n;}string_cut(a,m, n);system("pause");return 0;}

0 0