Monday, April 24, 2017

C ++ code to find if a string is a palindrome

A string is called a palindrome if it reads the same if they read both forward and backward. For eg:



All the above words spell the same when read forward and backward and are called as palindromes. The following code takes a input string and finds you whether the string is a palindrome or not.

To find if string is a palindrome or not we will start comparing the characters using two pointers, one titrating from the beginning and other from the end of the string. At each step we will increment the pointer that starts from the beginning by one character and decrementing the pointer that starts from the end by one character. At any step if the comparison returns a false that is characters don't match then we can conclude that the sting is not a palindrome.

The following figure shows the same steps for finding if the string CIVIC is a palindrome or not.



The pointer passed to the function , str, will be pointing to the beginning of the string. To get a new pointer pointing to the end of string, we will assign the value of new pointer to



where len is the length of the string. We do len-1 to skip comparing the null character.

We can start comparing the rev and st, and only len/2 comparisons are enough cause once we reach the middle of the string we would have covered the whole string as shown the example above.

Thus the full code in c++ for finding out if a string is palindrome or not is

 
#include&ltiostream&gt 
#include&ltcstring&gt 
using namespace std;

void palindrome(char *st){
int len=strlen(st);
cout << len << "\n";
bool palin=1;
char *rev;
rev=(st+(len-1)); 
for(int i=0;i&ltlen/2;i++){
	if(*(st++) != *(rev--)) {
		palin=0;
		break;
		}
}
if(palin){
	cout <<"The string is a palindrome\n";
}
else {
	cout <<"The string is not a palindrome\n";
}
	
}


int main() {
char str[20];
cout << "Enter the string to be checked \n";
cin >> str; 
if(strlen(str)>10) {
	cout <<"String length excedded\n";
	return -1;
}
palindrome(str);
return 0;
}

Saturday, April 15, 2017

Need for copy constructor...

The following examples try to bring out the need for having a copy constructor. With out a copy constructor if we allocate on object to other, i.e. object1=object2, the compiler internally does a bitwise copy,which works fine as long as there are no pointers present in the member variables of the object. A bit wise copy ends up copying the pointer too as it is, making pointers of both the objects point ot the same memory location.

This is depicted in the example below.



Output :



We can clearly see in the output above that the bar1 variable in both the objects is pointing to the same memory location so even when we modify only the variable in one object, the change is reflected in other object too which should no be the case as the two subjects are not related to each other.

The following example illustrates the same with the use of copy constructor.



Output:



In the above output it is clearly visible that the two objects have bar1 pointing to different memory location and changing the value in one object does not affect the other.

Friday, March 31, 2017

C plus plus program to extract substring of a given length

The following is a program to extract substring from a given string. The substring will be chosen number of charachters strarting from the first character.

The user will be asked to enter a string, and then the number of characters that need to be extracted from it.

The user entered length should not be greater than the length of the original string, thus the string length and requested length are compared and the program stops if requested length is greater than the original string length.



The value of the original string starting from the first character, till the requested length is copied into the new substring. The substring is appended with a null character to indicate end of string.



Full program

#include<iostream>
#include<cstring>
using namespace std ;

int main() { 
int i=0;
char *str,*substr; 
str = new char;
substr = new char;
cout << "Enter  a string,without space, less than 10 characters\n";
cin >> str;
cout << "Enter the number of characters needed to be extracted\n";
int extractLen;
cin >> extractLen;
if(extractLen > strlen(str)) {
 cout << "Can not extract longer than original string\n";
 return -1;
 }
for(i=0;i<extractLen;i++) {
 *(substr+i)=*(str+i);
 }
 *(substr+i) = '\0';

cout << "The substring extracted was \n" << substr<<"\n";
return 0;
}



Save,compile and execute the code.


Thursday, March 30, 2017

C plus plus Program to print the ASCII values of all the alphabets

The following program will print the ASCII codes for all the alphabets in lower as well as in upper case



Save, compile and execute


C plus plus program to change case of an alphabet without using library function.

All characters on the keyboard are given their own ASCII values. The list of ASCII values can be found at.ASCII Values

As we can see the ASCII values of the lower case alphabets and the upper case alphabets are different. The following a program which will convert any characters entered into its other case, that is if the entered character is lower case it will convert it to corresponding upper case and vice versa.

The ascii values of lower and upper case alphabets differt by a value of 32.



To find if the character is lower or upper case :

If the ASCII value of the entered alphabets lies between 65 and 90, its an upper case alphabet and we need to add 32 to get the corresponding lower case ASCII.



If the ASCII value of the entered alphabets lies between 97 and 122, its a lower case alphabet and we need to subtract 32 to get the corresponding upper case ASCII.



For any other ASCII values, it would not be an alphabet and there is no conversion needed.

The above steps are implemented in the function change_case function in the following program.



Save the program as change_case.cpp, compile and execute.


Sunday, March 19, 2017

Program in C plus plus to swap two numbers with out a third variable

Give two numbers of we want to swap them, the commonly used method is to swap them using a third variable as shown below.


But the use of third variable is not really needed and some times not recommended as it leads to unnecessary memory usage. We can swap the two numbers using the bitwise XOR operation too as shown below.



Example:



The same program implemented in c plus plus below. The compilation and output are shown as run in Linux terminal using g++ compiler.

save the above program as swap_with_out_temp.cpp Compile and Execute :

Output: