This tutorial will discuss about unique ways to check if a char array is null terminated.
Table Of Contents
Technique 1: Using STL Algorithm any_of()
To check if a char array has any ‘ ’ character or not, we can use the STL Algorithm any_of()
. For this we need to pass three arguments in the all_of()
function,
- Iterator pointing the first character in char array.
- Iterate pointing to the end of Char Array.
- A Lambda function that accepts a character as argument, and checks if the given character is ‘ ’ or not.
If all_of()
function returns true
, then it means that Char Array is null terminated.
Let’s see the complete example,
Frequently Asked:
#include <iostream> #include <algorithm> int main() { char strValue[5] = "test"; // Check if char array contains a // null terminated character bool result = std::any_of( std::begin(strValue), std::end(strValue), [](const char& ch) { return ch == '#include <iostream> #include <algorithm> int main() { char strValue[5] = "test"; // Check if char array contains a // null terminated character bool result = std::any_of( std::begin(strValue), std::end(strValue), [](const char& ch) { return ch == ' '; }); if (result) { std::cout<<"Yes, Char Array is Null terminated n"; } else { std::cout<<"No, Char Array is not Null terminated n"; } return 0; }';
});if (result)
{
std::cout<<"Yes, Char Array is Null terminated n";
}
else
{
std::cout<<"No, Char Array is not Null terminated n";
}return 0;
}Output :
Yes, Char Array is Null terminatedTechnique 2: Check if char array is null terminated
Iterate over all the characters in char array, using a for loop. During iteration, for each character, check if it matches with the ‘ ’ character or not. If yes, then break the loop and mark that the char array is null terminated.
Let’s see the complete example,
#include <iostream> #include <algorithm> // Check if char array is null terminated bool isNullTerminated(const char * arr, size_t len) { bool result = false; // Iterate over all characters in array for(int i = 0; i < len; i++) { // Check if it matches the null character if(arr[i] == '#include <iostream> #include <algorithm> // Check if char array is null terminated bool isNullTerminated(const char * arr, size_t len) { bool result = false; // Iterate over all characters in array for(int i = 0; i < len; i++) { // Check if it matches the null character if(arr[i] == ' ') { // break the loop result = true; break; } } return result; } int main() { char arr[5] = {'a', 'b', 'c', 'd', 'e'}; // Get the size of array size_t len = sizeof(arr) / sizeof(arr[0]); // Check if Array is null terminated if (isNullTerminated(arr, len)) { std::cout<<"Yes, Char Array is Null terminated n"; } else { std::cout<<"No, Char Array is not Null terminated n"; } return 0; }')
{
// break the loop
result = true;
break;
}
}
return result;
}int main()
{char arr[5] = {'a', 'b', 'c', 'd', 'e'};
// Get the size of array
size_t len = sizeof(arr) / sizeof(arr[0]);// Check if Array is null terminated
if (isNullTerminated(arr, len))
{
std::cout<<"Yes, Char Array is Null terminated n";
}
else
{
std::cout<<"No, Char Array is not Null terminated n";
}return 0;
}Output :
Best Resources to Learn C++:
No, Char Array is not Null terminatedSummary
Today we learned about several ways to check if a char array is null terminated. Thanks.
Leave a Reply
You must be logged in to post a comment.