-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathSubstring.c
70 lines (55 loc) · 1.47 KB
/
Substring.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
#include<stdio.h>
#include<string.h>
int main(void)
{
char string[100][100],input[100];
int ar_pos[100]; /* stores the indexes of the substrings stored in the 2-D array that are repeated*/
int len, i, j; /*len is length of substring , i,j are looping variables*/
int pos = 0, count = 0, res =0, flag=0;
int ar_index=0; /* variable used for refering to indices of array*/
scanf("%s", input);
scanf("%d", &len);
/*input the string and length of sub string from the console*/
char check[len+1]; /* string used for making comparisions */
int subs = (strlen(input) - len + 1); /* computing total no. of substrings in the string*/
for(i=0; i < subs; i++)
{
for(j=0 ; j < len ; j++)
string[i][j] = input[j + pos];
pos++;
}
/* storing all the substrings in 2D array*/
pos = 0;
for(j=0 ; j < subs ; j++)
{
strcpy(check , string[j]);
for(i=pos+1 ; i <= subs ; i++)
{
res = strcmp(check , string[i]);
if(res == 0)
ar_pos[ar_index++] = i;
}
pos++;
}
ar_pos[ar_index++] = 0;
/* storing the indices of repeated substrings in a array*/
for(i=0; i < subs ; i++)
{
j=0;
while(ar_pos[j] != 0)
{
if(i == ar_pos[j])
{
flag=1;
break;
}
j++;
}
if(flag == 0)
count++;
flag=0;
}
/* counting the no. of distinct substrings */
printf("%d", count);
return 0;
}