c program to perform all string operation using pointers
#include
#include
int slength(char *);
void srev(char *);
void toupper(char *);
void tolower(char *);
void scpy(char *,char *);
void ssubstr(char *,char *);
void sconcat(char *,char *);
int scompare(char *,char *);
void main()
{
int len=0,ch,flag=0;
char s[81]="",*ps=s;
char sa[81]="",*psa=sa;
clrscr();
while(1)
{
printf("\n---------------Menu---------------------\n");
printf("1.length\n2.Reverse\n3.copy\n4.concate\n5.compare\n");
printf("6.substring\n7.toupper\n8.tolower\n9.exit\n");
printf("-------------------------------------------\n");
printf("\nEnter ur choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter string:");
fflush(stdin);
gets(ps);
len=slength(ps);
printf("\n String length is:%d",len);break;
case 2: printf("\n Enter string:");
fflush(stdin);
gets(ps);
srev(ps);break;
case 3: printf("\n Enter string :");
fflush(stdin);
gets(ps);
scpy(ps,psa);
printf("\n New string in s1:%s",psa);break;
case 4: printf("\n Enter string first:");
fflush(stdin);
gets(ps);
printf("\n Enter string second:");
gets(psa);
sconcat(ps,psa);break;
case 5: printf("\n Enter string first:");
fflush(stdin);
gets(ps);
printf("\n Enter string second:");
gets(psa);
flag= scompare(ps,psa);
if(flag==0)
printf("\n strings are equals");
else
printf("\n strings are not equals");break;
case 6: printf("\n Enter string first:");
fflush(stdin);
gets(ps);
printf("\n Enter string second:");
gets(sa);
ssubstr(ps,sa);break;
case 7: printf("\n Enter string:");
fflush(stdin);
gets(ps);
toupper(ps);
printf("%s",ps);break;
case 8: printf("\n Enter string:");
fflush(stdin);
gets(ps);
tolower(ps);
printf("%s",ps);break;
case 9:exit(0);
}
getch();
}
}
int slength(char *ps)
{ int i;
for(i=0;*ps!='\0';++ps,i++);
return i;
}
void toupper(char *ps)
{
for(;*ps!='\0';++ps)
{
if(*ps >= 65 && *ps <= 90)
*ps=*ps;
else
*ps=*ps-32;
}
}
void tolower(char *ps)
{
for(;*ps!='\0';++ps)
{
if(*ps>= 97 && *ps <= 122)
*ps=*ps;
else
*ps=*ps+32;
}
}
void ssubstr(char *ps,char *psa)
{ int flag=0;
char *ch=NULL,*t=NULL,*p=NULL;
*ch=*t=*ps;
*p=*psa;
for(;*ps!='\0';++ps)
{ if(*psa==*t)
for(;*psa!='\0';++psa)
if(*t==*psa&& (*ch==*(--ps)||*t+(slength(t)-1)==*p) )
{
*ch=*ps;
flag=1;
}
}
if(flag==1)
printf("\n String is substring:");
else
printf("\n String is not substring:");
}
void srev(char *ps)
{
int i,j;
char *temp=NULL;
int l=slength(ps);
for(i=0,j=l-1;i
*temp=*(ps+i);
*(ps+i)=*(ps+j);
*(ps+j)=*temp;
}
printf("\n %s",ps);
}
void scpy(char *ps,char *psa)
{
for(;*ps!='\0';++ps,++psa)
*psa=*ps;
*psa='\0';
}
void sconcat(char *ps,char *psa)
{ int i;
for(i=slength(ps);*psa!='\0';i++,++psa)
*(ps+i)=*psa;
*(ps+i)='\0';
printf("\n String after concat:%s",ps);
}
int scompare(char *sa,char *psa)
{
if(slength(sa)==slength(psa))
{
while(*sa!='\0')
{
if(*sa!=*psa)
return 1;
++sa;
++psa;
}
return 0;
}
else
return 1;
}