33

问答题 33 /85

对于一个任意输入的少于200字节的字符串,把它按‘-’分成若干子串(子串最多20个),输出原始字符串和每个子串,要求能依次处理用户输入的多个字符串,直到用户输入字符串“0”时程序退出。

参考答案

#include <iostream>
using namespace std;
#include <string>

void main()
{
 char	str_s[200];
 char	*ptr	= str_s;
 char	*pt	= str_s;
 int	lenth;
 cout << "please input the source string you want to deal with:" << endl;
 while ( true )
 {
 gets( str_s );
 lenth = strlen( str_s );
 char	*ptr	= str_s;
 char	*pt	= str_s;
 if ( str_s[0] == '0' )
 {
     break;
 }
 int flag = 0, length = 0;
 if ( lenth < 200 )
 {
     while ( *ptr != '\0' && length <= lenth )
     {
         cout << *ptr;
         ptr++;
         length++;
     }
     cout << endl;
     while ( *pt != '\0' && flag <= 20 )
     {
         if ( *pt != '-' )
         {
             cout << *pt;
             pt++;
         }else  {
             flag++;
             pt++;
             cout << endl;
         }
     }
     cout << endl;
 }
 cout << "please input the source string you want to deal with:" << endl;
 }
}