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: