C Articles - Page 75 of 134

Print the longest prefix of the given string which is also the suffix of the same string in C Program.

Sunidhi Bansal
Updated on 22-Aug-2019 08:36:54

1K+ Views

Given a string in which we have to check that the length of the longest prefix which is also a suffix of the string like there is a string “abcab” so here “ab” is of length 2 and is the longest substring with same prefix and suffix.ExampleInput: str[] = { “aabbccdaabbcc” } Output: 6 Input: abdab Output: 2If we will start the pointer from start and end of the string than they will get overlapped at some point so instead of doing that we will break the string from middle and start matching left and right string. If they are ... Read More

Print modified array after multiple array range increment operations in C Program.

Sunidhi Bansal
Updated on 22-Aug-2019 08:25:46

183 Views

Given an array arr[m] with m number of integers and n, which is the value to be added in an array and r queries are given with some start and end. For each query we have to add value n from the start till the end of the limit in an array.ExampleInput: arr[] = {1, 2, 3, 4, 5} query[] = { { 0, 3 }, { 1, 2 } } n = 2 Output: If we run above program then it will generate following output: Query1: { 3, 4, 5, 6, 5 } Query2: { 3, 6, 7, 6, ... Read More

Print lower triangular matrix pattern from given array in C Program.

Sunidhi Bansal
Updated on 22-Aug-2019 08:19:21

2K+ Views

Given with the matrix of n x n the task is to print that matrix of n x n in lower triangular pattern.Lower triangular matrix is a matrix which has elements below the principle diagonal including the principle diagonal elements and rest elements as zero.Let’s understand this with help of a diagram −Above the elements in green are the elements below the principle diagonal and the red elements are the elements above the principle diagonal which are set as zero.ExampleInput: matrix[3][3] = {    { 1, 2, 3 },    { 4, 5, 6 },    { 7, 8, 9 ... Read More

Print the matrix diagonally downwards in C Program.

Sunidhi Bansal
Updated on 22-Aug-2019 08:08:58

499 Views

Given with an array of size n x n and the task is to print the matrix elements of integer type diagonally downwards.Diagonally downwards means printing the array of any size of n x n in diagonally moving downward like in the figure given below −Firstly it will print 1 and then move to 2 print it and moves down to 4 diagonally and print it and so on.ExampleInput: Matrix [3][3] = {    { 1, 2, 3 },    { 4, 5, 6 },    { 7, 8, 9 }} Output: 1 2 4 3 5 7 6 8 ... Read More

Print the arranged positions of characters to make palindrome in C Program.

Sunidhi Bansal
Updated on 22-Aug-2019 07:59:41

226 Views

You are provided with a string str with some length n. Print the position of every element of the string so it can form a palindrome, else print a message “No palindrome” on screen.What is palindrome?Palindrome is a word, sequence of characters which reads same from the reverse or backward as from the forward manner, like MADAM, racecar.To find a sequence or a word is palindrome we generally store the reverse of a word in a separate string and compare both if they are same then the given word or sequence is palindrome. But in this question we have to ... Read More

Print steps to make a number in form of 2^X – 1 in C Program.

Sunidhi Bansal
Updated on 22-Aug-2019 07:49:02

165 Views

Given a number n, we have to print the steps to make the number as in form of 2^X-1 by using Xor operation.We should XOR the number with any 2^M-1, where M is chosen by you, at odd step. At even step increment the number by 1Keep performing the step until n becomes 2^X-1, and print all the stepsExampleInput: 22 Output:    Step 1 : Xor with 15    Step 2: Increase by 1    Step 3 : Xor with 7    Step 4: Increase by 1    Step 5 : Xor with 1 Input:7 Output: No Steps to be performedAlgorithmint ... Read More

Print the lexicographically smallest DFS of the graph starting from 1 in C Program.

Sunidhi Bansal
Updated on 22-Aug-2019 07:36:26

334 Views

We will be given a connected graph with N vertices and M edges. So we have to print the lexicographically smallest DFS of the graph starting from 1.Vertices should be numbered from 1 to NExampleInput: N = 5 M =5    edge(1, 4, arr)    edge(3, 4, arr)    edge(5, 4, arr)    edge(3, 2, arr)    edge(1, 5, arr)    edge(1, 2, arr)    edge(3, 5, arr)    edge(1, 3, arr) output: 1 2 3 4 5Instead of doing a normal DFS, first we will sort the edges associated with each vertex, so that in each turn only the ... Read More

Print the lexicographically smallest BFS of the graph starting from 1 in C Program.

Sunidhi Bansal
Updated on 22-Aug-2019 07:28:17

577 Views

We will be given a connected graph with N vertices M edges. So we have to print the lexicographically smallest BFS of the graph starting from 1.Lexicographically means in order starting from the given point till the end point is found.Vertices should be numbered from 1 to NExampleInput: N = 5 M = 5    edges(1, 4, arr)    edges(3, 4, arr)    edges(5, 4, arr)    edges(3, 2, arr)    edges(1, 5, arr)    Output: 1 4 3 2 5Instead of doing a normal BFS traversal with a simple queue on the graph, we can use a priority queue(min ... Read More

Print string of odd length in ‘X’ format in C Program.

Sunidhi Bansal
Updated on 22-Aug-2019 07:20:47

1K+ Views

Given with a string program must print the string in ‘X’ format. For reference, see the image given below.Here, one variable can be used to print from left right(“i”) and other variable can used to print from right to left(“j”) and we can take other variable k which is used for space calculation.Below is the C++ implementation of the algorithm given.AlgorithmSTART Step 1 ->Declare Function void print(string str, int len)    Loop For int i = 0 and i < len and i++       Set int j = len-1- i       Loop For int k = ... Read More

Print numbers in sequence using thread synchronization in C Program.

Sunidhi Bansal
Updated on 22-Aug-2019 07:15:18

7K+ Views

Given with the threads the program must print the thread based on their priorities starting from 0 to 10.What is a thread?Thread is lightweight process that runs inside a program. A simple program can contain n number of threads.Unlike java, multithreading is not supported by the language standards, POSIX Threads (Pthreads) is the standard used in multithreading in C/C++. C does not contain any built-in support for multithreaded applications. Instead, it relies entirely upon the operating system to provide this feature.How it works in our program?To use the thread functions we use header file #include. This header file will include ... Read More

Advertisements