string - How to obtain a substring based on index in C -


for background, i'm not familiar c, i'm proficient in java. in current program i'm working on, i'm trying figure out how implement java method somestring.substring(int startindex, int endindex) returns new string based on starting , ending index of previous one.

for purposes of implementation, cut first char off , return remaining string. here's implementation in java.

public string cut_string(string word) {      string temp = word.substring(1, word.length());     return temp; } 

use this

#include <stdio.h> #include <stdlib.h>  char* substring(char*, int, int);  int main()  {    char string[100], *pointer;    int position, length;     printf("input string\n");    gets(string);     printf("enter position , length of substring\n");    scanf("%d%d",&position, &length);     pointer = substring( string, position, length);     printf("required substring \"%s\"\n", pointer);     free(pointer);     return 0; }  /*c substring function: returns pointer substring */  char *substring(char *string, int position, int length)  {    char *pointer;    int c;     pointer = malloc(length+1);     if (pointer == null)    {       printf("unable allocate memory.\n");       exit(1);    }     (c = 0 ; c < length ; c++)    {       *(pointer+c) = *(string+position-1);             string++;       }     *(pointer+c) = '\0';     return pointer; } 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -