Found 7401 Articles for C++

How to quickly reverse a string in C++?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

160 Views

In this section, we will see how to reverse a string very quickly using C++. For reversing there is a built-in function in the algorithm library, called reverse(). This function takes the beginning and the ending pointer of a container, then reverse the elements.Input: A number string “Hello World” Output: “dlroW olleH”AlgorithmStep 1:Take a string Step 2: reverse it using reverse() function Step 3: Print the result. Step 4: EndExample Code Live Demo#include #include using namespace std; main() {    string my_str = "This is a string to be reversed";    cout

How to map C++ enums to strings?

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

3K+ Views

Here we will see how to map some enum type data to a string in C++. There is no such direct function to do so. But we can create our own function to convert enum to string.We shall create a function that takes an enum value as the argument, and we manually return the enum names as a string from that function.Example Code Live Demo#include using namespace std; enum Animal {Tiger, Elephant, Bat, Dog, Cat, Mouse}; string enum_to_string(Animal type) {    switch(type) {       case Tiger:          return "Tiger";       case ... Read More

How to convert std::string to lower case in C++?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

8K+ Views

In this section, we will see how to convert all letters of a C++ string to lowercase letters. To do this thing we have to use the transform function. This transform function is present in the algorithm library.The transform function takes the beginning pointer of the string and the ending pointer of the string. It also takes the beginning of the string to store the result, then the fourth argument is ::tolower. This helps to convert the string into a lowercase string. We can use this same method if we want to convert some string into an uppercase string.Example Code Live ... Read More

How to convert an enum type variable to a string in C++?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

382 Views

Here we will see how to convert some enum type data to a string in C++. There is no such direct function to do so. But we can create our own function to convert enum to string.We shall create a function that takes an enum value as an argument, and we manually return the enum names as a string from that function.Example Code Live Demo#include using namespace std; enum Animal {Tiger, Elephant, Bat, Dog, Cat, Mouse}; string enum_to_string(Animal type) {    switch(type) {       case Tiger:          return "Tiger";       case ... Read More

How to convert a std::string to const char* or char* in C++?

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

955 Views

In this section, we will see how to convert C++ string (std::string) to const char* or char*. These formats are C style strings. We have a function called c_str(). This will help us to do the task. It returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object.Following is the declaration for std::string::c_str.const char* c_str() const;This function returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object. If an exception is thrown, ... Read More

Differences between C++ string == and compare()?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

3K+ Views

In C++ we can compare two strings using compare() function and the == operator. Then the question is why there are two different methods? Is there any difference or not?There are some basic differences between compare() and == operator. In C++ the == operator is overloaded for the string to check whether both strings are same or not. If they are the same this will return 1, otherwise 0. So it is like Boolean type function.The compare() function returns two different things. If both are equal, it will return 0, If the mismatch is found for character s and t, ... Read More

Difference between string and char[] types in C++

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

672 Views

In this section we will see what are the differences between string and the char[] in C++. The char[] is basically an array of characters. So there are some properties of this array. These properties are listed below.If the char[] is allocated in the stack section then it will always occupy 256 bytes of space. It will not depend on the size of the text.If we use malloc() or calloc() to dynamically allocate space for it into the heap section then we are responsible for releasing the memory after using this, and we always have the overhead of a heap ... Read More

C++ Program to Implement String Matching Using Vectors

Anvi Jain
Updated on 30-Jul-2019 22:30:25

466 Views

This is another string matching method. In this approach, we are searching for a substring using vectors.In C++ we can create vectors easily using the standard library. We are taking the main string and the string that will be searched as a vector, then searching it into the main string. When one match is found the function returns the address, and removes it from the main string. So in the next iteration, it starts from location 0 and searches again.For multiple occurrences, we are using loops and repeatedly searching for the match, and return the position.Input: Main String: “ABAAABCDBBABCDDEBCABC”, Pattern ... Read More

Wrapping C/C++ for Python using SWIG

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

1K+ Views

There are multiple ways to wrap your existing C or C++ functionality in Python. In this section, we are going to see how we can wrap C/C++ functionality with SWIG. Below are other options to wrap c/c++ functionality in python.Manual wrappingUsing pyrex to wrap C code.CtypesSIPBoost PythonSWIG (Simple Wrapper Interface Generator) is capable of wrapping C code with numerous other languages including Perl, Python, PHP, Ruby, Tcl, C#, Common Lisp (CLISP, Allegro, CL, UFFI, CFFI), Java, Modula-3 and OCAML. Swig also supports multiple interpreted and compiled Scheme implementations (like Guile, MzScheme, Chicken) are supported.But we will discuss its implementation with ... Read More

negate function in C++ STL

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

763 Views

Negate function is used to negate the given values such that to change the sign of the values. It changes the negative values to positive and vice-versa.Function prototype:function transform(a_begin, a_end, a1_begin, negate()):    a_begin = lower bound of the array.    a_end = upper bound of the array.    a1_end = Lower bound of the second modified array.    negate() = to negate the values of the array.Example Code#include #include #include using namespace std; int main() {    int a[] = { 4,6,7, -10, -20, -30 };    transform(a, a + 6, a, negate());    for (int i = 0; i < 6; i++)       cout

Advertisements