• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

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.
0
sanjex
Asked:
sanjex
  • 3
1 Solution
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now