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 NULL
 51:     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 NULL
 66:     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

  1. 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
  2. variable for storing lvalue i.e. address is “pointer”
  3. address of operator is ‘&’ , to get the address of any variable
  4. dereferencing operator is ‘*’ , to get the value which the pointer variable is pointing
  5. pointer arithmetic involves modifying address 
    1. so for a char* cptr saying cptr + 1, will increment the address by one (sizeof char)
    2. for a int* iptr saying iptr + 1, will increment the address by four (sizeof int) 

Friday, July 2, 2010

A sip of SIP

  1. Session Initiation Protocol (application layer protocol)
  2. communication session between two endpoints(single entity in the networked cloud which wants to initiate the session)
  3. how to start talking (transfer voice data between), how to initiate, what are the things to agree upon
    1. some central entity to coordinate
    2. willingness to send and receive
    3. agreeing on same protocol
    4. knowledge of the location of each other (location can change at run time)
    5. ability to inform each other of some change
    6. some quality with regards to the data (voice,text,video) packets
    7. subscription to services, reception of change notification