LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-14-2004, 11:40 AM   #1
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Rep: Reputation: 30
replace a substring with another string in C


yes. me again :P and this is yet another question for string manipulation in c.
I've searched this forum, but I didn't find sth helpful unluckily. So there I go:
I have
char *path = "/home/user/a_DIR/abc"

and I want to write a code to replace a_DIR with yet_another_DIR..
so path_new would be
char *path_new = "/home/users/yet_another_DIR/abc"

i tried man string and then man every function mention there. I think i could use a combination of them to make the above.<?> I'm not sure.. if you want you can help
thank you very much

ps. I 've tried some stuff with strstr but wasn't able to do have the wanted result

Last edited by zeppelin; 04-14-2004 at 11:50 AM.
 
Old 04-14-2004, 12:12 PM   #2
vinay_s_s
Member
 
Registered: Jul 2003
Posts: 659

Rep: Reputation: 30
well u can cut off the string after a_DIR and paste it to another temp string.
Then delete a_DIR.
then concatenate the new string to the path.
then concatenate the temp to path
 
Old 04-14-2004, 01:00 PM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Code:
#include <stdio.h>
#include <string.h>

char *replace_str(char *str, char *orig, char *rep)
{
  static char buffer[4096];
  char *p;

  if(!(p = strstr(str, orig)))  // Is 'orig' even in 'str'?
    return str;

  strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$
  buffer[p-str] = '\0';

  sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));

  return buffer;
}

int main(void)
{
  puts(replace_str("Hello, world!", "world", "Miami"));

  return 0;
}
itsme@dreams:~/C$ ./repstr
Hello, Miami!
itsme@dreams:~/C$
 
1 members found this post helpful.
Old 04-14-2004, 01:35 PM   #4
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
itsme86, you're the man!
I still don't get very well the arithmetic with pointers you do but it works!! :P

dreams.. good one!

Last edited by zeppelin; 04-14-2004 at 01:39 PM.
 
Old 04-22-2007, 11:41 AM   #5
ozmosis
LQ Newbie
 
Registered: Apr 2007
Posts: 9

Rep: Reputation: 0
Wow, wonderful code...too expressive!!!

deleted!!!

Last edited by ozmosis; 05-03-2007 at 10:22 AM.
 
Old 04-22-2007, 12:23 PM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Doing this sort of thing in memory and from harddisk are different(in C at least) as you can not safely update a file by doing a replace. So normally another couple of steps are involved, so the process becomes:
Read the file and store in memory
Manipulate the stored version
Write the whole memory version to file overwriting the previous version.
 
Old 04-22-2007, 12:59 PM   #7
ozmosis
LQ Newbie
 
Registered: Apr 2007
Posts: 9

Rep: Reputation: 0
deleted!!!

Last edited by ozmosis; 05-03-2007 at 10:25 AM.
 
Old 04-22-2007, 01:55 PM   #9
ozmosis
LQ Newbie
 
Registered: Apr 2007
Posts: 9

Rep: Reputation: 0

Thanks a lot!!!!
 
Old 04-23-2007, 08:08 AM   #10
ozmosis
LQ Newbie
 
Registered: Apr 2007
Posts: 9

Rep: Reputation: 0
deleted!!!

Last edited by ozmosis; 05-03-2007 at 10:26 AM.
 
Old 04-23-2007, 08:17 AM   #11
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
Quick look:

Code:
  stringa_sost(char buffer3[1000][1000], char buffer1[256], char buffer2[]);
Remove these char keywords!
And probably the array size [1000][1000] , [256]
 
Old 04-23-2007, 09:27 AM   #12
ozmosis
LQ Newbie
 
Registered: Apr 2007
Posts: 9

Rep: Reputation: 0
delete!!!!

Last edited by ozmosis; 05-03-2007 at 10:27 AM.
 
Old 04-24-2007, 01:59 PM   #13
ozmosis
LQ Newbie
 
Registered: Apr 2007
Posts: 9

Rep: Reputation: 0
delete!!!!

Last edited by ozmosis; 05-03-2007 at 10:27 AM.
 
Old 04-24-2007, 03:04 PM   #14
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Is this a homework/class assignment? how much knowledge do you have of C?
 
Old 04-24-2007, 03:35 PM   #15
ozmosis
LQ Newbie
 
Registered: Apr 2007
Posts: 9

Rep: Reputation: 0
delete!!!!

Last edited by ozmosis; 05-03-2007 at 10:27 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and replace string Johng Programming 9 01-13-2010 04:50 AM
How can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 10:58 AM
Replace substring with SED marri Programming 2 07-09-2005 05:18 PM
[sed] replace string? chuanyung Programming 3 03-11-2004 08:42 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration