Tuesday 24 June 2014

C++ program to insert character into the string at the given location


Here is a C++ program to insert character into the string at the given location


 SOURCE CODE OUTPUT
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    char str[100],temp;
    int lenght,n;
    cout<<"Enter string : ";
    cin>>str;
    cout<<"Enter character : ";
    cin>>temp;
    cout<<"Enter the value of n : ";
    cin>>n;
    lenght=strlen(str);
    for(int i=0;str[i]!='\0';i++)
    {
        if(i==n-1)
        {
            for(int y=lenght;y>=i;y--)
                str[y+1]=str[y];
            str[i]=temp;
            break;
        }
    }
    cout<<"After inserting character at "<<n<<"th : "<<str;
    return 0;
}

Any questions regarding to program please write in comments.

No comments:

Post a Comment