C Articles

Page 36 of 96

C Program for Matrix Chain Multiplication

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 13K+ Views

In this problem, we are given a sequence (array) of dimensions that define a chain of matrices. Our task is to create a C program for Matrix chain multiplication to find the minimum number of scalar multiplications required to multiply these matrices. The array of matrices will contain n elements, which define the dimensions of the matrices as arr[i-1] × arr[i] for the i-th matrix. Syntax int matrixChainOrder(int dimensions[], int n); Understanding the Problem Let's take an example to understand the problem − Input array[] = {3, 4, 5, 6} ...

Read More

C program to detect tokens in a C program

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 31K+ 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 can be a keyword, identifier, constant, string literal, or symbol. Syntax void detectTokens(char* sourceCode); Types of Tokens Examples of different types of tokens in C − Keywords: for, if, include, while, int, etc. Identifiers: variables, ...

Read More

C program to demonstrate fork() and pipe()

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 3K+ Views

In this problem, we will demonstrate fork() and pipe(). Here we will create a C program for Linux that will concatenate two strings, using 2 processes − one will take input and send it to another which will concatenate the string with a predefined string and return the concatenated string. Note: This program requires a Unix-like system (Linux/macOS) with support for fork() and pipe() system calls. It will not compile on Windows unless using WSL or similar environment. Fork() and Pipe() Overview fork() − creates a child process. This child process has a new PID ...

Read More

Program for Rabin-Karp Algorithm for Pattern Searching in C

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 653 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, which will find all the occurrences of the pattern in the text string. The Rabin-Karp algorithm is an efficient string matching algorithm that uses hashing to find patterns. It works by computing hash values for the pattern and text windows, comparing them first before doing character-by-character matching. Syntax void rabinKarpSearch(char pattern[], char text[]); // Where pattern is the string to search for // And text is the string to ...

Read More

C Program for KMP Algorithm for Pattern Searching

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 10K+ 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. The KMP (Knuth Morris Pratt) algorithm is an efficient string matching algorithm that preprocesses the pattern to avoid unnecessary comparisons. It uses a failure function to skip characters when a mismatch occurs. Syntax void KMPSearch(char* text, char* pattern); void computeLPSArray(char* pattern, int M, int* lps); How KMP Algorithm Works The KMP algorithm works in two phases ...

Read More

C Program for Anagram Substring Search

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 332 Views

In this problem, we are given two strings: one text of size n and another pattern of size m. Our task is to create a program for anagram substring search. Here, we have to find all occurrences of the pattern and all its permutations (anagrams) in the text. An anagram is a word formed by rearranging the letters of another word. Let's take an example to understand the problem − Input text = "xyztrwqyzxfg" pattern = "xyz" Output Found at index 0 Found at index 7 Algorithm ...

Read More

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

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 410 Views

In C programming, we can read and print a specific line from a file using file handling functions. This involves opening the file, reading through it line by line, and printing the desired line number. Syntax FILE *fopen(const char *filename, const char *mode); char *fgets(char *str, int n, FILE *stream); int fclose(FILE *stream); Method 1: Using Line Counter This approach reads the file line by line and uses a counter to track the current line number − Note: Create a text file named "text.txt" in the same directory with some content before ...

Read More

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

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 303 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 times 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. Syntax ...

Read More

Difference between scanf() and gets() in C

Nitin Sharma
Nitin Sharma
Updated on 15-Mar-2026 10K+ Views

In C programming, both scanf() and gets() functions are used to read input from the user. However, they handle input differently and have distinct characteristics that make them suitable for different scenarios. Syntax int scanf(const char *format, ...); char *gets(char *str); Important Note: The gets() function has been removed from the C11 standard due to security vulnerabilities. It is recommended to use fgets() instead. Key Differences Sr. No. Aspect scanf() Function gets() Function 1 Input Reading Reads input according to format specifiers and stops ...

Read More

Writing C/C++ code efficiently in Competitive programming

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 512 Views

In competitive programming, writing efficient C code is crucial for achieving better performance and rankings. Fast execution and optimal memory usage can make the difference between acceptance and time limit exceeded. Key Concepts Template − Code that works with different data types without rewriting Macro − Named code fragment that gets replaced during preprocessing Dynamic Arrays − Arrays that can resize during runtime Essential Optimization Techniques Fast Input/Output Methods Using scanf() and printf() instead of slower alternatives provides significant performance improvements ? #include int main() { ...

Read More
Showing 351–360 of 953 articles
« Prev 1 34 35 36 37 38 96 Next »
Advertisements