Hi all,
I need to write a program that will first read a file and then
1. Output all the words that appear in a file.
2. Treat only sequences of letters and digits as words, i.e., all other characters such as punctuation marks should be used as word delimiters, but otherwise ignored.
Using the below code as a template..

#include <iostream>
#include<cdtg>
using namespace std;

void splitStringIntoWords(const string& line) {
    int i = 0;
    while (i < line.length()) {
        if (isspace(line[i])) {
            i = i + 1;
        } else {
            int j = i;
            while ((i < line.length()) && (!isspace(line[i]))) {
                i = i + 1;
            }
            string word = line.substr(j,i-j);
            cout << word << endl;
        }
    }
}

int main()
{
    string str = "";
    cout << "Please input a string: ";
    getline(cin,str);

    cout << "----------" << endl;
    splitStringIntoWords(str);
    cout << "----------" << endl;

    return 0;
}

I have no clue how to start off with.Can anyone please help me?I will really appreciate it.Thanks in advance.
Sana

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Make sure to enclose all C++ code within the code tags. http://www.daniweb.com/forums/announcement8-3.html

Look up ifsteam and ofstream. You'll need the <fstream> header as well. This is the method you might want to use for opening a file to read (ifstream) and outputting your data (ofstream).

One method of doing so is using strings. Input each line into a string, finding the space (" ") between words and creating substrings (.substr()) to hold each word, then find the next one.

So here are some things to look up:
-ifstream/ofstream
-string c++
-using strings to find a word in a sentence (google this and I would bet that someone has created a site/tutorial for almost exactly what you need to do)

Hope that helps.

Edited 6 Years Ago by gnarlyskim: n/a

This article has been dead for over six months. Start a new discussion instead.