Found 1401 Articles for C

C program to detect tokens in a C program

sudhir sharma
Updated on 07-Oct-2023 03:03:53

26K+ Views

Here, we will create a c program to detect tokens in a C program. This is called the lexical analysis phase of the compiler. The lexical analyzer is the part of the compiler that detects the token of the program and sends it to the syntax analyzer.Token is the smallest entity of the code, it is either a keyword, identifier, constant, string literal, symbol.Examples of different types of tokens in C.ExampleKeywords: for, if, include, etc Identifier: variables, functions, etc separators: ‘, ’, ‘;’, etc operators: ‘-’, ‘=’, ‘++’, etcProgram to detect tokens in a C program−Example Live Demo#include #include ... Read More

C program to demonstrate fork() and pipe()

sudhir sharma
Updated on 17-Jul-2020 12:49:55

2K+ Views

In this problem, we will demonstrate fork() and pipe(). Here we will create a C program for Linux that will concatenate two string, using 2 processes one will take input and send it to others which will concatenate the string with a predefined string and return the concatenated string.First lets recap fork() and pipe()fork() − it creates a child process, this child process ahs a new PID and PPID.pipe() is a Unix, Linux system call that is used for inter-process communication.Let’s take an example for understanding the problem, InputLearn programming Predefined string: at tutorialspointOutputLearn programming at tutorialspointExplanationP1 take input of ... Read More

C Program for Reverse a linked list

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

11K+ Views

In this problem, we are given a linked list. Our task is to create a program for reverse a linked list.The program will reverse the given linked list and return the reversed linked list.Linked List is a sequence of links with contains items. Each link contains a connection to another link.Example9 -> 32 -> 65 -> 10 -> 85 -> NULLReverse linked list is a linked list created to form a linked list by reversing the links of the list. The head node of the linked list will be the last node of the linked list and the last one ... Read More

C Program for Recursive Insertion Sort

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

3K+ 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

278 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

8K+ 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

194 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

227 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

239 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

Advertisements