Replace Spaces in a String with a Specific Character in Java

AmitDiwan
Updated on 30-Mar-2022 05:49:41

1K+ Views

In this article, we will understand how to replace the spaces of a string with a specific character. String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”).Below is a demonstration of the same −Suppose our input is −Input string: Java Program is fun to learn Input character: $The desired output would be −The string after replacing spaces with given character is: Java$Program$is$fun$to$learnAlgorithmStep 1 - START Step 2 - Declare a string namely input_string, a char namely input_character. Step 3 - Define the values. Step 4 - Using the function replace(), replace the ... Read More

Join Two Lists in Java

AmitDiwan
Updated on 29-Mar-2022 13:50:24

344 Views

In this article, we will understand how to join two lists. The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.Below is a demonstration of the same −Suppose our input is −First list is defined as: [Java, Python] Second list: [Mysql, Redshift]The desired output would be −The list after joining two strings: [Java, Python, Mysql, Redshift]AlgorithmStep 1 - START Step 2 - Declare two lists ... Read More

Remove a Sublist from a List in Java

AmitDiwan
Updated on 29-Mar-2022 12:28:04

424 Views

In this article, we will understand how to remove a sub-list from a list. A list is an ordered collection that allows us to store and access elements sequentially. It contains the index-based methods to insert, update, delete and search the elements. It can also have the duplicate elements.Below is a demonstration of the same −Suppose our input is −Input list: [Java, Programming, Is, Fun]The desired output would be −The list after removing a sublist is: [Java, Programming]AlgorithmStep 1 - START Step 2 - Declare an AbstractList namely input_list. Step 3 - Add the values to the list. Step 4 ... Read More

C++ Code to Count Days to Complete Reading a Book

Arnab Chakraborty
Updated on 29-Mar-2022 12:18:41

319 Views

Suppose we have an array A with n elements and have another value t. On ith day Amal spends A[i] seconds in work. In the free time he reads a book. The entire book will take t seconds to complete. We have to find how many days he will need to read the complete book.So, if the input is like A = [86400, 86398]; t = 2, then the output will be 2, because one day has 86400 seconds, and first day is totally blocked. On second day he will get 2 seconds to complete the book.StepsTo solve this, we ... Read More

Create Random Strings in Java

AmitDiwan
Updated on 29-Mar-2022 12:18:27

749 Views

In this article, we will understand how to create random strings. String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”).Below is a demonstration of the same −Suppose our input is −The size of the string is defined as: 10The desired output would be −Random string: ink1n1dodvAlgorithmStep 1 - START Step 2 - Declare an integer namely string_size, a string namely alpha_numeric and an object of StringBuilder namely string_builder. Step 3 - Define the values. Step 4 - Iterate for 10 times usinf a for-loop, generate a random value using the function Math.random() ... Read More

Split String into Substrings in Java

AmitDiwan
Updated on 29-Mar-2022 12:09:04

682 Views

In this article, we will understand how to splitting into a number of sub-strings. String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”). A part or a subset of string is called substring.Below is a demonstration of the same −Suppose our input is −Input string: JVMThe desired output would be −The substring list printed as an ArrayList : [J, JV, JVM, V, VM, M] The sub-strings after splitting is: (1) "J" (2) "JV" (3) "JVM" (4) "V" (5) "VM" (6) "M"AlgorithmStep 1 - START Step 2 - Declare a string namely ... Read More

C++ Code to Find Palindrome String with Substring

Arnab Chakraborty
Updated on 29-Mar-2022 12:01:05

256 Views

Suppose we have a string S with n letters. We have to find another string T, such that T is palindrome and S is subsequence of T.So, if the input is like S = "ab", then the output will be "aabaa" (other answers are also available)StepsTo solve this, we will follow these steps −res := S reverse the array S res := res + S return resExampleLet us see the following implementation to get better understanding −#include using namespace std; string solve(string S){    string res = S;    reverse(S.begin(), S.end());    res += S;    return res; } ... Read More

C++ Code to Find Reduced Direction String of Robot Movement

Arnab Chakraborty
Updated on 29-Mar-2022 11:59:08

274 Views

Suppose we have a string S with n letters. The letters are either 'R' or 'U'. On a 2D plane, a robot can go right or up. When it is 'R' it moves right and when it is 'U' it moves up. However the string is too large, we want to make the string smaller. A pair like "RU" or "UR" will be replaced as diagonal move "D". We have to find the length of the final updated reduced string.So, if the input is like S = "RUURU", then the output will be 5, because the string will be "DUD"StepsTo ... Read More

C++ Code to Get Shortest Distance from Circular Stations

Arnab Chakraborty
Updated on 29-Mar-2022 11:56:52

467 Views

Suppose we have two numbers s and t, and another array D with n elements. The circle line of the Dreamland subway has n different stations. We know the distances between all pairs of neighboring stations: D[i] is the distance between station i and i+1, and D[n-1] is the distance between (n-1) and 0th station. We have to find shortest distance from s to t.So, if the input is like s = 1; t = 3; D = [2, 3, 4, 9], then the output will be 5.StepsTo solve this, we will follow these steps −n := size of D ... Read More

Check If Given Flag Is Stripped or Not in C++

Arnab Chakraborty
Updated on 29-Mar-2022 11:53:56

294 Views

Suppose we have a matrix of size n x m. Each cell will hold one value from 0 to 9. There is a flag should be striped: each horizontal row of the flag should contain squares of the same color, and the colors of the adjacent horizontal rows should be different. We have to check given matrix is valid flag or not.So, if the input is like000111333StepsTo solve this, we will follow these steps −n := row count of matrix m := column count of matrix l := 'm' res := 1 for initialize i := 0, when i < ... Read More

Advertisements