String Copy
void str_cpy( char *source, char * dest)
{
*source = 'a';
while(*source != '\0')
*dest++ = *source++;
*dest='\0';
}
Call like this
char dst_str[100];
str_cpy(src_str, dst_str);
printf("dst_str:%s\n", dst_str);
Reverse a string....
void reverse_str(const char* str, char **revstr)
{
int len = strlen(str);
if(len >0)
{
revstr[0]= new char[len];
revstr[0][len--]= '\0';
while(*str != 0)
{
revstr[0][len--]=*str++;
}
}
}
Call this function like
char *rev = NULL;
char *str= "ankur";
reverse_str(str, &rev);
Reverse using recursion
void recursion_reverse( char *str)
{
if(*(str) != 0)
recursion_reverse(++str);
else
return;
cout<< *--str;
}
Copy Constructor and Assignment Operator Example
class My_Array
{
public:
int *Array;
int count;
My_Array(){}
My_Array(int cnt):count(cnt), Array(new int[cnt]){}
My_Array(const My_Array & other):count(other.count), Array(new int[other.count])
{
std::copy(other.Array,other.Array+other.count,Array);
}
My_Array & operator=(const My_Array& other)
{
if (this != &other)
{
int *new_array= new int[other.count];
std::copy(other.Array,other.Array+other.count, new_array);
Array= new_array;
count= other.count;
}
return *this;
}
~My_Array()
{
delete[] this->Array;
}
};
Conversion constructor :
class Base
{
public:
int i, j;
Base(){}
Base(int a){i = a;}
};
int _tmain(int argc, _TCHAR* argv[])
{
Base b=10;
printf("i = %d", b.i);
return 0;
}
Check wheather string given is palindrome or not
bool palindrome(char *str)
{
int length=0;
char *temp= str;
while ( *temp != '\0' )
{ temp++; length++; }
int begin = 0;
int end = length - 1;
int middle = length/2;
for(begin = 0 ; begin < middle ; begin++ )
{
if ( *(str+begin) != *(str+end) )
{
printf("Not a palindrome.\n");
break;
}
end--;
}
if( begin == middle )
return true;
return false;
}
void str_cpy( char *source, char * dest)
{
*source = 'a';
while(*source != '\0')
*dest++ = *source++;
*dest='\0';
}
Call like this
char dst_str[100];
str_cpy(src_str, dst_str);
printf("dst_str:%s\n", dst_str);
Reverse a string....
void reverse_str(const char* str, char **revstr)
{
int len = strlen(str);
if(len >0)
{
revstr[0]= new char[len];
revstr[0][len--]= '\0';
while(*str != 0)
{
revstr[0][len--]=*str++;
}
}
}
Call this function like
char *rev = NULL;
char *str= "ankur";
reverse_str(str, &rev);
Reverse using recursion
void recursion_reverse( char *str)
{
if(*(str) != 0)
recursion_reverse(++str);
else
return;
cout<< *--str;
}
Copy Constructor and Assignment Operator Example
class My_Array
{
public:
int *Array;
int count;
My_Array(){}
My_Array(int cnt):count(cnt), Array(new int[cnt]){}
My_Array(const My_Array & other):count(other.count), Array(new int[other.count])
{
std::copy(other.Array,other.Array+other.count,Array);
}
My_Array & operator=(const My_Array& other)
{
if (this != &other)
{
int *new_array= new int[other.count];
std::copy(other.Array,other.Array+other.count, new_array);
Array= new_array;
count= other.count;
}
return *this;
}
~My_Array()
{
delete[] this->Array;
}
};
Conversion constructor :
class Base
{
public:
int i, j;
Base(){}
Base(int a){i = a;}
};
int _tmain(int argc, _TCHAR* argv[])
{
Base b=10;
printf("i = %d", b.i);
return 0;
}
Check wheather string given is palindrome or not
bool palindrome(char *str)
{
int length=0;
char *temp= str;
while ( *temp != '\0' )
{ temp++; length++; }
int begin = 0;
int end = length - 1;
int middle = length/2;
for(begin = 0 ; begin < middle ; begin++ )
{
if ( *(str+begin) != *(str+end) )
{
printf("Not a palindrome.\n");
break;
}
end--;
}
if( begin == middle )
return true;
return false;
}