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;
}
Comments
Post a Comment