Friday, May 30, 2014

WRITE A PROGRAMME TO IMPLEMENT THE LIBRARY FUNCTIONS-STRLEN,STRCMP,STRLWR,STRUPR,STRREV,STRCPY,STRCAT USING POINTER

//WRITE A PROGRAMME TO IMPLEMENT THE FOLLOWING LIBRARY FUNCTIONS-
//STRLEN,STRCMP,STRLWR,STRUPR,STRREV,STRCPY,STRCAT
//String & Pointer

#include<stdio.h>
#include<conio.h>
int length(char*);
int comp(char*,char*);
void lower(char*);
void upper(char*);
void reverse(char*);
void copy(char*,char*);
void concat(char*,char*);

void main()
{
char s1[20],s2[20],s3[20],s4[20];
int l,i;
clrscr();
printf("enter the first string\n");
gets(s1);
printf("enter the second string\n");
gets(s2);


printf("the length of %s string is- ",s1);
l=length(s1);
printf("%d\n",l);


printf("the result of compairing first and second string-\n");
i=comp(s1,s2);
if(i==0)
{
printf("\n%s and %s are equal\n",s1,s2);
}
else
{
printf("\n %s and %s are not equal with a difference of %d in their ascii value\n",s1,s2,i);
}


printf("\nthe %s string after changing case to lower- ",s1);
lower(s1);
printf("%s\n",s1);


printf("\nthe %s string after changing case to upper- ",s1);
upper(s1);
printf("%s\n",s1);

printf("\n the %s string after being reversed- ",s1);
reverse(s1);
printf("%s\n",s1);


printf("\n %s after being copied by %s is- ",s1,s2);
copy(s1,s2);
printf("%s\n",s1);


printf("enter two strings for applying concatination operation\n");
printf("enter the first string\n");
gets(s3);
printf("enter the second string\n");
gets(s4);
printf("\n%s after being concatenated with %s- ",s3,s4);
concat(s3,s4);
printf("%s\n",s3);


getch();
}

int length(char *p)
{
int i=0;
while(*p)
{
i++;
p++;
}
return(i);
}

int comp(char *p,char *q)
{
int i=0;
while(p[i] && q[i] && (p[i]==q[i]))
{
i++;
}
if(p[i]==q[i])
return 0;
else if(p[i]>q[i])
return(p[i]-q[i]);
else
return(q[i]-p[i]);
}

void lower(char *p)
{
int i=0;
while(p[i])
{
if(p[i]>=65 && p[i]<=91)
 {
   p[i]=p[i]+32;
 }
 i++;
}
}

void upper(char *p)
{
int i=0;
while(p[i])
{
if(p[i]>=97 && p[i]<=123)
 {
   p[i]=p[i]-32;
 }
 i++;
}
}

void reverse(char *p)
{
int i,j,len=strlen(p);
char ch;
for(i=0,j=len-1;i<j;i++,j--)
{
ch=p[i];
p[i]=p[j];
p[j]=ch;
}
}

void copy(char *p,char *q)
{
while(*q)
{
*p=*q;
q++;
p++;
}
if(*p!=0)
*p=0;
}

void concat(char *p,char *q)
{
int i=0,j=0;
while(p[i])
{
i++;
}
p[i]=32;
i++;
while(q[j])
{
p[i]=q[j];
i++;
j++;
}
p[i]='\0';
}

1 comment: