Programming Articles - Page 2657 of 3366

Python program to Rearrange a string so that all same characters become d distance away

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

996 Views

Given a non-empty string str and an integer k , rearrange the string such that the same characters are at least distance k from each other.All input strings are given in lowercase letters. If it is not possible to rearrange the string, return an empty string "".Example 1:str = “tutorialspoint”, k = 3 Answer: “tiotiotalnprsu”The same characters are at least 3 character distance apart.str = "aabbcc", k = 3 Answer: "abcabc" The same characters are at least 3 character distance apart.Example 2str = "aaabc", k = 3 Answer: "" It is not possible to rearrange the string.Example 3:str = "aaadbbcc", k = ... Read More

How to make the program sleep for x milliseconds in C++?

Samual Sam
Updated on 30-Jul-2019 22:30:26

532 Views

Here we will see how to sleep for x (given by user) milliseconds in C++ program.To do this thing we can use different libraries. But here we are using the clock() function. The clock() will return the current CPU time. Here we will try to find the ending time from the clock, and the given x value. Then for that amount of time, we will run one blank while loop to take the time. Here one macro is used called CLOCKS_PER_SEC, this finds the number of clock ticks per second.Let us see the code to get the better idea about ... Read More

What is the difference between static_cast<> and C style casting?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

4K+ Views

Here we will see what are the differences between static_cast and normal C style cast.The normal cast like (int)x is C style typecasting where static_cast(x) is used in C++.This static_cast() gives compile time checking facility, but the C style casting does not support that. This static_cast() can be spotted anywhere inside a C++ code. And using this C++ cast the intensions are conveyed much better.In C like cast sometimes we can cast some type pointer to point some other type data.Like one integer pointer can also point character type data, as they are quite similar, only difference is character has ... Read More

Python program to Find the first non-repeating character from a stream of characters?

AmitDiwan
Updated on 11-Aug-2022 11:57:47

9K+ Views

In this article, we will find the first non-repeating character from a stream of character. Let’s say the following is our input − Thisisit The following should be our output displaying first non-repeating character − H Find the first non-repeating character from a stream of characters using while loop We will find the first non-repeating character, by comparing each character with the other using a loop − Example # String myStr = "thisisit" # Looping while myStr != "": slen0 = len(myStr) ch = myStr[0] myStr = myStr.replace(ch, "") slen1 = len(myStr) if slen1 == slen0-1: print ... Read More

How do I generate random floats in C++?

Samual Sam
Updated on 30-Jul-2019 22:30:26

4K+ Views

In C or C++, we cannot create random float directly. We can create random floats using some trick. We will create two random integer values, then divide them to get random float value.Sometimes it may generate an integer quotient, so to reduce the probability of that, we are multiplying the result with some floating point constant like 0.5.Example#include #include #include using namespace std; main() {    srand((unsigned int)time(NULL));    float a = 5.0;    for (int i=0;i

How to automatically generate a stacktrace when a gcc C++ program crashes?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

2K+ Views

For Linux and we can use gcc to compile C/C++ codes. This compiler uses glibc library. We can use the backtrace() function to trace the error. This function is present inside the execinfo.h header file. In this example, we are going to display Segmentation fault error using the stack trace feature.Example#include #include #include #include #include using namespace std; void error_handler(int sig) {    void *array[10];    size_t size;    size = backtrace(array, 10); //get the void pointers for all of the entries    cout

Python program to Count words in a given string?

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

3K+ Views

Lets suppose we have a ‘string’ and the ‘word’ and we need to find the count of occurence of this word in our string using python. This is what we are going to do in this section, count the number of word in a given string and print it.Count the number of words in a given stringMethod 1: Using for loop#Method 1: Using for looptest_stirng = input("String to search is : ") total = 1 for i in range(len(test_stirng)):    if(test_stirng[i] == ' ' or test_stirng == '' or test_stirng == '\t'):       total = total + ... Read More

How to execute a command and get output of command within C++ using POSIX?

Samual Sam
Updated on 30-Jul-2019 22:30:26

871 Views

Here we will see how to use the POSIX command through C++. The process is very simple, we have to use the function called system(). Inside this we have to pass string. That string will contain the POSIX command. The syntax is like below.system(“command”)Example#include using namespace std; int main () {    cout

Increment and Decrement Operators in Python?

Arjun Thakur
Updated on 23-Aug-2023 14:01:38

66K+ Views

Python does not have unary increment/decrement operator (++/--). Instead to increment a value, usea += 1to decrement a value, use −a -= 1Example>>> a = 0 >>> >>> #Increment >>> a +=1 >>> >>> #Decrement >>> a -= 1 >>> >>> #value of a >>> a 0Python does not provide multiple ways to do the same thing .However, be careful if you are coming from a language like C, Python doesn’t have "variables" in the sense that C does, instead python uses names and objects and in python integers (int’s) are immutable.Let’s understand it with an example−>>> a =1 >>> ... Read More

What is the size of int, long type as per C++ standard?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

615 Views

Here we will see what are the sizes of the int and long type data in C++. The sizes are depending on the system architecture and the Operating system.So in the 32-bit system, the standard is ILP32. In this standard the int, long and the pointer variables are of 32-bits.For the 64-bit system there are two variations. For Linux Operating system the standard is LP64. Here long and pointer are of 64-bits, but int are of 32-bits. For the Windows operating system, the standard is LLP64. Here long long is 64-bit, but int and long are of 32-bits.Example#include using ... Read More

Advertisements