1: #include <stdio.h>2: #include <string.h>3:4: char* my_strcpy(char* dest,const char* src);5: char* my_strncpy(char* dest,const char* src, size_t num);6: char* my_strcat(char* dest,const char* src);7: char* my_strncat(char* dest,const char* src,size_t num);8: int my_strcmp(const char* str1, const char* str2);9: int my_strncmp(const char* str1, const char* str2, size_t num);10:11: main()12: {13:14: char arr1[] = "mithun";15: char arr2[] = "my name is mithun";16: char arr3[40] = "";17: char* temp = arr3;18: printf("%s \n",my_strncpy(arr1,arr2,6)?arr1:"dest size not adequate");19: printf("%s \n",my_strncpy(arr2,arr2,6)?arr2:"dest size not adequate");20: printf("%s \n",arr2);21: printf("%s \n",my_strncpy(arr1,arr2,7)?arr1:"dest size not adequate");22: printf("%s \n",arr1);23:24: while(*temp)25: printf("%p %c \n",temp,*temp++);26: printf("%p --> %c \n",temp,*temp);27:28: printf("%s \n",my_strcat(arr3,arr1));29: printf("%s \n",my_strcat(arr3," hi to all of you"));30: printf("%s \n",my_strncat(arr3," again hello, milo, bolo to all of you",7));31: printf("%s \n",my_strncat(arr3,"you",6));32: printf("%d \n",my_strcmp("abc","abc"));33: printf("%d \n",my_strcmp("abc","abcd"));34: printf("%d \n",my_strcmp("abcd","abc"));35: printf("%d \n",my_strcmp("abc","xyz"));36: printf("%d \n",my_strcmp("abc","abz"));37: printf("%d \n",my_strcmp("a","a"));38:39: printf("%d \n",my_strncmp("abc","axc",2));40: printf("%d \n",my_strncmp("abc","abc",3));41: printf("%d \n",my_strncmp("abc","abc",6));42:43: getchar();44: }45:46: int my_strncmp(const char* str1, const char* str2,size_t num)47: {48: while(*str1++ == *str2++ && num--)49: {50: if(!(*str1) && !(*str2) || !num)//both reached to NULL51: return 0;52: }53: str1--;54: str2--;55: if(*str1 > *str2)56: return 1;57: else if(*str1 < *str2)58: return -1;59: }60:61: int my_strcmp(const char* str1, const char* str2)62: {63: while(*str1++ == *str2++)64: {65: if(!(*str1) && !(*str2))//both reached to NULL66: return 0;67: }68: str1--;69: str2--;70: if(*str1 > *str2)71: return 1;72: else if(*str1 < *str2)73: return -1;74: }75:76: char* my_strcat(char* dest,const char* src)77: {78: char* temp = dest;79: while(*dest != '\0'){dest++;}80: while(*dest++ = *src++);81: return temp;82:83: }84:85: char* my_strncat(char* dest,const char* src,size_t num)86: {87: char* temp = dest;88: while(*dest != '\0'){dest++;}89:90: while(num-- && *src)91: *dest++ = *src++;92: *dest = '\0';93:94: return temp;95:96: }97:98: char* my_strncpy(char* dest,const char* src, size_t num)99: {100: if(dest == src)101: return NULL;102:103: char* temp = dest;104: while(num--)105: {106: if(*dest == '\0')107: return NULL;108: *dest++ = *src++;109: }110: return temp;111: }112:113: char* my_strcpy(char* dest,const char* src)114: {115: char *temp = dest;116: while(*dest++ = *src++);117: return temp;118: }119:
Wednesday, July 7, 2010
some string manipulation functions in C
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment