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

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: 

Sunday, June 27, 2010

char[] and char*

  1. char data[] = “data”;
    1. data: [D] [A] [T] [A] [\0]
  2. so , char data[] is a array holding the data\0
  3. the individual characters can be modified
  4. but data will always refer to the same storage

 

  1. char *data = “data”;
    1. [data] –> [D] [A] [T] [A] [\0]
  2. so, char *data is a pointing to a string constant
  3. the pointer can be modified to point to other location
  4. modifying the string constant gives undefined result (SIGSEGV in gcc)

Deep Copy

  1. needed when the class has dynamically allocated members
  2. always remember the explicit this pointer of c++
  3. copy constructor is implemented (algorithm)
    1. allocate memory for dynamically allocated members
    2. copy the data from const_argument.member to this.member
  4. assignment operator is implemented (algorithm)
    1. check for self assignment
    2. delete memory for this.member
    3. allocate new memory for this.member
    4. copy the data from const_argument.member to this.member
    5. return this
  5. when copy constructor is called
    1. doing initialization with objects, like
      1. class1 object1(“value”);
      2. class1 object2 = object1; //object to object assignment
    2. passing a object by value
    3. returning a object by value
  6. when assignment operator is called
    1. assigning a existing object(object with memory for its data members) a new object