counting number of letters in the strings entered by the user

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 2
Reputation: mridulsouth is an unknown quantity at this point 
Solved Threads: 0
mridulsouth mridulsouth is offline Offline
Newbie Poster

counting number of letters in the strings entered by the user

 
0
  #1
Nov 26th, 2008
I have problem in counting the number of letters in the strings entered by the user. CAn anyone suggest something in the following codes?
  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. char* str;
  9. cout<<"enter the string to count the number of letters in it.";
  10. cin>>str;
  11. cout<<str.length;
  12. return 0;
  13. }
Last edited by Ancient Dragon; Nov 26th, 2008 at 11:40 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: counting number of letters in the strings entered by the user

 
0
  #2
Nov 26th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 941
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: counting number of letters in the strings entered by the user

 
0
  #3
Nov 26th, 2008
Replace:
char *str; with
string str;
and fix:
str.length with
str.length()
Last edited by MosaicFuneral; Nov 26th, 2008 at 10:42 pm.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC