A string is an array of characters or letters. This is a successive assortment of letters or an array of characters. The assertion and delineation of a string containing a collection of characters are similar to the assertion and delineation of an arrangement of other data types. In C , the length of a string signifies the number of bytes that are utilized to encrypt the specified string. This is because bytes are generally mapped to C characters.

In this article, we will discuss different methods of finding the length of the string in C . We install “DEVC ” software on our laptop to execute codes. First, we create a new file by tapping “Ctrl N” from the keyboard. After coding, we compile and run the code by “F11” from the keyboard.

Use “While” and “For” Loop

The use of a while loop is just like a traditional method for finding the length of different strings. In using for and while loop, we adjust the variable “counter” to 0 and then add that counter from the beginning of the given string to the completion of the string (ends with a null character).

In this instance, we utilize two loops. A “for” loop and a “while” loop can determine the length of the definite string. First, we use preprocessor directives. It contains a header file. This is utilized at the start of the program. These directives start with the sign “#”:

#include

using namespace std;

int main()

{


string str = “visual programming”;

int i = 0,count=0;

while (str[i] != )

{

i;

}

cout <<“Length of the string by using While Loop: “<< i << endl;

for (i=0; str[i]!=; i )

{


count ;

}

cout <<“Length of the string by using For Loop: “<< count << endl;

return 0;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/C-String-Length-1.png" data-lazy- height="279" src="data:image/svg xml,” width=”922″>

Here, we take #include header file. Then, we use the main function. Every program in C contains the function main(), which is the first segment to be implemented when the code runs.

Now, we take the string “visual programming”. The variable used for this string is “str”. Further, we take two more variables: the “i” variable and the “count” variable. We declare the variable “i”. Here, we utilize a variable named “count” to determine the length of the string. We initialize both variables to zero. We use a while loop here. Each string terminates with “” and this is known as an escape sequence. This “” is not a distinctive character. It is an accurate number zero. The while loop executes until the variable “str[i]” is no longer equivalent to the escape series.

At the end of the loop, there is an addition in the value of “I” to 0 until the last element of the defined string is found. By this, we find out the length of a given string. We use “cout” to print the message “length of the string by using while loop”:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/C-String-Length-2.png" data-lazy- height="57" src="data:image/svg xml,” width=”951″>

Now, we use the “for” loop. Here, the expression “i=0” initializes the variable “i” to 0. Initialization is done as soon as the loop is entered. This loop executes until the last character is reached. The expression “i ” increments the variable “i” each time the loop is executed. In the loop, the variable “count” is added each time until the termination of the defined string is reached. By this, we get the value of variable “count” and variable “i”. In the end, we again use “cout” to print the statement “length of the string by using for loop”.

Use Strlen() Function

“Cstring” is a library and it contains the function strlen(). In C , we utilize strlen() function to get the string length. This is an in-built function. It is used in C-style strings. This built-in function returns the length of the defined string from the first character to the end null character:

#include

#include

using namespace std;


 

int main() {


   char str[] = “I love to play badminto”“;


   int len = strlen(str);


   cout <<“"
Length of the string :” << len << endl;


}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/C-String-Length-3.png" data-lazy- height="148" src="data:image/svg xml,” width=”846″>

In this case, first, we use the “#include ” header file. And we must utilize the header file “#include ” at the beginning of the program to execute the code in which we use the strlen() function. The following code sample gets a C-style string and a char array and uses the strlen() function to get its length. We take a string “I love to play badminton” to get the length of this string.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/C-String-Length-4.png" data-lazy- height="37" src="data:image/svg xml,” width=”555″>

The given string has 24 characters in it. So, we get the 24 output. We use “cout” to print the “length of the string” message.

Use Str.length() Method

Another method for finding the length of the given string is the use of the str.length() function. It provides the string length in bytes. It is the actual number of bytes corresponding to the characters of the string, not certainly its storing capacity. The object of the defined string grips the bytes without encrypting information that might be utilized to encrypt its characters. So, the return value might not reflect the real number of encrypted characters in the series of multi-byte characters:

#include

#include

int main ()

{


  std::string str (“modern programming language”);


  std::cout << “The length of the string is “ << str.length();


  return 0;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/C-String-Length-5.png" data-lazy- height="147" src="data:image/svg xml,” width=”953″>

We use two header files: “#include ” and “#include ”. We take the object “str” of the class “std::string”. Then, we want to get the string length for “modern programming language”. We use str.length() function. It is a built-in function. Another built-in function used to find out the length of the string is str.size(). Using both functions will return an identical result. These functions return the length of the given string in bytes:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/C-String-Length-6.png" data-lazy- height="26" src="data:image/svg xml,” width=”647″>

For the strings of the class, we always utilize suitable methods. For example, we use str.length() or str.size() to find their lengths. The use of std::string is typically easier because it automatically allocates memory.

Conclusion

In this article, we explained several approaches that are used to get the length of different strings in C . The C strings are arrangements of letters or characters saved in adjacent memory addresses. To obtain the length of the C-style strings, we utilize the strlen() method. In the string, the constructor sets it to a C-style string ending in “ 0”. In the last method, we use a built-in function str.length(). This method is quite easy to implement because we just call the built-in function and obtain the length. We hope you found this article helpful. Check out other Linux Hint articles for more tips and information.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/omar-150×150.png" height="112" src="data:image/svg xml,” width=”112″>

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.