473,503 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find first occurrence of string and replace

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?

Oct 16 '06 #1
6 5778

On Sun, 15 Oct 2006, Registered User wrote:
>
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*/
What if strlen(s3)+strlen(s2) >= 80? Does 'insert' behave differently
then, or have you got a buffer overflow?
http://en.wikipedia.org/wiki/Buffer_overflow

Also, you could speed up some cases by deleting or inserting only
as many characters as you need to, and writing over the others. For
example, to get from "concatenate" to "concamelenate", you'd delete
zero characters, insert two characters ("concatxxenate"), and then
overwrite all five characters ("concamelenate"). To get from
"concatenate" to "condogenate", you wouldn't insert or delete any
characters; just write "dog" at the right point in "concatenate"
and you're done.

HTH,
-Arthur
Oct 16 '06 #2
On Sun, 15 Oct 2006, Arthur J. O'Dwyer wrote:
>
On Sun, 15 Oct 2006, Registered User wrote:
>>
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*/

What if strlen(s3)+strlen(s2) >= 80? Does 'insert' behave differently
then, or have you got a buffer overflow?
http://en.wikipedia.org/wiki/Buffer_overflow

Also, you could speed up some cases by deleting or inserting only
as many characters as you need to, and writing over the others. For
example, to get from "concatenate" to "concamelenate", you'd delete
zero characters, insert two characters ("concatxxenate"), and then
overwrite all five characters ("concamelenate"). To get from
"concatenate" to "condogenate", you wouldn't insert or delete any
characters; just write "dog" at the right point in "concatenate"
and you're done.
One could also merge the the three operations together by
using strstr(), memmove() and memcpy() in a single function:

#include <string.h>

char *found = strstr(s3, s1);
size_t len = strlen(s2);

if (found) {
/* Exercise: insert bounds checking code here */
memmove(found + len,
found,
strlen(s3) - strlen(s1) - (found - s3));
memcpy(found, s2, len);
}

Tak-Shing
Oct 16 '06 #3
On Mon, 16 Oct 2006, Tak-Shing Chan wrote:
On Sun, 15 Oct 2006, Arthur J. O'Dwyer wrote:
>>
On Sun, 15 Oct 2006, Registered User wrote:
>>>
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*/

What if strlen(s3)+strlen(s2) >= 80? Does 'insert' behave differently
then, or have you got a buffer overflow?
http://en.wikipedia.org/wiki/Buffer_overflow

Also, you could speed up some cases by deleting or inserting only
as many characters as you need to, and writing over the others. For
example, to get from "concatenate" to "concamelenate", you'd delete
zero characters, insert two characters ("concatxxenate"), and then
overwrite all five characters ("concamelenate"). To get from
"concatenate" to "condogenate", you wouldn't insert or delete any
characters; just write "dog" at the right point in "concatenate"
and you're done.

One could also merge the the three operations together by
using strstr(), memmove() and memcpy() in a single function:

#include <string.h>

char *found = strstr(s3, s1);
size_t len = strlen(s2);

if (found) {
/* Exercise: insert bounds checking code here */
memmove(found + len,
found,
strlen(s3) - strlen(s1) - (found - s3));
memcpy(found, s2, len);
}
Sorry, the memmove() line above is wrong. It should be:

memmove(found + len,
found + strlen(s1),
strlen(s3) - strlen(s1) - (found - s3));

Tak-Shing
Oct 16 '06 #4
On Mon, 16 Oct 2006, Tak-Shing Chan wrote:
On Mon, 16 Oct 2006, Tak-Shing Chan wrote:
>On Sun, 15 Oct 2006, Arthur J. O'Dwyer wrote:
>>>
On Sun, 15 Oct 2006, Registered User wrote:

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*/

What if strlen(s3)+strlen(s2) >= 80? Does 'insert' behave differently
then, or have you got a buffer overflow?
http://en.wikipedia.org/wiki/Buffer_overflow

Also, you could speed up some cases by deleting or inserting only
as many characters as you need to, and writing over the others. For
example, to get from "concatenate" to "concamelenate", you'd delete
zero characters, insert two characters ("concatxxenate"), and then
overwrite all five characters ("concamelenate"). To get from
"concatenate" to "condogenate", you wouldn't insert or delete any
characters; just write "dog" at the right point in "concatenate"
and you're done.

One could also merge the the three operations together by
using strstr(), memmove() and memcpy() in a single function:

#include <string.h>

char *found = strstr(s3, s1);
size_t len = strlen(s2);

if (found) {
/* Exercise: insert bounds checking code here */
memmove(found + len,
found,
strlen(s3) - strlen(s1) - (found - s3));
memcpy(found, s2, len);
}

Sorry, the memmove() line above is wrong. It should be:

memmove(found + len,
found + strlen(s1),
strlen(s3) - strlen(s1) - (found - s3));
Oops, I forgot the terminating '\0':

memmove(found + len,
found + strlen(s1),
strlen(s3) - strlen(s1) - (found - s3) + 1);

Tak-Shing
Oct 16 '06 #5
In comp.lang.c Tak-Shing Chan <t.****@gold.ac.ukwrote:
Sorry, the memmove() line above is wrong. It should be:
Oops, I forgot the terminating '\0':
Perhaps posting on Mondays is a bad idea :-)

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Oct 16 '06 #6
Is there a better way of doing this?

Your method looks fine. In the real world, time is money. An argument can
be made that whatever method works for you, especially if it's simple to
code, understand, and maintain, can be considered "better." Are there more
elegant solutions? Possibly. Are the "better?" Not necessarily.

I don't code in C but here's one way to do it PowerBASIC. (This probably
won't help you, since C almost surely doesn't have its own EXTRACTS$
function, but I couldn't resist.)
first$ = "cat" : second$ = "dog"
third$ = "Actually, I believe cat is man's best friend!"

a$ = EXTRACT$(third$, first$)

IF a$ <third$ THEN
a$ = a$ + second$ + RIGHT$(third$, LEN(third$) - LEN(a$) - LEN(first$))
END IF
In the above example, the variable a$ now is equal to "Actually, I believe
dog is man's best friend!"

Ed Collins

"Registered User" <in*******************@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
Hi experts,
I'm trying to write a program to solve the following exercise:
rest snipped...

Oct 18 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2239
by: andrea.gavana | last post by:
Hello NG, probably this is a basic question, but I'm going crazy... I am unable to find an answer. Suppose that I have a file (that I called "Errors.txt") which contains these lines: MULTIPLY...
4
62086
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
24
4468
by: Wim Roffal | last post by:
Is there a possibility to do a string replace in javascript without regular experessions. It feels like using a hammer to crash an egg. Wim
9
2141
by: Crirus | last post by:
dim pp as string pp="{X=356, Y=256}{X=356, Y=311.2285}{X=311.2285, Y=356}{X=256, Y=356}{X=200.7715, Y=356}{X=156, Y=311.2285}{X=156, Y=256}{X=156, Y=200.7715}{X=200.7715, Y=156}{X=256,...
9
9116
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
5
3168
by: comp.lang.php | last post by:
$orderBy = 's.app_date desc, s.last_name asc, s.first_name asc, s.mi asc'; if ($_REQUEST) { $ascArray = array('asc' => 'desc', 'desc' => 'asc'); // ARRAY OF ALL ORDERING POSSIBILITIES $junk =...
1
6689
by: MCH | last post by:
Hi there, I am using std:string and try to replace a substring with replace function. From the document, I found one prototype of string.replace basic_string& replace(iterator first, iterator...
2
1304
by: glennremar | last post by:
I was tring to write a function to make that took the letters that a user is searching for and making the first occurrence of it bold in the results (ignoring the case). thought this would work....
10
18612
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5596
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
401
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.