R
Registered User
Hi experts,
I'm trying to write a program to solve the following exercise:
Accept three strings from the user. Find the first occurrence of the
first string in the third string. If it is present replace the second
string in its place.
Note: First and Second string might not be of the same length.
E.g.: First string: "cat"
Second string: "camel"
Third string: "concatenate"
Output: "concamelenate"
I've made a complete working program. The following is the main() part
of my program.
int main()
{
char s1[80], s2[80], s3[80];
int pos;
/*Code for accepting 3 strings from user*/
/*and storing in s1, s2 and s3*/
pos=find(s1, s3); /*find() returns the position at which */
/*s1 occurs in s3, -1 otherwise*/
delete(pos, s3, strlen(s1)); /*Deletes strlen(s1) characters from s3
starting at pos*/
/*Does not delete if pos<0*/
insert(pos, s3, s2);/*Inserts s2 in s3 at location specified by pos*/
/*Does nothing if pos<0*/
puts(s3);
}
So, the three operations I've used for finding and replacing are:
(1)find
(2)delete and
(3)insert
My question is: Is there a better way of doing this?
I'm trying to write a program to solve the following exercise:
Accept three strings from the user. Find the first occurrence of the
first string in the third string. If it is present replace the second
string in its place.
Note: First and Second string might not be of the same length.
E.g.: First string: "cat"
Second string: "camel"
Third string: "concatenate"
Output: "concamelenate"
I've made a complete working program. The following is the main() part
of my program.
int main()
{
char s1[80], s2[80], s3[80];
int pos;
/*Code for accepting 3 strings from user*/
/*and storing in s1, s2 and s3*/
pos=find(s1, s3); /*find() returns the position at which */
/*s1 occurs in s3, -1 otherwise*/
delete(pos, s3, strlen(s1)); /*Deletes strlen(s1) characters from s3
starting at pos*/
/*Does not delete if pos<0*/
insert(pos, s3, s2);/*Inserts s2 in s3 at location specified by pos*/
/*Does nothing if pos<0*/
puts(s3);
}
So, the three operations I've used for finding and replacing are:
(1)find
(2)delete and
(3)insert
My question is: Is there a better way of doing this?