characterarray

how can i find length of character array in c++.
eg
char a[100];
cin>>a;

if i enter ABC how to retrieve its length ie 3…

The first, and the simplest way is to use the strlen() function from <string.h> ( on C ) or <cstring> ( on C++ ). It returns the length . For example you can use something like

char a[100];
int l;
std::cin.getline ( a , 100 ) ; 
l = strlen( a );
std::cout << l;

Now, l will have the length of the character array ( the null character '\0' is not included ).

Alternatively, you can use this code

char a[100];
int  l;
std::cin.getline( a,100 );
for( l = 0 ; a[l] != '\0' ; ++l );   // don't forget the semicolon here
std::cout << l;	

But, it’s much better to use

std::string

over character arrays. When using that, you can get the size like

std::string st;
std::cin >> st;
int l = st. length() ;
std::cout << l ;

@the_flash here it is

  1. using loop

    int main() {
    char a[100];
    int i,len;
    len = 0;
    cin >> a;

     for ( i=0;a[i]!='\0';i++ )
     	len += 1;
     	
     cout << len;
     return 0;
    

    }

  2. using in-built function

    #include<bits/stdc++.h>
    int main() {
    char a[100];
    int i,len;

     	cin >> a;
     	
     	len = strlen(a);
     		
     	cout << len;
     	return 0;
     }