Q:

C++ Program to Find The Length of a String Without Using Strlen Function

0

Logic:- 

We have an inbuilt function to find length or size of the string. In this problem, I count string character from start to end ( '\0' ) and print the total number of count. we can either use a count variable and increase the count by 1 in each iteration but no need I an using an array index and with the help of an array I print the last index of an array. we can calculate a length of a string in C/C++ both functions are given below.

Length of String In C
strlen(str)

Length of String In C++
str.size();
str.length() c++

Both Use in C++. Here str is a string variable.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
 int main()
{
   
    cout<<"=====================================";
    cout<<"\nVisit - www.nerdutella.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;

}

 

Output:

=====================================

Visit - www.nerdutella.com

=====================================

Enter An Any  String:

c++ programming

Lenth Of The Given String Given Below Is

15

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Program to Compare Two Strings Without Using str... >>
<< C++ Program to Check Whether Given String is a Pal...