Finding the length of a string in c and c++

# Finding the length of a string in c and c++:

# What is a string:

A string is a set of characters and string is terminated by a '\0' character. This character is used to know the termination of the string.


So to find the length of the above string. We have to traverse this string till we find the '\0'. Please check the below program for more details:

#include <iostream>

using namespace std;

int main()
{
    
/*
     * No need to append '\0' as this is
     * added by Compiler automatically as we use "".
     */
    char *s = "welcome";

    int iLength = 0;

    while (s[iLength] != '\0')
    {
        ++iLength;
    }

    cout << "The Length of the string is: " << iLength << endl;
    return 0;
}

# Output of the program:




Note: Full program can be downloaded from the link below:

Comments

Popular posts from this blog

Insert an element in a sorted array

Transposition Linear Search