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? 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
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
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
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
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.
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...
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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
|
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,...
|
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...
| |
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 =...
|
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...
|
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....
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |