Posts

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...

Hash table implementation in C

               A hash table has an array of buckets. It has a key and value. Key is to find the position of the bucket. Value is to be stored on that location. It can be done by two ways as follows. ·        Using linked list ·        Linear probing Here, we use linked list to implement the hash table. Code: #include <stdio.h> #include <stdlib.h> #include <string.h> //The size of the hash table is defined as 4. #define SIZE 4 //create a node for hash table. it has a key, value and a pointer to point the next node struct h_Node {     int h_key;     int h_value;     struct h_Node* h_next; }; // create a hash table struct h_Node* hashTable[SIZE];   // Hash function to find the hash code int hashCode(int h_key) {     return h_key % SIZE; } // this function adds the ...