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;
}

Recommended Answers

All 2 Replies

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.

Replace: char *str; with string str; and fix: str.length with str.length()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.