-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcg_substr_cnt.c
145 lines (88 loc) · 2.74 KB
/
cg_substr_cnt.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/***************************************************************************/
/* Function Name: cg_substr_cnt
/* Purpose : Extract a string between left boundary and right boundary.
/* for the given occurrence
/* Input :
/* Output :
/************************************************************************/
char* cg_substr_cnt(char* source,char* lbound, char* rbound, int cnt)
{
char* lposition;
char* rposition;
int begin,end,i,j,length;
char* newstring;
char* tmpstring;
char* newstring1;
char* tmp;
char* temp;
int lblength = strlen(lbound);
lposition = (char *)strstr(source, lbound);
// strstr has returned the address. Now calculate * the offset from the beginning of str
begin = (int)(lposition - source + 1);
length= strlen(source);
if(length<0)
{
lr_output_message("Enter string with some characters");
}
newstring = (char*)malloc(length+1);
memset(newstring,'\0',length+1);
strncpy(newstring,source,length);
/* Verifying count values */
if(cnt == 0)
{
lr_output_message("Enter occurrence value as 1 or >1");
return("-1");
}
/* Moving begin index for the number of occurence */
if(cnt>1)
{
temp = (char*)malloc(length+1);
temp = (char*)strdup(source);
for(j=0;j<cnt;j++)
{
for(i=1;i<begin;i++)
{
temp++;
}
lposition = (char *)strstr(temp, lbound);
begin = (int)(lposition - temp + 1);
begin = begin + lblength;
length= strlen(newstring);
strncpy(newstring,temp,length);
//lr_output_message("The newstring value is :%s",newstring);
}
/* Noting down the new begin position */
lposition = (char *)strstr(newstring, lbound);
begin = (int)(lposition - newstring + 1);
if(begin<0)
{
lr_output_message("The argument occurence count exceeds the left boundary occurrence count");
return("-1");
}
}
newstring1 = (char*)malloc(length+1);
memset(newstring1,'\0',length+1);
tmpstring = (char*)strdup(newstring);
begin = begin + lblength;
/* Extracting string from the new begin value */
for(i=1;i<begin;i++)
{
tmpstring++;
}
/* Finding right boundary in the new string */
rposition = (char *)strstr(tmpstring, rbound);
end = (int)(rposition - tmpstring + 1);
/* Calculating length upto the begining of right boundary*/
length = end - 1;
//lr_output_message("The value of length is :%d",length); to verify length
if(length < 0)
{
lr_output_message("ERROR: Verify right boundary");
//length = strlen(newstring);
return("-1");
}
/* Copying the extracted string to newstring 1*/
strncpy(newstring1,tmpstring,length);
lr_output_message("Substring is ::>%s",newstring1);
return newstring1;
}