• C

Substring function

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.
sanjexAsked:
Who is Participating?
 
rbrCommented:
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
 
rbrCommented:
include <string.h>
include <stdlib.h>

0
 
alexoCommented:
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
 
rbrCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.