159. User defined (strlen, strupr, strchr, strcpy, strrev, strrev1, strrev2, strcat, strcmp)

 #include<stdio.h>

int my_strlen(const char *);

int my_strcmp(const char *,const char *);

const char * my_strchr(const char *,char);

void my_strcpy(const char *,char *);

void my_strcat(const char *,char *);

void my_strrev2(char *,int);

void my_strrev1(char *,char *);

void my_strrev(char *);

void my_strupr(char *);

char ch1[10],ch2,ch3[10];

int l1,l2,l3;

const char *s;

void main()

{

    // To compare 2 strings

    /*

    printf("Enter string 1 : ");

    scanf("%s",ch1);

    printf("Enter string 2 : ");

    scanf("%s",ch3);

    l3=my_strcmp(ch1,ch3);

    printf("%d\n",l3);

    */

   

    // To concatenate 2 srings

    /*

    printf("Enter string 1 : ");

    scanf("%s",ch1);

    printf("Enter string 2 : ");

    scanf("%s",ch3);

    my_strcat(ch1,ch3);

    printf("%s\n",ch3);

    */   

   

    // To reverse a string ( Method 3 )

    /*

    printf("Enter string 1 : ");

    scanf("%s",ch1);

    for(l2=0;ch1[l2];l2++);

    my_strrev2(ch1,l2);

    printf("%s\n",ch1);

    */

   

    // To reverse a string ( Method 2 )

    /*

    printf("Enter string 1 : ");

    scanf("%s",ch1);

    for(l1=0;ch1[l1];l1++);

    my_strrev1(ch1,ch1+l1-1);

    printf("%s\n",ch1);

    */

   

    // To reverse a string ( Method 1 )

    /*

    printf("Enter string 1 : ");

    scanf("%s",ch1);

    my_strrev(ch1);

    printf("%s\n",ch1);

    */

   

    // To copy 1 string to another string

    /*

    printf("Enter string 1 : ");

    scanf("%s",ch1);

    printf("Enter string 2 : ");

    scanf("%s",ch3);

    printf("Before copying : %s\n",ch3);

    my_strcpy(ch1,ch3);

    printf("After copying : %s\n",ch3);

    */

   

    // To find string length

    /*

    printf("length 1 = %d\n",my_strlen(ch1));

    */

   

    // To convert string to upper case

    /*

    my_strupr(ch1);

    printf("Upper converted : %s\n",ch1);

    */

   

    //To find character in a string

    /*

    printf("Enter a character : ");

    scanf(" %c",&ch2);

    s=my_strchr(ch1,ch2);

    if(s)

    {

        printf("Character is present\n");

    }

    else

        printf("Character not present\n");

    */

   

}

 

int my_strcmp(const char *p,const char *q)

{

    while(*p)

    {

        if(*p==*q)

        {

            p++,q++;

        }

        else

        return *p-*q;

    }

}

 

void my_strcat(const char *p,char *q)

{

    while(*q)

    q++;

   

    while(*p)

    {

        *q=*p;

         q++;

         p++;

    }

}

 

 

void my_strrev2(char *p,int i)

{

    char *q=p+i-1;

    while(p<q)

    {

        *p=*p^*q;

        *q=*p^*q;

        *p=*p^*q;

        p++;

        q--;

    }

}

 

 

void my_strrev1(char *p,char *q)

{

    while(p<q)

    {

        *p=*p^*q;

        *q=*p^*q;

        *p=*p^*q;

        p++;

        q--;

    }

}

 

 

void my_strrev(char *p)

{

    // using index

    /*

    int i,j,len;

    for(len=0;p[len];len++);

    for(i=0,j=len-1;i<j;i++,j--)

    {

        p[i]=p[i]^p[j];

        p[j]=p[i]^p[j];

        p[i]=p[i]^p[j];

    }

    */

   

    //using pointer

    char *q;

    q=p;

    while(*q)

        q++;

    q--;

   

    while(p<q)

    {

        *p=*p^*q;

        *q=*p^*q;

        *p=*p^*q;

        p++;

        q--;

    }

}

 

 

void my_strcpy(const char *p,char *q)

{

    // using index

    /*

    int i;

    for(i=0;p[i];i++)

    {

        q[i]=p[i];

    }

    */

   

    // using pointers

    while(*p)

    {

        *q++=*p++;

    }

}

 

 

int my_strlen(const char *p)

{

    int i;

    for(i=0;p[i];i++);

    return i;

}

 

void my_strupr(char *p)

{

    int i;

    for(i=0;p[i];i++)

            if(p[i]>='a' && p[i]<='z')

                     p[i]=p[i]-32;

}

 

const char *my_strchr(const char *p,char ch)

{

    int i;

    for(i=0;p[i];i++)

        if(p[i]==ch)

             return p+i;

    return 0;

}

Comments