How to Find Length of String in C++ Without Using Strlen(). C++ program to find the length of a string without using strlen. Write a C++ program to find a number of characters in a given string without using a library function. Program to find the length of a string taken from the user without using an inbuilt function.
How to Find Length of String in C++ Without Using Strlen()
- First, take a String input from the user and store the string gets() functions.
- Now, initialize the FOR LOOP, i = 0.
- Count string characters from start i = 0 to end ( '\0' ) and Increase the array index by 1.
- At the end print the value of i. Here we can either use a count variable and increase the count by 1 in each iteration.
- Here no need to use an extra variable, Print the last index of an array. We can calculate a length of a string.
Length of String in C++ Without Using Library Function
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main()
{
cout << "=======================================";
cout << "\nVisit - www.programmingwithbasics.com";
cout << "\n=====================================";
char a[50];
int i;
cout << "Enter An Any String:\t";
gets(a);
for (i = 0; a[i] != '\0'; ++i) {}
cout << "\nLenth Of The Given String Given Below Is\n\n";
cout << i << endl;
return 0;
}
The Output of Find Length of String in C++ Without Strlen
Similar to the Length of the String
0 Comments: