/*
Title: Implement following operations on string with pointers
 1) Length
 2) Palindrome
 3) String comparison
 4) Copy
 5) Reverse
 6) Substring
*/


#include<stdio.h>
#include<stdlib.h>

int mylen(char *s)
{
int i;
i=0;
while(*(s+i)!='\0')
i++;
return i;
}

void myrev(char *s)
{
int i,j;
char t;
i=0;
j=mylen(s)-1;
while(i<j)
{
t=*(s+i);
*(s+i)=*(s+j);
*(s+j)=t;
i++;j--;
}

}

void mycopy(char *s1,char *s2)
{
int i=0;
while(*(s1+i)!='\0')
{
*(s2+i)=*(s1+i);
i++;
}
*(s2+i)='\0';
}

void myconcat(char *s1,char *s2)
{
int i,j;
i=mylen(s1);
for(j=0;*(s1+j)!='\0';j++,i++)
*(s1+i)=*(s2+j);
*(s1+i)='\0';
}


int mycmp(char *s1,char *s2)
{
int i=0;
while(*(s1+i)!='\0' || *(s2+i)!='\0')
{
if(*(s1+i)!=*(s2+i))
return (*(s1+i))-(*(s2+i));
i++;
}
return 0;
}

int mypal(char *s)
{
int i,j;
i=0;
j=mylen(s)-1;
while(i<j)
{
if(*(s+i)!=*(s+j))
return 0;
i++;j--;
}
return 1;
}

void mysubstr(char *s1,char *s2)
{
int i=0,j,a[10],cnt=0;
for(i=0;*(s1+i)!='\0';i++)
{
j=0;
if(*(s1+i)==*(s2+j))
{
i++;
for(j=1;*(s2+j)!='\0';j++,i++)
{
if(*(s1+i)!=*(s2+j))
break;
}
if(*(s2+j)=='\0')
cnt++;
}
}

if(cnt==0)
printf("\n%s is NOT substr of %s",s2,s1);
else
printf("\n[%s] is substr of [%s] and It's [ %d ] times",s2,s1,cnt);
}

int main()
{
int ch;
char s1[10],s2[10];
int l;
while(1)
{
system("clear");
printf("\n********MENU***********");
printf("\n1.Length\n2.Copy\n3.Compare\n4.Reverse\n5.Palindrome");
printf("\n6.Concat\n7.Substring\n8.Exit\nEnter ur choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter String: ");
scanf("%s",s1);
l=mylen(s1);
printf("\nLenght of [%s] is [%d]",s1,l);
getchar();
break;

case 2:
printf("\nEnter String: ");
scanf("%s",s1);
mycopy(s1,s2);
printf("\ncopied string is %s",s2);
getchar();
break;
case 3:
printf("\nEnter String 1: ");
scanf("%s",s1);
printf("\nEnter String 2:");
scanf("%s",s2);
l=mycmp(s1,s2);
if(l==0)
printf("\nStrings are EQUAL");
else if(l<0)
printf("\nS2>S1");
else
printf("\nS1>S2");
getchar();
break;
case 4:
printf("\nEnter String: ");
scanf("%s",s1);
myrev(s1);
printf("\nReverse is %s",s1);
getchar();
break;
case 5:
printf("\nEnter String: ");
scanf("%s",s1);
l=mypal(s1);
if(l==1)
printf("\nString %s is palindrome",s1);
else
printf("\nString %s is NOT palindrome",s1);
getchar();
break;
case 6:
printf("\nEnter String 1: ");
scanf("%s",s1);
printf("\nEnter String 2: ");
scanf("%s",s2);
myconcat(s1,s2);
printf("\nConcated String is %s",s1);
getchar();
break;
case 7:
printf("\nEnter String 1: ");
scanf("%s",s1);
printf("\nEnter String 2: ");
scanf("%s",s2);
mysubstr(s1,s2);
getchar();
break;
case 8:
exit(0);
}
getchar();
}
return 0;
}


//END OF PROGRAM