Count Number of characters of the Character Array

Count Number of characters of the character Array” is basically a problem of finding length of the character array. Here, we are given a character array and our task is to count the number of characters of the character array.

The number of characters of the character array is same as the size of the character array. 

Different programming languages have different in-built function to find the length of the string/character array.

Example:

Input:
Abcdef
Output:
6

Method 1: Without using in-built function

We know that the last character of the character array is NULL character (‘/0’). So, to find the length of character array, we will initialize a variable as res = 0 and traverse the complete character array, until we find the NULL character. While traversing, we will increment the res by 1 for every character encountered.

C++ Program to count the number of characters of the string is as follows:

#include<bits/stdc++.h>
#define len 1000   
using namespace std;  
int main()    
{  
    char str[len];  
    cout<<"Enter the string:\n";  
    cin>>str;  
    int res = 0;  
    /* In character array, last character is always null character('
 #include<bits/stdc++.h> #define len 1000 using namespace std; int main() { char str[len]; cout<<"Enter the string:\n"; cin>>str; int res = 0; /* In character array, last character is always null character('\0'), we will loop till null character is not found*/ for(int i = 0 ; i < len && str[i]!='\0' ; i++) { res++; } cout<<"\nThe number of characters of the "<<str<<" are: "; cout<<res; } 
'), we will loop till null character is not found*/ for(int i = 0 ; i < len && str[i]!='
 #include<bits/stdc++.h> #define len 1000 using namespace std; int main() { char str[len]; cout<<"Enter the string:\n"; cin>>str; int res = 0; /* In character array, last character is always null character('\0'), we will loop till null character is not found*/ for(int i = 0 ; i < len && str[i]!='\0' ; i++) { res++; } cout<<"\nThe number of characters of the "<<str<<" are: "; cout<<res; } 
' ; i++) { res++; } cout<<"\nThe number of characters of the "<<str<<" are: "; cout<<res; }
OUTPUT:
Enter the string: sdfdfv
The number of characters of the sdfdfv are 6
Time Complexity:
O(n), where n is the length of string.

Method 2: With using in-built function

In C Programming language, to find the length of the character array, we can use the in-built function strlen() which comes with header file string.h .

C++ Program to count the number of characters of the string is as follows:

#include<bits/stdc++.h>  
#define len 1000
using namespace std;  
int main()   
{  
    char str[len];  
    cout<<"Enter the string:\n";  
    cin>>str;  
    int res = strlen(str);  
    cout<<"\nThe number of characters of the "<<str<<" are: ";  
    cout<<res;  
}  

OUTPUT:
Enter the string: sdfdfv
The number of characters of the sdfdfv are 6
Time Complexity:
O(n), where n is the length of string.

Related Posts:

  1. Program to Count frequency of particular character of the String.
  2. Program to Count Number of Vowels and Number of Consonants in a String.
  3. Program to Check if String is alphabetic or numeric or alpha-numeric.
  4. Program to copy one String to Another.
  5. Program to Concatenate Two Strings.
  6. Program to Check whether given String is Palindrome or not.
  7. Program to convert lower-case alphabet to upper-case alphabet and vice-versa.
  8. Program to count total number of words in a string.
  9. Program to find smallest and largest word of the String.
  10. Program to find Most Frequent Character of the String.
  11. Program to Remove all the blank spaces from the String.
  12. Program to check if String is isogram or not.
  13. Program to Reverse Each word of the String.
  14. Program to Print All the Substring of the Given String.
  15. Program to find longest palindromic Substring.
  16. Program to check Anagram Strings.
  17. Program to check whether two Strings are Rotation of each other or not.
  18. Program to check Palindromic Anagram.
  19. Program to print all the Palindromic Substring of the String.
  20. Program to check Panagram String.

You may also like...