VJ_字符串加空格_string

编写一个函数,函数的参数是一个字符串指针,函数的功能完成在字符串中每隔一个字符插入一个空格。在主函数打印变化后的字符串.
注意:要在同一个字符串中操作,不要再定义一个字符串

Input

一个字符串指针

Output

变化后的字符串

Sample

Inputcopy Outputcopy
test
test ab
 test
t e s t
t e s t   a b
  t e s t

Hint

注意:要在同一个字符串中操作,不要再定义一个字符串
以子函数的形式给字符串加空格,子函数的形式为:
void intsert(char *p)

因为有多组测试数据,读取输入的时候建议采用如下形式:
char strin[40];
while(cin.getline(strin, 40, '\n'))
{
//你的代码
}

//
#include<bits/stdc++.h>
using namespace std;

int main()
{
    string s,ans;
    while( getline( cin,s ) )
    {
        ans.clear();
        for( int i=0;i<s.size();i++ )
        {
            ans+=s[i]; ans+=' ';
        }
        cout<<ans<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_63173957/article/details/125130268
今日推荐