Tuesday, December 5, 2017

Insert Character - CTS

Given a string (array of characters) insert a given character at the specified location (if the location is beyond string length, ignore)

Sample Input & Output:
Input : Hello 3 m
Output: Hemllo

Solution:
#include<iostream>
using namespace std;
int main()
{
string s;
char c;
int n;
cin>>s>>n>>c;
cout<<s.substr(0,n-1)+c+s.substr(n-1,s.size());
return 0;
}
Share: