#include <iostream>
#include <algorithm>
void insert_blank( char * line, std:: size_t line_size, std:: size_t pos)
{
std:: size_t index = 0 ;
while ( line[ index++ ] )
;
if ( index + 1 >= line_size || pos >= index) // sanity check
return ;
while ( index ! = pos)
{
line[ index] = line[ index - 1 ] ;
-- index;
}
line[ index] = ' ' ;
}
std:: istream & get( std:: istream & is, int & val)
{
// get a number and remove the trailing newline.
return ( is >> val) .ignore ( 100 , '\n ' ) ;
}
int main( )
{
const char * prompt = "Enter a string (or just hit <ENTER> to quit)\n > " ;
const std:: size_t buffer_size = 1024 ;
char line_buffer[ buffer_size] ;
while ( std:: cout << prompt && std:: cin .getline ( line_buffer, buffer_size- 1 ) && line_buffer[ 0 ] )
{
const char * prompt = "Enter the position to insert blank (or negative to quit)\n > " ;
int position;
while ( std:: cout << prompt && get( std:: cin , position) && position >= 0 )
{
insert_blank( line_buffer, buffer_size, position) ;
std:: cout << "modified: \" " << line_buffer << "\" \n " ;
}
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8YWxnb3JpdGhtPgoKCnZvaWQgaW5zZXJ0X2JsYW5rKGNoYXIqIGxpbmUsIHN0ZDo6c2l6ZV90IGxpbmVfc2l6ZSwgc3RkOjpzaXplX3QgcG9zKQp7CiAgICBzdGQ6OnNpemVfdCBpbmRleCA9IDA7CiAgICB3aGlsZSAobGluZVtpbmRleCsrXSkKICAgICAgICA7CgogICAgaWYgKGluZGV4ICsgMSA+PSBsaW5lX3NpemUgfHwgcG9zID49IGluZGV4KSAvLyBzYW5pdHkgY2hlY2sKICAgICAgICByZXR1cm47CgogICAgd2hpbGUgKGluZGV4ICE9IHBvcykKICAgIHsKICAgICAgICBsaW5lW2luZGV4XSA9IGxpbmVbaW5kZXggLSAxXTsKICAgICAgICAtLWluZGV4OwogICAgfQoKICAgIGxpbmVbaW5kZXhdID0gJyAnOwp9CgpzdGQ6OmlzdHJlYW0mIGdldChzdGQ6OmlzdHJlYW0mIGlzLCBpbnQmIHZhbC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