Found 34472 Articles for Programming

Remove Trailing Zeros from string in C++

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

3K+ Views

IN this program we will see how to remove the trailing zeros from a string in C++. Sometimes some string may contain trailing zeros like "00023054". After executing this program, it will return "23054" only. The initial zeros are removed.Input: A string with trailing zeros “000023500124” Output: “23500124”AlgorithmStep 1: Get the string Step 2: Count number of trailing zeros n Step 3: Remove n characters from the beginning Step 4: return remaining string.Example Code Live Demo#include using namespace std; main() {    string my_str = "000023500124";    int num = 0;    cout

Remove spaces from std::string in C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

15K+ Views

In this program we will see how to remove the spaces from a std::string in C++. To remove this we will use the remove() function. With this remove() function it takes the beginning and end of the iterator, then takes the third argument that will be deleted from that iterator object.Input: A string "This is C++ Programming Language" Output: "ThisisC++ProgrammingLanguage"AlgorithmStep 1: Get the string Step 2: Remove spaces from the given string using remove() function. Step 3: Return string.Example Code Live Demo#include #include using namespace std; main() {    string my_str = "This is C++ Programming Language";    cout

Raw string literal in C++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

400 Views

In C++11 and above there is a concept called Raw string. In strings we use different characters like , \t etc. They have different meaning. The is used to return the cursor to the next line, the \t generates a tab etc.If we want to print these characters in the output without seeing the effect of them, we can use the raw string mode. To make a string to raw string we have to add "R" before the string.Input: A string "Hello\tWorldC++" Output: "Hello\tWorldC++"AlgorithmStep 1: Get the string Step 2: Use R before string to make it raw string ... Read More

Collectors minBy() method in Java 8

George John
Updated on 30-Jul-2019 22:30:25

344 Views

The minBy() method of the Collectors class in Java 8 returns a Collector that produces the minimum element according to a given Comparator, described as an OptionalThe syntax is as followsstatic Collector

LongStream mapToInt() method in Java

Chandu yadav
Updated on 30-Jul-2019 22:30:25

759 Views

The mapToInt() method returns an IntStream consisting of the results of applying the given function to the elements of this stream.The syntax is as followsmapToInt(LongToIntFunction mapper)Here, the parameter mapper is the stateless function applied to each element.Declare LongStream and add some elementsLongStream longStream = LongStream.of(1000L, 13000L, 18000L);Now, use the IntStream and mapToInt()IntStream intStream = longStream.mapToInt(val -> (int) val);The following is an example to implement LongStream mapToInt() in JavaExample Live Demoimport java.util.*; import java.util.stream.IntStream; import java.util.stream.LongStream; public class Demo {    public static void main(String[] args) {       LongStream longStream = LongStream.of(1000L, 13000L, 18000L);       IntStream intStream = ... Read More

What is AbstractSequentialList class in Java?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

292 Views

The AbstractSequentialList class provides an implementation of the List interface. For an unmodifiable list, implement the list iterator's hasNext, next, hasPrevious, previous and index methods. For a modifiable list the programmer should implement the list iterator's set method.The syntax is as followspublic abstract class AbstractSequentialList extends AbstractListTo work with the AbstractSequentialList class in Java, you need to import the following packageimport java.util.AbstractSequentialList;First, create an AbstractSequentialList classAbstractSequentialList absSequential = new LinkedList();Now, add elementsabsSequential.add("Accessories"); absSequential.add("Home Decor"); absSequential.add("Books"); bsSequential.add("Stationery");Let us see an example to implement the AbstractSequentialList class in JavaExample Live Demoimport java.util.LinkedList; import java.util.AbstractSequentialList; public class Demo {    public static void main(String[] ... Read More

The toString() method of Java AbstractCollection class

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

220 Views

The toString() method of the AbstractCollection class is used to return the string representation of the elements of this collection.The syntax is as followspublic String toString()To work with AbstractCollection class in Java, import the following packageimport java.util.AbstractCollection;The following is an example to implement AbstractCollection toString() method in JavaExample Live Demoimport java.util.ArrayList; import java.util.AbstractCollection; public class Demo {    public static void main(String[] args) {       AbstractCollection absCollection = new ArrayList();       absCollection.add("HDD");       absCollection.add("Earphone");       absCollection.add("Headphone");       absCollection.add("Card Reader");       absCollection.add("SSD");       absCollection.add("Pen Drive");       ... Read More

The toArray(T[] a) T method of Java AbstractCollection class

George John
Updated on 30-Jul-2019 22:30:25

82 Views

The difference between toArray() and toArray(T[] arr) in Java AbstractCollection is that both the methods returns an array containing all of the elements in this collection, but the latter has some additional features i.e. the runtime type of the returned array is that of the specified array.The syntax is as followspublic T[] toArray(T[] arr)Here, arr is the array into which the elements of this collection are to be stored.To work with AbstractCollection class in Java, import the following packageimport java.util.AbstractCollection;The following is an example to implement AbstractCollection toArray() method in JavaExample Live Demoimport java.util.ArrayList; import java.util.AbstractCollection; public class Demo { ... Read More

ArrayBlockingQueue add() method in Java

Chandu yadav
Updated on 30-Jul-2019 22:30:25

64 Views

To add elements to the ArrayBlockingQueue class, use the add() method.The syntax is as followsboolean add(E ele)Here, ele is the element to be added to the queue. To work with ArrayBlockingQueue class, you need to import the following packageimport java.util.concurrent.ArrayBlockingQueue;The following is an example to add elements in Java ArrayBlockingQueue classExample Live Demoimport java.util.concurrent.ArrayBlockingQueue; public class Demo {    public static void main(String[] args) {       ArrayBlockingQueue q = new ArrayBlockingQueue(7);       q.add(100); q.add(250); q.add(300); q.add(450); q.add(550); q.add(600); q.add(700);       System.out.println("ArrayBlockingQueue = " + q);    } }OutputArrayBlockingQueue = [100, 250, 300, 450, 550, 600, 700]

DoubleStream summaryStatistics() method in Java

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

67 Views

The summaryStatistics() method of the DoubleStream class returns a DoubleSummaryStatistics describing various summary data about the elements of this stream. This is a special case of a reduction.The syntax is as followsDoubleSummaryStatistics summaryStatistics()Here, DoubleSummaryStatistics is a state object for collecting statistics such as count, min, max, average, etc. It works with streams. To use the DoubleStream class in Java, import the following packageimport java.util.stream.DoubleStream;First, create a DoubleStream and add some elementsDoubleStream doubleStream = DoubleStream.of(20.5, 35.8, 45.9, 50.8, 80.7);Now, get the statisticsDoubleSummaryStatistics details = doubleStream.summaryStatistics();The following is an example to implement DoubleStream summaryStatistics() methodExample Live Demoimport java.util.stream.DoubleStream; import java.util.DoubleSummaryStatistics; public class Demo ... Read More

Advertisements