Solved

Void-valued Function to extract substring from a string

Posted on 1997-04-05
1
242 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now