Friday, March 4, 2011

User-Defined Functions

Design a function locate ( ) that takes two character array s1 and s2 and one integer value m as parameters and inserts the string s2 into s1 immediately after the index m.
Write a program to test the function using a real-life situation. (Hint: s2 may be a missing word in s1 that represents a line of text).

#include <stdio.h>
#include <string.h>
void locate(char s1[30],char s2[20],int m);
void main()
{
char str[30],str1[20];
int n;
clrscr();
printf("Input a string:");
gets(str);
printf("Input one more string:");
scanf("%s",str1);
printf("Enter place to insert second string:");
scanf("%d",&n);
locate(str,str1,n);
getch();
}
void locate(char s1[30],char s2[20],int m)
{
int i;
char str[40];
for(i=0;i<=m-1;i++)
str[i]=s1[i];
str[i]='\0';
strcat(str,s2);
printf("\nString is\n%s",str);
}

No comments:

Post a Comment