sudhir sharma

sudhir sharma

975 Articles Published

Articles by sudhir sharma

Page 11 of 98

Program for Rabin-Karp Algorithm for Pattern Searching in C

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 607 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 327 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 406 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 296 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

Writing C/C++ code efficiently in Competitive programming

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 501 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

Writing OS Independent Code in C/C++

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

Writing OS-independent code in C allows programs to run across different operating systems without modification. This is achieved using preprocessor macros that detect the target platform at compile time. Syntax #ifdef MACRO_NAME // OS-specific code #elif defined(ANOTHER_MACRO) // Alternative OS code #else // Default code #endif Common OS Detection Macros GCC and other C compilers define platform-specific macros automatically − _WIN32 − Defined for 32-bit and 64-bit Windows _WIN64 − Defined only for 64-bit Windows __unix__ − Defined for Unix-like ...

Read More

Predefined Identifier __func__ in C

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

The __func__ is a predefined identifier in C that provides the name of the current function. It was introduced in C99 standard and is automatically available in every function without any declaration. Syntax __func__ The __func__ identifier is implicitly declared as if the following declaration appears at the beginning of each function − static const char __func__[] = "function-name"; Example 1: Basic Usage Here's a simple example showing how __func__ returns the current function name − #include void function1(void) { printf("Current function: ...

Read More

Print * in place of characters for reading passwords in C

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

In C programming, when handling passwords, it's important to hide the actual characters from being displayed on screen for security reasons. This involves replacing each character of the password with an asterisk (*) symbol. Let's take an example to understand the problem − Input: password Output: ******** Syntax for(int i = 0; i < strlen(password); i++){ printf("*"); } Example 1: Using Predefined Password String The below program demonstrates how to replace each character of a password string with asterisks ? #include #include ...

Read More

Print 1 2 3 infinitely using threads in C

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

In C programming, we can print the sequence "1 2 3" infinitely using multiple threads with proper synchronization. This demonstrates thread coordination using mutexes and condition variables to ensure orderly execution. Syntax pthread_create(&thread_id, NULL, thread_function, argument); pthread_mutex_lock(&mutex); pthread_cond_wait(&condition, &mutex); pthread_cond_signal(&condition); pthread_mutex_unlock(&mutex); Installation: On Linux systems, compile with: gcc filename.c -lpthread Example This program creates three threads that print numbers 1, 2, and 3 in sequence infinitely. Each thread waits for its turn using condition variables − #include #include pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER; pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER; ...

Read More
Showing 101–110 of 975 articles
« Prev 1 9 10 11 12 13 98 Next »
Advertisements