-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmirror_words_in_a_string.c
104 lines (90 loc) · 1.29 KB
/
mirror_words_in_a_string.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char* reverse(char* a)
{
int i,j,n=0;
char temp;
while(a[n]!='\0')
{
n++;
}
for(i=0;i<n/2;i++)
{
temp=a[i];
a[i]=a[n-i-1];
a[n-i-1]=temp;
}
a[n]='\0';
return a;
}
int word(char* l,char* m,int p,int q)
{
if(q==p)
{
return 0;
}
int i=p,j,k=0,count=0;
while(m[k]!='\0')
{
k++;
}
for(j=0;j<=k;j++)
{
if(m[i]==l[j] && count<q-p+1)
{
count++;
i++;
}
else if(count==q-p+1 && l[j]==' ')
{
return 1;
}
else if(count==q-p+1 && j==k)
{
return 1;
}
else
{
i=p;
count=0;
}
}
return 0;
}
int check(char* l,char* m)
{
int res=0,i=0,j=0,k=0;
while(m[k]!='\0')
{
k++;
}
while(i<=k && j<=k)
{
while(m[j]!=' ' && j<k )
{
j++;
}
res=res+word(l,m,i,j-1);
i=j+1;
j++;
}
return res;
}
int main()
{
char* str=(char*)calloc(sizeof(char),100);
scanf("%[^\n]s",str);
char* s=(char*)calloc(sizeof(char),100);
int i;
for(i = 0; str[i] != '\0'; ++i)
{
s[i] = str[i];
}
s[i] = '\0';
char* str1=(char*)calloc(sizeof(char),100);
str1=reverse(str);
int result=check(s,str1);
printf("%d\n",result);
return 0;
}