| | |
counting number of letters in the strings entered by the user
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 2
Reputation:
Solved Threads: 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?
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by Ancient Dragon; Nov 26th, 2008 at 11:40 pm. Reason: add code tags
•
•
Join Date: Nov 2008
Posts: 392
Reputation:
Solved Threads: 72
Why not use a string
Add
replace char* ... with this
and change your output line to this....
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.
Add
#include <string> at the topreplace 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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Read from file
- Next Thread: [windows]How can i change the value of a loweord
| Thread Tools | Search this Thread |
api array beginner bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





