Posts

Stack implementation using arrays in C

               Stack is a linear data structure which stores data. It uses the LIFO(Last In First Out) principle. It has following functions. ‘push()’ – it inserts the data into stack. ‘pop()’ – it deletes the data from the stack. Program implementation: ·        This program includes the built-in header file . It defines the maximum size as 50. ·        Stack and top values are initialised. ·        To insert the value, check top value with maximum size to check for overflow . If it is overflow, the message is displayed. Otherwise, the data is inserted. ·        To delete the value, check for top value. If it is -1, then it is called underflow . Otherwise, the data is removed from the top. ·        Finally, data is displayed using print (). C Code : #include < stdio.h > ...

C program to count Vowels, Consonants and digits in a String

              String is a collection of characters. This program counts the vowels present in the input string, consonants appear in the input and numbers available in the string. Let us implement it in C language .               This program reads the input string from the user using fgets() function . Using a for loop , each character is separated. Each character is checked whether it is an alphabet or a digit . If it is a digit, the count of digit variable is increased by one. If it is an alphabet, the alphabet is converted as lowercase. It is compared with vowels. If it a vowel, vowel count is increased. Otherwise, consonant count is increased. Finally, each count is displayed. C Code: #include <stdio.h> #include <ctype.h>   //It has built-in function to check the characters and numbers int main() {     char s[100]; ...

Reverse a string in C

               Reverse a string deal with reverse each character of the entire string. It can be done by user defined function or built-in function. First, we implement it using the user defined function. It uses reverse_String() for implementing the function. 👉C Code: //include the header files #include <stdio.h> #include <string.h> //a user defined function ‘reverse_String()’ is defined here. //starting and ending point and temporary variables are created. //each character is read and reversed using temp variable. void reverse_String(char s[]) {     int start = 0;     int end = strlen(s) - 1;     char temp;     while (start < end) {         // Exchange the characters         temp = s[start];         s[start] = s[end];   ...

Selection Sort implementation in C

              Selection sort is one of the simple algorithm which deals with comparison. It finds the minimum element and makes that element to sit in the front. It keeps on the same process for entire elements. Let us the discuss the process. Selection sort:               It makes the array into two parts. One is sorted and other one is unsorted. First, the sorted part doesn’t have a value. The unsorted part has the entire elements. The process starts now. Each iteration, repeat the process. The minimum element is searched in the unsorted part. Once, it found, swap it with the first element of the unsorted side. Continue this until last element. C implementation: #include <stdio.h>   void selection_Sort(int a[], int n) {     int i, j, min_Index, t;     for (i = 0; i < n-1; i++) {      ...

Bubble sort implementation in C

       Sorting is the process of arranging elements in order. Bubble sort is one of the basic sorting methods which compares adjacent elements. It swaps if the elements are not sorted. Otherwise, it keeps the array elements and moves to the next adjacent elements. 👉Implementation: This implementation starts with including the built-in header file. Next, a function to swap the elements is defined. It uses a temporary variable to exchange the values. The function ‘bubble_Sort()’ gets the input as array and its size. It checks the adjacent elements value. If these are sorted already, it keeps the values. Else, it swaps the values. ‘main()’ function gets the array size and elements from the user. It calls the ‘bubble_Sort()’ function and gets the output. Finally,it prints the sorted array. ##C Code: #include <stdio.h> // Function to swap two elements void b_swap(int *x, int *y) {     int temp = *x;     *x...

Binary search implementation in C

              Searching is a process of finding an element from a group of elements. Binary search is one of the efficient search algorithms . Let us implement it in C language . Implementation: This algorithm follows a method as follows. First, find the middle element. Check the element to be searched with middle element. If it is small, search the element in the left child of the array. If it is large, go for right side. Repeat this process to find the exact position. C Code: #include <stdio.h> int binarySearch(int a[], int n, int key) {     int low = 0, high = n - 1;     while (low <= high) {         int mid = (low + high) / 2;         if (a[mid] == key)             return mid;   // yes ,it is Found         else if (...

How to insert and delete elements in an array using C

       Array is one of the important data structures widely used in programming. This blog post includes the insertion and deletion of elements in array. Insertion:               An array is declared and inputs are read from the user. To insert an element, the position and data are the inputs. Using loop , we insert the element and displayed the output after insertion. C Program: #include <stdio.h> int main() {     int a[10];     int n,i,j,k;     printf("Enter the number of elements:");     scanf("%d",&n);     printf("Enter the elements:");     for(i=0;i<n;i++)     {         scanf("%d",&a[i]);     }     printf("Enter the position to insert an element:");     scanf("%d...