i am taking a cpsc 140 class and i'm a little confused how to begin my assignment. Write a C++ program that reads in an unknown number of characters from the a text file, count the number of words, and print each word in reverse to the screen.
Words are delimited by blanks, tabs, newlines and the end of file. Note that mulitple whitespace characters should only count one word. Hint: You can use the get method to enter whitespace . i dont recall what the get method is. if anyone can start me out i would be greatly appreciated. thanks :)

Recommended Answers

All 8 Replies

could you help me get started on this program. i dont want u write the program for me, i just would like to know what steps i have to take to figure it out on my own

#include <iostream>
#include <fstream>
#include <string>

int main(void)
{
   std::ifstream file(__FILE__);
   std::string word;
   while ( file >> word )
   {
      std::cout << word << std::endl;
   }
   return 0;
}

Just a little help ...

// pulls the words out of a text stream
string get_word(istream& in)
{
  char ch;
  string s;

  while(in.get(ch)) 
  {
    // these characters separate the words
    if (ch == ' ' || ch == ',' || ch == '.' || ch == '\n' || ch == '\t')
      break;
    s.push_back(ch);  // build the word
  }
  return s;
}

steps:
1) order a large amount of coffee
2) order some pizzas
3) kickstart your brain
4) start thinking how you would solve it
5) write down your solution in words on a piece of paper
6) translate that into a flowchart and/or other diagrams
7) translate that into a functional model of your application
8) translate that into code
9) test and fix until working

thanks for all the help..the only thing i cant figure out now is how to put the actual text file in. the text is :

this is a file containing lots of
words with whitespace such as tabs and
endoflines and blanks but no
punctuation
you should be counting the
words and reversing each word before
echoing this tothescreen at the end
print the number of words in the file


is there a way for me to put this in without actually putting all this text in my code?

Sure, and that's exactly how you should do it:

Create a textfile on disk.
Read in the file in your program. C(++) has full facilities for reading and writing any kind of file built right into the core language.

thanks for all the help..the only thing i cant figure out now is how to put the actual text file in. the text is :

this is a file containing lots of
words with whitespace such as tabs and
endoflines and blanks but no
punctuation
you should be counting the
words and reversing each word before
echoing this tothescreen at the end
print the number of words in the file


is there a way for me to put this in without actually putting all this text in my code?

Select the text in your editor and save it as test.txt then read it into your program as a text file. You can separate the text into words with strtok(), setting the appropriate delimiters. It's up to your genius to spell each word in reverse.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.