Solved

Void-valued Function to extract substring from a string

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 120
mixing C++ & C# in Vis Studio 2013 7 170
VS2015 compilation and missing DLLs 9 147
Dialogbox API leak? 18 94
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

786 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