Solved

Void-valued Function to extract substring from a string

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SetCurrentDirectory path limit 7 105
C++ Language error 28 197
GUI: DIalog Stacking and Popping in MS C++ 4 65
designing in object programming 12 52
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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…

861 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

24 Experts available now in Live!

Get 1:1 Help Now