LAST UPDATED: OCTOBER 31, 2020
C++ Program to Find Length of String without using System Defined method
Hello Everyone!
In this tutorial, we will learn how to demonstrate how to find the length of the String without using the System Defined method, in the C++ programming language.
Logic:
In programming, the end of the string is denoted with a special delimiter called the NULL character. The null or string-terminating character is represented by another character escape sequence, '\0
'.
So, to compute the length of the string, we need to traverse the string from its first character and keep incrementing the counter till we come across the NULL character, which will tell us that the string has got terminated.
Code:
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
cout << "\n\nWelcome to Studytonight :-)\n\n\n";
cout << " ===== Program to Determine the length of the String without using System defined function, in CPP ===== \n\n";
//Variable Declaration
char s1[100], c = 'a';
int n = 0, i = 0;
cout << "\n\nEnter the String you want to find the length for : ";
cin >> s1;
//Computing string length without using system defined method
while (c != '\0')
{
c = s1[i];
i++;
}
n = i - 1;
cout << "\n\nLength of the entered string \"" << s1 << "\" is : " << n << "\n\n\n";
return 0;
}
Output:
We hope that this post helped you develop a better understanding of the concept of finding the length of the string without using the system-defined method in C++. For any query, feel free to reach out to us via the comments section down below.
Keep Learning : )