Solved

Substring function

Posted on 1999-01-20
4
222 Views
Last Modified: 2010-04-15
Can anyone give me a complete code of the substr function using this parameters. substr( source, start, length ). If any non standard header files are included please include the code also. Planning to implement the function in Red hat linux.
0
Comment
Question by:sanjex
  • 3
4 Comments
 
LVL 10

Accepted Solution

by:
rbr earned 100 total points
ID: 1258192
char *substr (char *src,int start,int length)
{
      char *ret;
      ret=calloc (sizeof(char),length+1);
      if (ret) {
            strncpy (ret,src+start,length);
            ret[lentgh]='\0'
      }
      return (ret);
}

0
 
LVL 10

Expert Comment

by:rbr
ID: 1258193
include <string.h>
include <stdlib.h>

0
 
LVL 11

Expert Comment

by:alexo
ID: 1258194
Allocating memory in a function and expecting the caller to free it is an extremely bad practice and a source of countless memory leaks.
0
 
LVL 10

Expert Comment

by:rbr
ID: 1258195
Otherwise he has to give a opointer to the function were to store the string

void substr (char *src,int start,int length,char *dest)
{
    strncpy (dest,src+start,length);
    dest[lentgh]='\0'
}
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Binary Bomb: Phase 4 func4 3 330
memory leak detection 9 81
Why adding test code changes the build ? 11 132
C#, VS15, StructLayout 1 134
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

776 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