Saturday, July 8, 2017

Library functions implementation


Main resource : http://vijayinterviewquestions.blogspot.in/search?q=strcpy


1. String lenth implemention
way 1:

int my_strlen(char *string)
{
int length;
for (length = 0; *string != '\0', string++)
{
length++;
}
return(length);
}


way 2:

int my_strlen(char *s)
{
char *p=s;

while(*p!='\0')
p++;

return(p-s);
}

recursive way3:


int strlen_r(char *s)
{
 if (*s=='\0') return 0;
 else return(1 + strlen_r(s+1));


2. String copy implemention

way 1:
char *my_strcpy(char dest[], const char source[])
{
int i = 0;
while (source[i] != '\0')
{
dest[i] = source[i];
i++;
}
dest[i] = '\0';
return(dest);
}

way 2:
char *mystrcpy(char *dst, const char *src)
{
char *ptr;
ptr = dst;
while(*dst++=*src++);
return(ptr);
}

way3: recursive
char *strcpy_r(char *s, char *t)
{
 if ((*s = *t) != '\0') strcpy_r(s+1, t+1);
 return s;
}

3. String compare implementation


int
strcmp(const char *str1, const char *str2)

Output:
  • if Return value < 0 then it indicates str1 is less than str2.
  • if Return value > 0 then it indicates str2 is less than str1.
  • if Return value = 0 then it indicates str1 is equal to str2.
A to Z ascii value = 65 to 90

a to z ascii value = 97 to 122


#include 

int mystrcmp(const char *s1, const char *s2);

int main()
{
printf("\nstrcmp() = [%d]\n", mystrcmp("A","A"));
printf("\nstrcmp() = [%d]\n", mystrcmp("A","B"));
printf("\nstrcmp() = [%d]\n", mystrcmp("B","A"));
return(0);
}

int mystrcmp(const char *s1, const char *s2)
{
while (*s1==*s2)
{
if(*s1=='\0')
return(0);
s1++;
s2++;
}
return(*s1-*s2);
}

strcmp() = [0]
strcmp() = [-1]
strcmp() = [1]


Recursive:
int strcmp_r(char *s, char *t)
{
 if (*s == '\0' || *s != *t) return *s - *t;
 else return(strcmp_r(s+1, t+1));
}
4. String cat implemention
/* Function to concatenate string t to end of s; return s */
char *myStrcat(char *s, const char *t)
{
char *p = s;

if (s == NULL || t == NULL)
return s; /* we need not have to do anything */

while (*s)
s++;

while (*s++ = *t++)
;

return p;
}


4. to upper and to lower  implemention
way1:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char my_tolower(char c)
{
        if(c >= 'a' && c<='z') {
                printf("\n already lower");
        }
        else 
        {
                return c ^ 32; 
        }
        // ('a'-'A' == 97-65 ==32)
        // space ' ' ascii value is 32. xor generally do toggle. 
}

char my_toupper(char c)
{
        if(c >= 'A' && c<='Z')
        {
                printf("\n already UPPER");
        }
        else 
        {
                return c ^ 32; 
        }
}
int main() {
        printf("\n=========================== \n");
        printf("\nto upper:%c \n",my_toupper('a'));
        printf("\nto lower:%c \n",my_tolower('A'));
        printf("\n=========================== \n");
        printf("\nto upper:%c \n",my_toupper('A'));
        printf("\nto lower:%c \n",my_tolower('a'));
        return 0;
}
SELVATHA-M-V0HE:test selvatha$ gcc to_upper_lower.c 
SELVATHA-M-V0HE:test selvatha$ ./a.out 

=========================== 

to upper:A 

to lower:a 

=========================== 

 already UPPER
to upper:? 

 already lower
to lower:? 


Ref: https://stackoverflow.com/questions/40995040/toupper-and-tolower-function-in-linux-using-xor-bitwise-operation


way 2:
int toUpper(int ch)
{
if(ch>='a' && c<='z')
return('A' + ch - 'a');
else
return(ch);
}

int toLower(int ch)
{
if(ch>='A' && c<='Z')
return('a' + ch - 'A');
else
return(ch);
}


way 3:


[tselva@bng-junos-d037 /b/tselva/bcopy/bk_copy_needed/copy-needed/work_tips_dir/sample_c_program]$ cat toupper.c
#include <stdio.h>

int mytoupper(int ch)
{
        if(ch >= 'a' && ch <= 'z') {
                //return ('A' + ch - 'a');
                return (ch-32);
        }
        else
                return ch;
}
int mytoLower(int ch)
{
        if(ch >= 'A' && ch <= 'Z') {
                //return ('A' + ch - 'a');
                return (ch+32);
        }
        else
                return ch;
}


int main()
{
        char lch, uch;

        //printf("\nEnter a lowercase character: ");
        //lch = getchar();

        //uch = mytoupper(lch);

        //printf("\nConverted %c to its uppercase %c",lch,uch);

        printf("\nEnter a uppercase character: ");
        lch = getchar();

        uch = mytoLower(lch);

        printf("\nConverted %c to its Lowercase %c\n",lch,uch);


        return 0;
}


way 4: malloc()
There are a number of ways we can implement this. We’re going to arbitrarily choose to use the sbrk syscall. The OS reserves stack and heap space for processes and sbrk lets us manipulate the heap. sbrk(0) returns a pointer to the current top of the heap. sbrk(foo) increments the heap size by foo and returns a pointer to the previous top of the heap.

void *malloc(size_t size) {
  void *p = sbrk(0);
  void *request = sbrk(size);
  if (request == (void*) -1) {
    return NULL; // sbrk failed.
  } else {
    assert(p == request); // Not thread safe.
    return p;
  }
}


way 5: sizeof()
#define my_sizeof(type) (char *)(&type+1)-(char*)(&type)
int main()
{
    double x;
    printf("%d", my_sizeof(x));
    getchar();
    return 0;
}

way 6: memcpy and memmove implementation:
http://www.geeksforgeeks.org/write-memcpy/





   return 0;

}

No comments:

Post a Comment