Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Void-valued Function to extract substring from a string

Posted on 1997-04-05
1
245 Views
Last Modified: 2010-04-10
I need help in writing a a void-valued function to extract a substring from an existing string.

For Example:
     char   sub[80];
     ...
     substr ('Converting to C', 7, 4, &sub);

would store the string 'ting' into the variable called 'sub'. The function should store a NUL character at the end of the extracted string.
0
Comment
Question by:cyrax
1 Comment
 

Accepted Solution

by:
linpage earned 50 total points
ID: 1162578
the following will do it.  However most 'c' programmers would probably make your example substr('Converting to C', 6, 4, &sub);   because of the 0 offset convention (change the line to while ( *src && start-- > 0) for this).  This snippet tests for the
src running out, but not the destination buffer.  You might want
to include a length for dest to be on the safe side.

void substr(const char* src, int start, int len, char ** dest)
{
  while ( *src && --start > 0)
      src++;
 
  while ( *src && len > 0)
  {
    **dest = *src;
    *dest++;
    src++;
    len--;
  }
  **dest = 0x00;
}
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question