+ 2

(C++)How do I separate words from a sentence?

Hello, I would like to know how to separate words in a character array which were inputed as a sentence and also calculate the number of total words in a sentence?

1st Feb 2020, 8:02 PM
Muneeb Bhalli
Muneeb Bhalli - avatar
9 Antworten
+ 2
Considering that there is only a single space between every word in the sentence, take a pointer and let it traverse the char array and whenever there is a blank space, increment a count variable. So at the end, your total number of words in a sentence would be count+1.
1st Feb 2020, 8:08 PM
Avinesh
Avinesh - avatar
+ 2
That should be '\0' and not '/0'.
1st Feb 2020, 8:12 PM
Avinesh
Avinesh - avatar
+ 2
This example is quite close to what you are looking for so use it to write the solution to your problem. https://code.sololearn.com/c4fFM41kJ6KM/?ref=app
1st Feb 2020, 8:20 PM
Avinesh
Avinesh - avatar
+ 1
If you show your code I'm sure the community would be able to help you but we are not here to do your work for you... Thanks...
1st Feb 2020, 8:08 PM
BroFar
BroFar - avatar
+ 1
I mistyped that as /0 sorry. I will share my code with you guys tomorrow as I don't have my laptop atm. The problem I am facing is that the number of words are taken as input and stored in a char array with large size the loop works fine till the end of the sentence but after that all goes wrong
1st Feb 2020, 8:21 PM
Muneeb Bhalli
Muneeb Bhalli - avatar
0
Here's the thing I already tried the method using cin.get method and the cin until I find '/0' but I can't seem to end the loop when I reach the end of the sentence in short I would like to know how I can terminate my loop when i reach the end of sentence
1st Feb 2020, 8:10 PM
Muneeb Bhalli
Muneeb Bhalli - avatar
0
Void removeDupWord(string s) { istringstream ss(s); do{ string word; ss >> word; cout << word << endl;} while(ss); } int main() { string s = "hello how are you"; removeDupWord(s); return 0; }
3rd Feb 2020, 11:57 AM
Gagan Verma
Gagan Verma - avatar