C++ Program to Find the Length of a String

Here is a C++ program to calculate string length using while loop.


C++ program to find the length of a string

#include <iostream>

using namespace std;
 
int main(){
    char str[1000];
    int length = 0;
    cout << "Enter a string\n";
    cin.getline(str, 1000);

    while(str[length] != '\0'){
     length++;
    }
     
    cout << "Length of String is : " << length << endl;
     
    return 0;
}
Output
Enter a string
techcrashcourse
Length of String is : 15

Recommended Posts
C++ Program to Copy String Without Using strcpy
C++ Program to concatenate two strings without using strcat function.
C++ Program to Print Array in Reverse Order
C++ Program to Delete Spaces from String or Sentence
C++ Program to Delete Vowels Characters from String
C++ Program to Convert Uppercase to Lowercase Characters
C++ Program to Multiply Two Numbers Using Addition
C++ Program to Store Information of a Book in a Structure
C++ Program to Calculate Difference Between Two Time Periods
All C++ Programs