Subscribe

RSS Feed (xml)

Saturday, February 7, 2009

Write a C program to insert a sub-string in to given main string from a given position.

//Without using Functions.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(string.h)
void main( )
{
char st[20],sub[10],temp[10];
int pos, i, j;
clrscr( );
printf("Enter the main string:");
gets(st);
printf("\nEnter the substring to insert:");
gets(sub);
printf("Enter the index position:");
scanf("%d",&pos);
if(pos<=strlen(st)) {
for(i=0;i<=strlen(st);i++) {
if(i==pos)
{
for(j=0;st[i]!='\0';j++) // to store the 'st' to 'temp' from given position.
{
temp[j]=st[i];
i++;
}
temp[j]='\0';
i=pos;
for(j=0;sub[j]!='\0';j++) // to insert a sub-str to main string.
{
st[i]=sub[j];
i++;
}
for(j=0;temp[j]!='\0';j++) // Lastly to insert the 'temp' to 'st' after sub-str.
{
st[i]=temp[j];
i++;
}
st[i]='\0';
}}
printf("\nAfter adding the sub-string: %s",st);
}
else
printf("\nSorry, it is not possible to insert a substring in that position.");
getch();
}

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

9 comments:

  1. program is good ....pls provide the logic for me....

    ReplyDelete
  2. the program doesn't word... i tried executing it... but it failed

    ReplyDelete
  3. how to print this pyramid in c
    1
    25
    368
    47910

    ReplyDelete
  4. with out using functions is OK but will u provide it with using functions

    ReplyDelete
  5. total rubbish !!!!! this idiot is just stretching dis short n simple progam !!!!

    ReplyDelete
  6. sir this is too lenthy and takes lot of time for debugging can u minimise .

    ReplyDelete
  7. its tooo.... lengthy

    ReplyDelete
  8. how to write by using string manipulation functions

    ReplyDelete
  9. i want to know about the output

    ReplyDelete