string

what i should use
if the user enter from keyboard a sentence and sentence will show how many sentence it has

i have used myString.length()
but this one only shows how many character i have entered
what i should use helppp
I assume you meant how many words the sentence has. In that case, you can use find() to search for spaces, which will give you a general approximation of the number of words in the string.
this is what i use to find how many words user entered i have used as you said but there is an error ???

#include <iostream>
#include <string>
using namespace std;
int main()
{
string myString;
int i,count=0;
cout<<"\n Enter a sentence:";
getline(cin, myString, '\n');
cout<<"\n You sentence contain " << myString.find() << " characters" << endl;
system("pause");
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
myString.find(' ') //will return the index of the first space it finds
//note the space charecter ' ', it needs to know what to find (search for)

//example
int index = myString.find(' ');
if( index > 0 ) //index will be -1 if no space (' ') was found
  cout << myString[index]; //if a space was found, we print it

//you can also supply a starting point for the search
string name = "Chris L. D."
int index = name.find(' ', 6); //now it is only looking for a space (' ') at or past the 6th index
cout << index; //it will print 8, because the spaces are at the 5th and 8th indexes,
               // but 8 is the smallest index larger then 6 (the first space at or past index 6) 


See http://cplusplus.com/reference/string/string/find/ for more details.
Last edited on
Try looking up how to use the find() method.
http://cplusplus.com/reference/string/string/find/
Then consider how you can use that information to figure out how many words there are.
i think its not hard to write some answer for that problem i dont need copy paste something i need solution i dnt need the sh t not fck ng hard problem is it for you

if you dont want to help than dont give any answr i am not interest of this crap i just have do my lesson
Last edited on
malikrgc wrote:
if you dont want to help than dont give any answr


a, "we want to help"
b, "we give any answr"
Define "any answr" as "an answer"

Given: ~a ⊃ ~b
Observe: b

(~a ⊃ ~b) • ba


PS: You might want to check your keyboard because I think you lost a few vowel keys.
Last edited on
I don't feel like doing your homework for you, especially not for free. You can take your attitude and feel free to go elsewhere. Mathead200 has given you more than enough information to solve to problem yourself.
homework :)) what you think writing some codes makes you Scientist or a hero :)
this question answer not FBI secret :))
so what i am trying to say sharing information is plus you information
anyway thank you for not answering my question
CHANGE THAT FORUM MAKE IT WE DONT DO YOUR HOMEWORK WE DONT GIVE SOLUTION WE JUST GIVE MORE COMPLEX PROBLEMS PLUS TO YOUR HOMEWORK && PROJECTS



Last edited on
malikrgc wrote:
so what i am trying to say sharing information is plus you information
anyway thank you for not answering my question
CHANGE THAT FORUM MAKE IT WE DONT DO YOUR HOMEWORK WE DONT GIVE SOLUTION WE JUST GIVE MORE COMPLEX PROBLEMS PLUS TO YOUR HOMEWORK && PROJECTS

and then we smile your helpless situations :)


You're welcome. Thank you for not trying to do the problem assigned to you yourself.
get of my question board huge freak wht for u r here ?
I get the impression you could get help with homework if you were polite enough.

If they think they can help you by giving you clues that may seem cryptic at first, then think about it respecting that they gave any attention to your problem at all, and then if unable to overcome the problem still, ask for help again.

It is a tendency of people who give free help to want to help you through more than just doing homework for you, but actually giving you clues so that you can solve the problem yourself which is a better learning experience and will help you far more overall and in the long-term.

And if people selectively choose against helping with your homework then it's really their prerogative.
malikrgc wrote:
what you think writing some codes makes you Scientist or a hero :)
Why yes, but don't take my word for it; try it and find out for yourself.

malikrgc wrote:
this question answer not FBI secret
How do you know? Do you know how to solve this problem?

malikrgc wrote:
thank you for not answering my question
Any time, and may I be so bold as to thank you for not reading my response: http://cplusplus.com/forum/beginner/43389/#msg234469

malikrgc wrote:
CHANGE THAT FORUM MAKE IT WE DONT DO YOUR HOMEWORK WE DONT GIVE SOLUTION
http://cplusplus.com/forum/beginner/1


If you don't think we are that smart then prove us wrong. Go do the problem yourself and show us how easy it is... And, I started to get sarcastic around this post: http://cplusplus.com/forum/beginner/43389/#msg234490

Lol, Trolls... http://www.youtube.com/watch?v=FMEe7JqBgvg
Last edited on
Topic archived. No new replies allowed.