Tuesday 24 June 2014

C++ program to insert a word in a string


Here is a C++ program to insert a word in a string


 SOURCE CODE OUTPUT
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    char str[100],newstr[100],subst[20];
    int i,j=0,k=0,n,flag=0;
    cout<<"Enter String : ";
    cin.getline(str,100);
    cout<<"Enter string to be inserted : ";
    cin>>subst;
    cout<<"Enter the position : ";
    cin>>n;
    n++;
    for(int i=0;i<n-2;i++,k++)
        newstr[k]=str[i];
    for(int i=0;subst[i]!='\0';i++,k++)
        newstr[k]=subst[i];
        newstr[k++]=' ';
    for(int i=n-2;str[i]!='\0';i++,k++)
        newstr[k]=str[i];
        cout<<"New string : "<<newstr;
    return 0;
}

Any questions regarding to program please write in comments.

No comments:

Post a Comment