1: #include <stdio.h>2: #include <stdlib.h>3:4: int mystrrev(char * str,int size);5:6: int main()7: {8: char name1[7]="Mithun";9: char name2[6]="Mithu";10: mystrrev(&name1,6);11: mystrrev(&name2,5);12:13: printf("%s \n",name1);14: printf("%s \n",name2);15:16: }17:18: int mystrrev(char * str,int size)19: {20: int pivot = size/2;21: char* s = str;22: char* e = str+size-1;23: printf("%d, %c \n", *s,*e);24:25: while(*str)26: {27: printf("%c , %d \n",*str,*str);28: str++;29: }30:31: while(pivot--)32: {33: *s = (*s)+(*e);34: *e = (*s)-(*e);35: *s = (*s)-(*e);36:37: s++;38: e--;39: }40: }
Showing posts with label C. Show all posts
Showing posts with label C. Show all posts
Tuesday, July 26, 2011
String reversal in C
Wednesday, July 7, 2010
some string manipulation functions in C
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:
Sunday, July 4, 2010
Pointers to pointers,and arrays and strings I
- the value stored (rvalue) and the address (lvalue) at which the value is stored and the name(variable name) which is mapped(in symbol table) to this address
- variable for storing lvalue i.e. address is “pointer”
- address of operator is ‘&’ , to get the address of any variable
- dereferencing operator is ‘*’ , to get the value which the pointer variable is pointing
- pointer arithmetic involves modifying address
- so for a char* cptr saying cptr + 1, will increment the address by one (sizeof char)
- for a int* iptr saying iptr + 1, will increment the address by four (sizeof int)
Monday, June 28, 2010
Find Zeros and One
1: #include <stdio.h>2:3: main()4: {5: int val = 1;
6: int data = 10;
7: int OneCount = 0;
8: int ZeroCount = 0;
9: while(val)
10: {11: if(data & val)
12: OneCount++;13: else
14: ZeroCount++;15:16: val = val << 1;17: }18: printf("Number of ones %d \n",OneCount);19: printf("Number of zeroes %d \n",ZeroCount);20: getchar();
21: }
malloc(0), why is it working ?
1: #include <stdio.h>2: #include <malloc.h>3:4: char* reverse(char *data);5: void my_Strcpy(char* dest,char* source);6: main()7: {8: char* p_Name = "Mithun P";9: char a_Name[] = "Mithun P";10: char *pd_Name = malloc(0);11: my_Strcpy(pd_Name,"Mithun P");12:13: //printf("reverse of p_Name is %s \n",reverse(p_Name));14: printf("reverse of a_Name is %s \n",reverse(a_Name));15: printf("reverse of pd_Name is %s \n",reverse(pd_Name));16:17: getchar();18: }19:
Subscribe to:
Posts (Atom)