Posts

Convert Lower case string to Upper case string

Image
# Program to Convert Lower case string to Upper case string: As we know that everything in the computer is in Binary Form, So for characters, we have standardized numeric codes and these codes are called ASCII ( American Standard Code for Information Interchange ) codes and UniCodes. The ASCII codes range of Upper case A to Z characters are 65 to 90 and lower case a to z are 97 to 122. So the difference between each upper case character and lower case character is 97 - 65 = 32, So If we have a Lower case character and we want to convert it to Upper case character, then we have to subtract 32 from it. and If we have Upper case character and we have to convert it to Lower case then we have to add 32 to it. # include < iostream > using namespace std ; /* * upper case A-Z ASCII value are 65-90 and lower case a-z values are 97-122 * So the difference between each upper case letter and lower case letter is * 97 - 65 = 32, So If we have ...

Finding the length of a string in c and c++

Image
# Finding the length of a string in c and c++: # What is a string: A string is a set of characters and string is terminated by a '\0' character. This character is used to know the termination of the string. So to find the length of the above string. We have to traverse this string till we find the '\0'. Please check the below program for more details: # include < iostream > using namespace std ; int main () {      /*   * No need to append '\0' as this is   * added by Compiler automatically as we use "".   */ char * s = "welcome" ; int iLength = 0 ; while ( s [ iLength ] != '\0' ) { ++ iLength ; } cout << "The Length of the string is: " << iLength << endl ; return 0 ; } # Output of the program: Note: Full program can be downloaded from the link below: https://github.com/VChaudhary854/LearnD...

Insert an element in a sorted array

Image
# Insert an element in a sorted array: To insert an element in a sorted array, We have to start comparing the key element with the last element of the array and if the last element is greater than the key element then shift the last element to its next index. This procedure we have to follow until we find the element in array equal to or smaller than the key element. like if we need to insert 5 in below array: then we have to take a variable "i" and "i" will be initialized by array length - 1 So i = A.length - 1 = 5 - 1 = 4; So 5 is compared with A[4], as A[4] is 8 so 8 > 5 , then we have to shift A[i] to A[i+1], A[i+1] = A[i] // A[5] = A[4] // A[5] = 8  and A[4] is empty. and need to do --i, so i will be 3 now now again we have to compare 5 with A[3], as A[3] is 7 and 7 > 5 , So again we have to shift it to the next index. A[i+1] = A[i]; // A[4] = A[3] // A[4] = 7  and A[3] is empty. --i; So i will be 2 now now again we have to compare 5 with A[2], as A[2] is...

Display Operation of Array

# Display Operation Of Array: The Display operation of the array is nothing but the display/print the elements of Array on the console or any output stream. To display the elements of an Array, We need to iterate it and print the elements. Below is the program to display the elements of Array. void display ( struct Array objArray ) { cout << "Elements in Array are: " << endl ; for ( int i = 0 ; i < objArray . iLength ; ++ i ) { cout << objArray . A [ i ] << " " << endl ; } } int main () { Array objArr = {{ 2 , 4 , 6 , 7 , 8 }, 5 }; display ( objArr ); return 0 ; }

Binary Search (using while loop and using recursion)

Image
# Binary Search: Binary Search   is the searching technique, which works on the divide and conquers algorithm and it works only for sorted arrays.  Binary Search is a searching algorithm. In each step, the algorithm compares the input element x with the value of the middle element in the array. If the values match, return the index of the middle. Otherwise, if x is less than the middle element, then the algorithm recurs for the left side of the middle element, else recurs for the right side of the middle element.  Suppose below is the array in which we want to search the key 53. For searching any element in the array, We need below three variables: Low // Contains the lower index of Array Mid // Contains the Mid Index of Array High // Contains the Higher Index of Array So initially Low will be 0 and High will be (length - 1) length of below array is 21(number of items in the array), So High will be 20. Low = 0; High = 20; // length - 1 = 21 - 1 Mid = 10; //((Low + High) /...

Move to Front/Head Method of Linear search:

# Move to Front/Head Method of Linear search: In this method of searching, If the key is found then it is swapped with the first element of the array. The idea behind this method is if the item will be searched again, then the item will search very fast. Below is the program for this method. # include < iostream > using namespace std ; /** * @brief The Array struct: Structure Which Contains array of size 10 and arrays * Length (number of items it contains.) */ struct Array { int A [ 10 ]; int iLength ; }; /** * @brief swap: This function swaps the items. * @param pFirst: First item. * @param pSecond: Second item */ void swap ( int * pFirst , int * pSecond ) { int iTemp ; iTemp = * pFirst ; * pFirst = * pSecond ; * pSecond = iTemp ; } /** * @brief moveToHeadLinearSearch: This methods search the Key in the array passed in first param, * and if ite...