0

I have problem in counting the number of letters in the strings entered by the user. CAn anyone suggest something in the following codes?

#include <iostream>


using namespace std;

int main()
{
	char* str;
	cout<<"enter the string to count the number of letters in it.";
	cin>>str;
	cout<<str.length;
	return 0;
}
3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by MosaicFuneral
0

Why not use a string
Add #include <string> at the top
replace char* ... with this std::string str; // and
and change your output line to this.... std::cout<<"Length == "<<str.length()<<std::endl; note that since string is a class, and str is an object of type string,
you call the string method called length. It is not a variable of string but a function hence the () after length.

However, it you want to count the letters and be a bit more careful about
spaces etc. Then there a a lot of post here and FAQs elsewhere that discuss how to proceed.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.