20/03/2023

How to Find Length of String in C++ Without Using Strlen()

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()
{
	/*Find Length of String in C++ Without Using Strlen() */

	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


The Output of Find Length of String in C++ Without Strlen

Similar to the Length of the String


Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: