C++ program for calculating the length of string without using length() function

Calculating the length of string without using length() function.

In this article we will learn how to code a C++ program to calculate the length of string without using length() function. To do so we will iterate each character of the string through a for loop and increase the count until null(‘\0’) is not encountered .

C++ program for calculating the length of string without using length() function

Algorithm:

  • Initialize the variable.
  • Accept the string.
  • Initiate a for loop.
  • Increase the count on every iteration. 
  • Terminate the loop when null(‘\0’).
  • Print length.

C++ program to calculate the length of string without using length() function

Competitive Coding Techniques
#include <iostream>
using namespace std;
 
int main()
{
        //Initializing variable.
	char str[30];
	int i,length=0;
	//Accepting input.
	cout<<"Enter the string:";
	gets(str);
	//Initializing for loop.
	for(i=0;str[i]!='\0';++i)
	{
	length++;//Counting the length.
	}
	
	cout<<"Length of the string is:"<<length<<endl;
 
	return 0;
}
Output:
Enter the string:PREPINSTA
Length of the string is:9

Operations on Strings

  • Length of the string without using strlen() function : C | C++ | Java | Python
  • Toggle each character in a string : C | C++ | Java | Python
  • Count the number of vowels : C | C++ | Java | Python
  • Remove the vowels from a String : C | C++ | Java | Python
  • Check if the given string is Palindrome or not : C | C++ | Java | Python
  • Print the given string in reverse order : C | C++ | Java | Python
  • Remove all characters from string except alphabets : C | C++ | Java | Python
  • Remove spaces from a string : C | C++ | Java | Python
  • Remove brackets from an algebraic expression : C | C++ | Java | Python
  • Count the sum of numbers in a string : C | C++ | Java | Python
  • Capitalize the first and last character of each word of a string : C | C++ | Java | Python
  • Calculate frequency of characters in a string : C | C++ | Java | Python
  • Find non-repeating characters in a string : C | C++ | JAVA | Python
  • Check if two strings are Anagram or not : C | C++ | Java | Python
  • Replace a sub-string in a string: C | C++ | Java | Python
  • Count common sub-sequence in two strings : C | C++ | Java | Python
  • Check if two strings match where one string contains wildcard characters : C | C++ | Java | Python
Operations on Strings