implementation of c string functions

13
Prepared by Mohammed Sikander Technical Lead Cranes Software International Limited

Upload: mohamed-sikander

Post on 13-Jan-2017

413 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Implementation of c string functions

Prepared by Mohammed SikanderTechnical LeadCranes Software International Limited

Page 2: Implementation of c string functions

size_t strlen(const char *str);

[email protected] 2

size_t mystrlen(const char *str){

const char *end = str;while( *end != ‘\0’)

end++;return end – str;

}

Page 3: Implementation of c string functions

char * strcpy(char *dest, const char *src);

[email protected] 3

void mystrcpy(char *dest, const char *src){

while(1){

*dest = *src;if(*dest == ‘\0’)

return;src++;dest++;

}}

Page 4: Implementation of c string functions

char * strcpy(char *dest, const char *src);

[email protected] 4

void mystrcpy(char *dest, const char *src){

while((*dest++ = *src++)){}

}

Page 5: Implementation of c string functions

[email protected] 5

void mystrncpy(char *dest, const char *src, size_t n){

for(int i = 0 ; i < n ; i++){

*dest = *src;if(*dest == ‘\0’)

return;src++;dest++;

}}

int main( ){

char str[20] = “KARNATAKA”;char s1[20] = “ABCDEFGHIJKLMN”;char s2[20] = “ABCDEFGHIJKLMN”;

mystrncpy(s1 , str ,5);

mystrncpy(s2 , str , 12);

printf(“s1 = %s \n” , s1);printf(“s2 = %s \n” , s2);

}

Page 6: Implementation of c string functions

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

[email protected] 6

int mystrcmp(const char *str1 , const char *str2){

while(1){

if(*str1 != *str2)return *str1 - *str2;

if(*str1 == ‘\0’)return 0;

str1++;str2++;

}}

while(*str1 == *str2 && *str1 != ‘\0’){

str1++;str2++;

}return *str1 - *str2;

Page 7: Implementation of c string functions

int strncmp(const char *str1 , const char *str2,size_t n);

[email protected] 7

int mystrcmp(const char *str1 , const char *str2,size_t n){

for(int I = 0 ; i < n ; i++){

if(*str1 != *str2)return *str1 - *str2;

if(*str1 == ‘\0’)return 0;

str1++;str2++;

}return 0;

}

int main( )

{

char str1[ ] = “ BANGALORE”;

char str2[ ] = “BANGKOK”;

int res;

res = mystrncmp(str1 , str2 , 3) ;

printf(“res = %d “ , res);

res = mystrncmp(str1 , str2 , 5) ;

printf(“res = %d “ , res);

}

Page 8: Implementation of c string functions

int strcmpi(const char *str1 , const char *str2);

[email protected] 8

int mystrcmpi(const char *str1 , const char *str2){

while(1){

char x = toupper(*str1);char y = toupper(*str2);if(x != y)

return x - y;if(*str1 == ‘\0’)

return 0;str1++;str2++;

}}

int main( ){

char str1 [ ] = “ BANGALORE “;char str2 [ ] = “bangalore”;int res;res = mystrcmpi( str1 , str2);printf(“res = %d “ , res);

}

Page 9: Implementation of c string functions

void mystrrev(char *startptr)

{

int len = strlen(startptr);

char *endptr = startptr + len - 1;

while(startptr < endptr)

{

myswap(startptr , endptr);

startptr++;

endptr--;

}

}

[email protected] 9

void myswap(char *a,char *b)

{

char temp = *a;

*a = *b;

*b = temp;

}

int main( )

{

char str[ ] = “ SIKANDER”;

printf(“str = %s” , str);

mystrrev(str);

printf(“\n After Reverse \n”);

printf(“str = %s” , str);

}

Page 10: Implementation of c string functions

char *strchr(const char *str, char key);

char *mystrchr(const char *str , char key){

while(*str != ‘\0’){

if(*str == key)return str;

str++;}return NULL;

}

[email protected] 10

int main( ){

char str[ ] = “BANGALORE”;char *ptr;

ptr = mystrchr(str , ‘A’);if(ptr == NULL)

printf(“Key Not found”);else

printf(“Found at index “ , ptr – str);

ptr = mystrchr(str , ‘C’);if(ptr == NULL)

printf(“Key Not found”);else

printf(“Found at index “ , ptr – str);}

Page 11: Implementation of c string functions

char *strrchr(const char *str, char key);

char *mystrrchr(const char *start , char key){

int len = strlen(str);char *end = start + len – 1; while(end > start){

if(*end == key)return end;

end--;}return NULL;

}

[email protected] 11

int main( ){

char str[ ] = “BANGALORE”;char *ptr;

ptr = mystrrchr(str , ‘A’);if(ptr == NULL)

printf(“Key Not found”);else

printf(“Found at index “ , ptr – str);

ptr = mystrrchr(str , ‘C’);if(ptr == NULL)

printf(“Key Not found”);else

printf(“Found at index “ , ptr – str);}

Page 12: Implementation of c string functions

char * mystrcat(char *dest , const char *src)

{

int len = strlen(dest);

mystrcpy(dest + len , src);

return dest;

}

[email protected] 12

char *mystrncat(char *dest , const char *src,size_t n)

{

int len = strlen(dest);

mystrncpy(dest + len , src , n );

}

Page 13: Implementation of c string functions

char * mystrstr(char *str , char *key)

{

int len = strlen(key);

char *ptr;

while(1)

{

ptr = strchr(str , *key);

if(ptr == NULL)

return NULL;

int res = strncmp(ptr , key , len);

if(res != 0)

return ptr;

str = ptr + 1;

}

}

[email protected] 13

Main String :BANGALORE

Sub String to Search :GALGALALOT