Found 1339 Articles for C

C Program for Recursive Insertion Sort

sudhir sharma
Updated on 17-Jul-2020 12:38:47

4K+ Views

Insertion sort is a sorting algorithm which is an in-place comparison-based algorithm.The algorithm works by place element in their position in the sorted sub-array i.e. the sub-array preceding the element which is a sorted sub-array.AlgorithmStep1 − loop from 1 to n-1 and do −Step2.1 − select element at position i, array[i].Step2.2 − insert the element in its position in the sorted sub-array array[0] to arr[i].Let’s take an example to understand the algorithmArray = [34, 7, 12, 90, 51]For i = 1, arr[1] = 7, placing in its positon in subarray arr[0] - arr[1].[7, 34, 12, 90, 51]For i = 2, ... Read More

Program for Rabin-Karp Algorithm for Pattern Searching in C

sudhir sharma
Updated on 17-Jul-2020 12:36:21

408 Views

In this problem, we are given two strings a text and a pattern. Our task is to create a program for Rabin-Karp algorithm for pattern search, it will find all the occurrences of pattern in text string.Here, we have to find all the occurrences of the pattern in the text.Let’s take an example to understand the problem, Inputtext = “xyztrwqxyzfg” pattern = “xyz”OutputFound at index 0 Found at index 7Here, we will discuss the solution of the problem using the Rabin-Karp algorithm. In this algorithm, we take a window of the size of the pattern in the string and slide ... Read More

C Program for KMP Algorithm for Pattern Searching

sudhir sharma
Updated on 17-Jul-2020 12:32:15

9K+ Views

In this problem, we are given two strings a text and a pattern. Our task is to create a program for KMP algorithm for pattern search, it will find all the occurrences of pattern in text string.Here, we have to find all the occurrences of patterns in the text.Let’s take an example to understand the problem, Inputtext = “xyztrwqxyzfg” pattern = “xyz”OutputFound at index 0 Found at index 7Here, we will discuss the solution to the problem using KMP (Knuth Morris Pratt) pattern searching algorithm, it will use a preprocessing string of the pattern which will be used for matching ... Read More

C Program for Iterative Merge Sort

sudhir sharma
Updated on 17-Jul-2020 12:27:30

1K+ Views

Merge sort what is a sorting algorithm based on the divide and conquer technique. the time complexity of merge sort is O(n log n). The algorithm first divides the array into equal halves and then merges them in a certain manner.Iterative merge sortIn iterative merge sort, we will divide the elements into equal halves using a recursive approach and then merge them back as a sorted array using the iterative approach.Program for iterative Merge Sort/* Recursive C program for merge sort */Example Live Demo#include #include void merge(int arr[], int l, int m, int r) {    int i, j, k;   ... Read More

C Program for Anagram Substring Search

sudhir sharma
Updated on 17-Jul-2020 12:24:10

275 Views

In this problem, we are given two string one text of size n and other a pattern of size m. Our task is to create a program for Anagram substring search.Here, we have to find all the occurrence of pattern and all its permutations (anagrams) in the text.Let’s take an example to understand the problem, Inputtext = “xyztrwqyzxfg” pattern = “xyz”OutputFound at index 0 Found at index 7To solve this problem, we will have to use an algorithm similar to the Rabin Karp algorithm which is used to check for anagram occurrence by adding the ASCII values of all characters ... Read More

Write a program that does not terminate when Ctrl+C is pressed in C

sudhir sharma
Updated on 17-Jul-2020 11:12:32

289 Views

In this problem, we have to create a program that does not terminate when ctrl+C is pressed. Instead, it prints“Ctrl + C cannot terminate the program”.For this, we can use signal handling. The signal SIGINT is created on pressing ctrl+c. To solve this problem, we will catch this signal and handle it.Program to show the implementation of our solution,Example#include #include void signalHandle(int sig_num) {    signal(SIGINT, signalHandle);    printf(" Ctrl + C cannot terminate the program");    fflush(stdout); } int main (){    signal(SIGINT, signalHandle);    while(!0)    return 0; }OutputCtrl + C cannot terminate the program

Write a bash script to print a particular line from a file in C

sudhir sharma
Updated on 17-Jul-2020 10:50:06

355 Views

In this program, we are given a file name text.txt. Our task is to print a particular line from the file.For this there are multiple methods in bash script, they are awk, sed, head.Syntax$> awk ‘{if(NR==LINE_NUMBER) print $0}’ filename $> sed -n LINE_NUMBERp filename $head -n LineNumber filename | tail - n + LINE_NUMBERCode to print a specific line in bash programming from file text.txt.Using awk$> awk ‘{if(NR==5) print $0}’ text.txtUsing sed$>sed -n 5p text.txtUsing head$head -n 5 filename | tail - n + 5

Write a program to print ‘Tutorials Point’ without using a semicolon in C

sudhir sharma
Updated on 15-Jul-2020 06:40:59

230 Views

In this problem, we have to write a program that will print ‘Tutorials Point ’ without using a semicolon.We all know that to end a statement in c semicolon is necessary. And print statement will be executed when a semicolon is added at the end.So, for printing ‘Tutorials point’ without a semicolon, we will have to first learn about the printf method in c. in actually returns an integer which is the count of total number of characters that are required to be printed.Syntaxint printf(constant char *format, ...)The method can accept n number of arguments. The first will be the ... Read More

Write a C program to print ‘ABCD’ repeatedly without using loop, recursion and any control structure

sudhir sharma
Updated on 15-Jul-2020 06:31:02

239 Views

In this problem, we have to write a program in c that will print a string ‘ABCD’ repeatedly without using loop, recursion and any control structure.So, we will have to call or run the same block of code infinite time but without using loop, recursion or control structure which are the most common methods to perform the task. For this, we will run the same program multiple times instead of looping. This will perform our task within the given constraints. The system() method can be employed inside the code that will call the program infinite times.We will pass the file ... Read More

Difference between scanf() and gets() in C

Nitin Sharma
Updated on 09-Jun-2020 08:53:21

10K+ Views

In C language both scanf() and gets() functions are defined to get input from external source and pass to system as input. Now there is some characteristics difference between both the functions.Following are the important differences between scanf() and gets() in C −Sr. No.Keyscanf() functiongets() function1DefinitionThe scanf() function can read input from keyboard and stores them according to the given format specifier. It reads the input till encountering a whitespace, newline or EOF.On other hand gets() function is used to receive input from the keyboard till it encounters a newline or EOF. The whitespace is considered as a part of ... Read More

Advertisements