lab06Q7.cpp - Q7 Break up a sentence into individual words and print out the words#include <iostream> using namespace std int main const int SIZE = 100

lab06Q7.cpp - Q7 Break up a sentence into individual words...

This preview shows page 1 out of 1 page.

// Q7. Break up a sentence into individual words and print out the words #include <iostream> using namespace std; int main() { const int SIZE = 100; // maximum number of characters in sentence char sentence[SIZE]; // character array to store sentence char word[10]; // character array to store one word char *psentence, *pword; // pointer to character psentence = sentence; // pointer psentence points to address of the first element of array sentence pword = word; // pointer pword points to address of the first element of array word cout << "Enter a sentence: "; // tell user to enter a sentence cin.getline(sentence, SIZE); // get a line of characters from input for(int i=0; i<SIZE; i++) { if(*psentence != '\0') // check if the pointer has reached the end of the sentence { if(*psentence != ' ') // check if pointer is pointing to a space character { // If yes, do the following: *pword = *psentence; // copy current character from sentence to
Image of page 1

You've reached the end of your free preview.

Want to read the whole page?

Unformatted text preview: word. pword++; // increment point to point to the next character space in word psentence++; // increment pointer to point to the next character in array sentence } else // If no, do the following: { *pword = '\0'; // terminate the character array word with \0 to indicate end of word cout << word << endl; // print out the word if space is encountered by current pointer pword = word; // reset the position of pointer pword back to the first element of array word. psentence++; // increment pointer to point to the next character in array sentence } } else // if pointer reaches end of the sentence { *pword = '\0'; // terminate the character array word with \0 to indicate end of word cout << word << endl; // print out the last word if \0 is encountered by current pointer break; // break out from the for loop } } return 0; }...
View Full Document

  • Winter '19

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors