Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Void-valued Function to extract substring from a string

Posted on 1997-04-05
1
Medium Priority
?
255 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
[X]
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
1 Comment
 

Accepted Solution

by:
linpage earned 100 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

670 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