Skip to content

Data Type Manipulation

Data types are an essential part of any programming language. They define the type of data that can be stored in a variable and the operations that can be performed on that data. In C++, data types can be manipulated in various ways to achieve different results. In this section, we will discuss some common data type manipulations in C++.

Type Casting

Type casting is the process of converting a value from one data type to another. In C++, there are two types of type casting:

  • Implicit Type Casting: This type of casting is done automatically by the compiler. It is also known as coercion. For example, when you assign an int value to a double variable, the int value is implicitly converted to a double.

  • Explicit Type Casting: This type of casting is done manually by the programmer. It is also known as type conversion. Explicit type casting is done by placing the desired data type in parentheses before the value to be converted. For example, (double) 10 converts the int value 10 to a double.

Here is an example of type casting in C++:

int a = 10;
double b = (double) a;

In the example above, the int value 10 is explicitly converted to a double and stored in the variable b.

Type Conversion Functions

C++ provides several functions that can be used to convert data from one type to another. Some of the common type conversion functions in C++ are:

  • stoi(): Converts a string to an integer.
  • stof(): Converts a string to a float.
  • stod(): Converts a string to a double.
  • to_string(): Converts a number to a string.

Here is an example of using the atoi() function to convert a string to an integer:

#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "10";
int num = stoi(str);
int result = num + 5;
cout << result << endl;
return 0;
}

In the example above, the stoi() function is used to convert the string "10" to an integer, which is then added to 5 and stored in the variable result. Without the stoit() function, you would not be able to perform arithmetic operations on the string "10" and would get a compilation error.

String Manipulation

Strings are a sequence of characters in C++. They are used to store text data and can be manipulated in various ways. Some common string manipulation operations in C++ are:

Concatenation

String concatenation is the process of combining two or more strings into a single string. In C++, you can concatenate strings using the + operator or the append() function.

Here is an example of concatenating strings in C++:

main.cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "Hello, ";
string str2 = "World!";
string result = str1 + str2;
cout << result << endl;
return 0;
}

In the example above, the strings "Hello, " and "World!" are concatenated using the + operator and stored in the variable result.

Substring

A substring is a part of a string. In C++, you can extract a substring from a string using the substr() function.

Here is an example of extracting a substring in C++:

main.cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello, World!";
string sub = str.substr(7, 5);
cout << sub << endl;
return 0;
}

In the example above, the substring "World" is extracted from the string "Hello, World!" starting from the 7th character and with a length of 5 characters.

String Length

The length of a string is the number of characters it contains. In C++, you can get the length of a string using the length() function.

Here is an example of getting the length of a string in C++:

main.cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello, World!";
int len = str.length();
cout << len << endl;
return 0;
}

In the example above, the length of the string "Hello, World!" is calculated using the length() function and stored in the variable len.

String Replace

String replace is the process of replacing a part of a string with another string. In C++, you can replace a substring in a string using the replace() function.

Here is an example of replacing a substring in C++:

main.cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello, World!";
str.replace(7, 5, "Universe");
cout << str << endl;
return 0;
}

In the example above, the substring "World" in the string "Hello, World!" is replaced with the string "Universe" using the replace() function.

String Find

String find is the process of searching for a substring in a string. In C++, you can find a substring in a string using the find() function.

Here is an example of finding a substring in C++:

main.cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello, World!";
string word = "World";
size_t pos = str.find(word);
if (pos != string::npos) {
cout << "Found '" << word << "' at position: " << pos << endl;
} else {
cout << "'" << word << "' not found" << endl;
}
return 0;
}

In the example above, the substring "World" is searched in the string "Hello, World!" using the find() function. If the substring is found, the position of the substring is printed; otherwise, a message indicating that the substring was not found is printed.

String to Upper/Lower Case

You can convert a string to uppercase or lowercase in C++ using the toupper() and tolower() functions.

Here is an example of converting a string to uppercase and lowercase in C++:

main.cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello, World!";
for (char &c : str) {
c = toupper(c);
}
cout << str << endl;
for (char &c : str) {
c = tolower(c);
}
cout << str << endl;
return 0;
}

In the example above, the string "Hello, World!" is converted to uppercase and lowercase using the toupper() and tolower() functions, respectively.

String Split

String split is the process of splitting a string into multiple substrings based on a delimiter. In C++, you can split a string using the find() and substr() functions.

Here is an example of splitting a string in C++:

main.cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello, World!";
string delimiter = ", ";
size_t pos = 0;
string token;
while ((pos = str.find(delimiter)) != string::npos) {
token = str.substr(0, pos);
cout << token << endl;
str.erase(0, pos + delimiter.length());
}
cout << str << endl;
return 0;
}

In the example above, the string "Hello, World!" is split into two substrings "Hello" and "World!" based on the delimiter ", ".

String Reverse

You can reverse a string in C++ using the reverse() function.

Here is an example of reversing a string in C++:

main.cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello, World!";
reverse(str.begin(), str.end());
cout << str << endl;
return 0;
}

In the example above, the string "Hello, World!" is reversed using the reverse() function and stored in the variable str.

String Trim

String trim is the process of removing leading and trailing whitespace characters from a string. In C++, you can trim a string using the find_first_not_of() and find_last_not_of() functions.

Here is an example of trimming a string in C++:

main.cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = " Hello, World! ";
size_t start = str.find_first_not_of(" ");
size_t end = str.find_last_not_of(" ");
string trimmed = str.substr(start, end - start + 1);
cout << trimmed << endl;
return 0;
}

In the example above, the string " Hello, World! " is trimmed to "Hello, World!" by removing the leading and trailing whitespace characters.

Number Manipulation

Other than the Mathematically operations, you can manipulate numbers in C++ in various ways. Some common number manipulation operations in C++ are:

Absolute Value

The absolute value of a number is the positive value of the number. In C++, you can get the absolute value of a number using the abs() function.

Here is an example of getting the absolute value of a number in C++:

main.cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int num = -10;
int absNum = abs(num);
cout << absNum << endl;
return 0;
}

In the example above, the absolute value of the number -10 is calculated using the abs() function and stored in the variable absNum.

Power

You can calculate the power of a number in C++ using the pow() function.

Here is an example of calculating the power of a number in C++:

main.cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int base = 2;
int exponent = 3;
int result = pow(base, exponent);
cout << result << endl;
return 0;
}

In the example above, the power of 2 raised to 3 is calculated using the pow() function and stored in the variable result.

Square Root

You can calculate the square root of a number in C++ using the sqrt() function.

Here is an example of calculating the square root of a number in C++:

main.cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int num = 16;
double result = sqrt(num);
cout << result << endl;
return 0;
}

In the example above, the square root of the number 16 is calculated using the sqrt() function and stored in the variable result.

More Math Functions

The <cmath> library in C++ provides several math functions that can be used to manipulate numbers. You can find the complete list of math functions provided by the <cmath> library in w3schools’ C++ cmath Library Reference.