Check if a Char Array is Null Terminated in C++

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 ‘\0’ 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 ‘\0’ or not.

If all_of() function returns true, then it means that Char Array is null terminated.

Let’s see the complete example,

#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 == '\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;
}
'; }); 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 terminated

Technique 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 ‘\0’ 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] == '\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;
}
') { // 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 :

No, Char Array is not Null terminated

Summary

Today we learned about several ways to check if a char array is null terminated. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top