The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Substituting variables from external files oneillc9 Shell Programming and Scripting 1 07-31-2008 03:12 PM
substituting neyo Shell Programming and Scripting 1 03-08-2008 10:08 AM
Substituting with value of variable in Sed jyotipg Shell Programming and Scripting 1 09-26-2006 09:54 AM
Substituting variable with current time jhansrod Shell Programming and Scripting 4 11-20-2005 08:20 PM
substituting shell variables suds19 Shell Programming and Scripting 1 10-16-2002 08:55 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-21-2008
cleopard cleopard is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 29
substituting one string for another

I have a Linux C program I'm writing that has one section where, within a large string, I need to substitute a smaller string for another, and those probably won't be the same size.

For instance, if I have a string:

"Nowisthetimeforallgoodmen"

and I want to substitute 'most' for 'all' the main string will increase in size:

"Nowisthetimeformostgoodmen"


or a substitution that will increase the size of the main string:

"Nowisthetimefornogoodmen"


I thought I might get some opinions about what might be the best approach on something like this, besides some more cumbersome way of doing it. I've been looking at the C string functions to see if there's one that might be helpful, but I don't really see any.

Any help would be appreciated.
  #2 (permalink)  
Old 08-21-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 741
Look into the strstr() and strspn() functions.
  #3 (permalink)  
Old 08-21-2008
redoubtable redoubtable is offline
Registered User
  
 

Join Date: Aug 2008
Location: Portugal
Posts: 242
There are actually many ways in which you can do this.

The first problem is the size of your string (char array). You can either use a fixed length array with a bound which you expect won't be exceeded (512 bytes for example) or you can work with dynamic length memory (malloc()/calloc()). I would advise you to use malloc() only as a last resort because it's slower/harder to maintain/prone to human error.

The second problem is choosing the appropriate way of doing the substitution. If you're replacing characters of the same size you could just do it directly like
Code:
char * p;
if ((p = strstr (array, "isthetime")) != NULL)
    strncpy (p, "new chars", strlen ("new chars"));
and it should cause no problem. On the other hand if you're replacing characters with different lengths say "AAA" for "AAAA" you must have in mind that the substitution will overwrite one element in the array. If you wish to keep that element you must copy the array into a temporary array and then strncpy() it back to the original array after the replacing takes place.

You're third problem is having much caution with the terminating byte '\0' in your array. If you're not careful enough you might overwrite it and forget to replace it which in turn could lead to unexpected conditions.

PS: you can also use regcomp() and regexec() for pattern matching/replacing.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 10:18 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0