The UNIX and Linux Forums  

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
ksh: Comparing strings that contain spaces and working with substrings nancylt723 Shell Programming and Scripting 2 02-19-2008 11:01 AM
What the command to find out the record length of a fixed length file? tranq01 UNIX for Dummies Questions & Answers 3 10-19-2007 11:16 AM
Breaking strings into Substrings switch Shell Programming and Scripting 4 04-06-2006 01:29 AM
Sed working on lines of small length and not large length thanuman UNIX for Dummies Questions & Answers 3 04-15-2005 03:12 AM
creating a fixed length output from a variable length input r1500 Shell Programming and Scripting 2 12-03-2003 09:09 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-17-2002
Registered User
 

Join Date: Aug 2002
Location: Marlboro, MA
Posts: 114
Stumble this Post!
Accessing substrings by offset and length

Hi,

I have a simple question... In C do we have a standard library function which will return the pointer to a substring at certain offset and having certain length...

Ofcourse we should take care not to access beyond allocated length in the parent string and don't overwrite beyond allocated length in the destination string...

it may look like this

int foo (char *s, char *t, int offset, int length);

if *s = "vishnu"; offset = 3; length = 2; it should give back
*t = "sh"

I'm not saying I want a function with the exact interface as above, but anything which allows me to access substrings by offset and length would be great...

This is not a homework question!!! I work in an office and write small C programs and sh scripts to automate task in a UNIX environment..

Thank you!
Vishnu.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-17-2002
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,425
Stumble this Post!
strncpy() can copy n bytes. If you want to start at position 3 then you need to skip the first two bytes. So use strncpy(dest, s+2, 2). The first 2 is number of bytes to skip, the second 2 is the number of bytes to copy.
Reply With Quote
  #3 (permalink)  
Old 10-17-2002
Registered User
 

Join Date: Aug 2002
Location: Marlboro, MA
Posts: 114
Stumble this Post!
That was great Perderabo... and I learnt a lesson too! I certainly need to put more thought before taking for granted utility of certain standard C functions.. I have seen the strncpy(), but thought I'm restricted to access only from the beginning always...
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:40 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0