Finding the number of letters in a text means finding the length of the string. Although C++ has a built-in function for finding the length of a text, it may not help you in coding interviews if you are asked to count the number of letters in a text without using the length() function. So in this article, I will take you through to a tutorial on how to write a C++ program to find the number of letters in a text.
C++ Program to Find Number of Letters
You can get a question to find the number of letters in a text in many ways. Some other popular questions on the same logic are:
- Write a C++ program to count the letters of a text/string/character.
- Write a C++ program to find the length of a text/string/character.
All questions mean the same thing. It wants you to find the length of the text. While there is a built-in length() function in C++ that can be used here, but if you get this question in coding interviews, using the length() function may not be a good choice. So here is how you can write a C++ program to find the number of letters in a text:
19
In the above code, I have first defined a string variable as word. You can also ask for user input to take a string value inside the word variable. Then I have defined an integer variable as i with an initial value as 0. Then I am using a while loop over the string value inside the word variable where each letter of the string will increase the value of i by adding 1 to it. In the end, we will get the number of letters in the word variable as the value of i.
Also, Read - C++ Programs for Beginners with Code.
Summary
So this is how you can write a C++ program to count the number of letters in a text without using the length() function. Finding the number of letters in a text means finding the length of the string. I hope you liked this article on a tutorial on how to write a C++ program to find the number of letters in a text. Feel free to ask your valuable questions in the comments section below.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.