Server Side Programming Articles

Page 2087 of 2109

Bool to int conversion in C++

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jul-2019 13K+ Views

Here we will see how to convert bool to int equivalent in C++. Bool is a datatype in C++, and we can use true or false keyword for it. If we want to convert bool to int, we can use typecasting. Always true value will be 1, and false value will be 0.Example#include using namespace std; main() {    bool my_bool;    my_bool = true;    cout

Read More

C++ Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 219 Views

This is a C++ program to repeatedly search the same text.AlgorithmsBegin    Take the original string and pattern to be searched as input.    org_len = store the length of original string    pat_len = store the length of pattern    for i = 0 to (org_len - pat_len)       for j = 0 to pat_len - 1          if (org[i + j] != patt[j])             if (j == pat_len)                Increase m.       Print the position at which the pattern is ...

Read More

C++ Program to Find the Longest Prefix Matching of a Given Sequence

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 623 Views

Here we shall discuss a C++ program to find the Longest Subsequence Common to All Sequences in a Set of Sequences.AlgorithmsBegin Take the array of strings as input. function matchedPrefixtill(): find the matched prefix between string s1 and s2 :    n1 = store length of string s1.    n2 = store length of string s2.    for i = 0, j = 0 to i

Read More

getopt() function in C to parse command line arguments

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 9K+ Views

The getopt() is one of the built-in C function that are used for taking the command line options. The syntax of this function is like below −getopt(int argc, char *const argv[], const char *optstring)The opstring is a list of characters. Each of them representing a single character option.This function returns many values. These are like below −If the option takes a value, then that value will be pointed by optarg.It will return -1, when no more options to procesReturns ‘?’ to show that this is an unrecognized option, it stores it to optopt.Sometimes some options need some value, If the ...

Read More

How to find Segmentation Error in C & C++ ? (Using GDB)

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 1K+ Views

The segmentation error is one of the runtime error, that is caused because of the memory access violation, like accessing invalid array index, pointing some restricted address etc. In this article, we will see how to detect this type of error using the GDB tool.Let us see the code and respective steps to locate the error.Example#include main() {    int* ptr = NULL;    *ptr = 1; //trying to access unknown memory location    printf("%p", ptr); }Compile the code using ‘gcc –g program_name.c’, and run using ‘./a.out’Outputsoumyadeep@soumyadeep-VirtualBox:~/Cpp_progs$ ./a.out Segmentation fault (core dumped)The segmentation error occurred.Write ‘gdb ./a.out core’soumyadeep@soumyadeep-VirtualBox:~/Cpp_progs$ gdb ...

Read More

Measure execution time with high precision in C/C++

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 2K+ Views

To create high precision timer we can use the chrono library. This library has high resolution clock. This can count in nanoseconds.In this program we will see the execution time in nanoseconds. We will take the time value at first, then another time value at the last, then find the difference to get elapsed time. Here we are using blank loop to pause the effect for sometimes.Example#include #include typedef std::chrono::high_resolution_clock Clock; main() {    auto start_time = Clock::now();    for(int i = 0; i

Read More

Generalized Lambda Expressions in C++14

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 386 Views

In C++11, the lambda was introduced. Lambdas are basically a part of code, that can be nested inside other function call statements. By combining lambda expressions with the auto keyword, they can be used later.In C++14, these lambda expressions are improved. Here we can get the generalized lambda. For example, if we want to create a lambda, that can add integers, add numbers, also concatenate strings, then we have to use this generalized lambda.Syntax of the lambda expression is looking like this:[](auto x, auto y) { return x + y; }Let us see one example to get the better idea.Example#include ...

Read More

The best way to hide a string in binary code in C++?

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 699 Views

Here we will see how to hide some string into some binary code (Here binary code is represented in hexadecimal number).The approach is very simple. We can use the string stream to convert decimal number to hexadecimal numbers. Now from the string, we will read each character, and take its ASCII value, these ASCII values are converted into hexadecimal values. Then we can print them one by one.Example#include #include using namespace std; string dec_to_hex(int decimal){ //function is used to convert decimal to hex    stringstream my_ss;    my_ss

Read More

What's the best way to trim std::string in C++?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 11K+ Views

Here we will see how to trim the strings in C++. The trimming string means removing whitespaces from left and right part of the string.To trim the C++ string, we will use the boost string library. In that library, there are two different methods called trim_left() and trim_right(). To trim string completely, we can use both of them.Example#include #include using namespace std; main(){    string myStr = " This is a string ";    cout

Read More

What is the most effective way for float and double comparison in C/C++?

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 8K+ Views

Here we will see how to compare two floating point data or two double data using C or C++. The floating point / double comparison is not similar to the integer comparison.To compare two floating point or double values, we have to consider the precision in to the comparison. For example, if two numbers are 3.1428 and 3.1415, then they are same up to the precision 0.01, but after that, like 0.001 they are not same.To compare using this criteria, we will find the absolute value after subtracting one floating point number from another, then check whether the result is ...

Read More
Showing 20861–20870 of 21,090 articles
Advertisements