-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcg_substr_index.c
55 lines (34 loc) · 1.01 KB
/
cg_substr_index.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/********************************************************************/
/* Function Name: cg_substr_index
/* Purpose : Extracts a string between the start index and end index.
/* Input : Source string, Stat index and end index
/* Output : Extracted sting between the start and end index
/******************************************************************/
char* cg_substr_index(char* source,int begin, int end)
{
int length = end-begin;
char* newstring;
char* tmpstring;
int i, len;
len = strlen(source);
if(end>len)
{
lr_output_message("ERROR:Verify right bound index");
return("-1");
}
if(length<=0)
{
lr_output_message("ERROR: Enter start and end index value to extract atleast 1 character");
return("-1");
}
newstring = (char*)malloc(length+1);
memset(newstring,'\0',length+1);
tmpstring = (char*)strdup(source);
for(i=1;i<begin;i++)
{
tmpstring++;
}
strncpy(newstring,tmpstring,length);
lr_output_message("Substring is ::>%s",newstring);
return newstring;
}