forked from amalpoulose/Zoho-Interview-questions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet 01-03.Save the string in a two dimensional array and search for substring.c
79 lines (73 loc) · 1.84 KB
/
Set 01-03.Save the string in a two dimensional array and search for 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
71
72
73
74
75
76
77
78
79
/* Save the string “WELCOMETOZOHOCORPORATION” in a two dimensional array and search for substring like “too” in the two dimensional string both from left to right and from top to bottom.
w e L C O
M E T O Z
O H O C O
R P O R A
T I O n
And print the start and ending index as
Start index : <1,2>
End index: <3, 2>*/
#include<stdio.h>
int sqt(float);
int main(void)
{
// a is 2d array to store the sting
char str[100],str1[100],a[100][100];
//main variables inx =index of string n= length of string rc=number of rows and coloumn(square matrix)
int n,rc,i,j,inx,k,c;
//enter main string to str
printf("Enter the string : ");
scanf("%s",str);
//find length of string
for(n=0;str[n];n++);
// find row and coloumn size by taking square root of n(length of string
if(n=sqt(n)*sqt(n))
rc=sqt(n)+1;
else
rc=sqt(n);
//enter each character in to 2 diamensional array
for(inx=0,i=0;i<rc ;i++)
for(j=0;j<rc && str[inx];j++)
{
a[i][j]=str[inx++];
}
// print contents of 2d array
for(i=0;i<rc;i++)
{
for(j=0;j<rc;j++)
printf("%c ",a[i][j]);
printf("\n");
}
//Enter the substring as str1
printf("Enter word to found : ");
scanf("%s",str1);
//find substring in rows
for(i=0;i<rc;i++)
for(j=0;j<rc;j++)
if(a[i][j]==str1[0])
{ for(k=j,c=0;a[i][k]==str1[c] && a[i][k]!='\0';k++,c++);
if(str1[c]=='\0'){
printf("\nStart index : <%d ,%d> End index: <%d, %d>\n",i,j,i,k-1);}
}
//find substring in coloumns
for(i=0;i<rc;i++)
for(j=0;j<rc;j++)
if(a[j][i]==str1[0])
{
for(k=j,c=0;a[k][i]==str1[c] && a[k][i]!='\0';k++,c++);
if(str1[c]=='\0'){
printf("\nStart index : <%d ,%d> End index: <%d, %d>\n",j,i,k-1,i);}
}
return 0;
}
//user defined function to find square root by Babylonian method
int sqt(float n)
{
float x=n,y=1;
while(x-y>0.00001)
{
x=(x+y)/2;
y=n/x;
}
return x;
}