Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C++ Exercises: Find the length of a string without using the library function

C++ For Loop: Exercise-34 with Solution

Write a program in C++ to find the length of a string without using the library function.

Pictorial Presentation:

C++ Exercises: Find the length of a string without using the library function

Sample Solution:-

C++ Code :

#include <iostream>
#include <string>
using namespace std;

int main()
{
    char str1[50];
    int i, l = 0;
    cout << "\n\n Find the length of a string:\n";
    cout << "---------------------------------\n";
    cout << " Input a string: ";
    cin >> str1;
    for (i = 0; str1[i] != '\0'; i++) {
        l++;
    }
    cout << "The string contains " << l << " number of characters." << endl;
    cout << "So, the length of the string " << str1 << " is:" << l << endl;
}

Sample Output:

 Find the length of a string:                                          
---------------------------------                                      
 Input a string: w3resource.com                                        
The string contains 14 number of characters.                           
So, the length of the string w3resource.com is:14 

Flowchart:

Flowchart: Find the length of a string without using the library function

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C++ to Check Whether a Number can be Express as Sum of Two Prime Numbers.
Next: Write a program in C++ to display the pattern like right angle triangle using an asterisk.

What is the difficulty level of this exercise?