Followers

Monday, May 8, 2023

Program to Find the Length of a String

Program:

 #include <iostream>    
 using namespace std;    
 int main() {  
      string a;  
      cout<<"Enter the string: ";  
      cin>>a;  
      cout<<"The length of the string: "<<a.size();  
      return 0;    
 }   

Output:







  Working Of The Program:

1. First, I include the iostream library in my program, which allows me to use input/output operations in C++.
2. Next, I declare that I'll be using the "std" namespace, which includes many useful functions and objects in C++.
3. In the main function, I declare a variable "a" of type string, which will be used to store the input string.
4. I then prompt the user to enter a string using the cout function, which displays the message "Enter the string: ".
5. To get the user's input, I use the cin function to read in the input string and store it in the variable "a".
6. Finally, I use the cout function again to display the length of the input string, which I obtain using the "size" member function of the string object. The message "The length of the string: " is concatenated with the size of the input string using the << operator.
7. Lastly, I return 0 to indicate that the program executed successfully.

Program to Find the Length of a String

Program: #include <iostream> using namespace std; int main() { string a; cout<<"Enter the string...