Reading from file [like word count but including punctuation]

I have to use character array to count words as well as punctuation. (not including numbers and arithmetic signs (%, +).

But I only know how to read and count words INCLUDING punctuation

For example:
Sample sentence : I like bananas.

sample output
 1. I
2. like
3. bananas
4. .


Here's my current code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <fstream>

using namespace std;

ifstream inStream;
ofstream outStream;

int main()
{
    inStream.open("input4.txt");
    if (inStream.fail()){
    cout<<"Unable to open file!"<<endl; }
    else {
 int i = 1;
    char word[200];
    while(!inStream.eof()){    
    inStream>>word;

    cout<<i<<". "<<word<<endl;
    i++;}}
                          
system("Pause");
return 0;
}


and the output would be this:
1. I
2. like
3. bananas.


I know I don't do anything to change it, which is why I'm asking, because I don't know how to count punctuation as a word using Char or string. :\ .
Any suggestions?
you need to have some thing like this .. also you can use ascii code for comparision of symbols .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

#include <fstream>

using namespace std;

ifstream inStream;
ofstream outStream;
int count_puncuation = 0; 
int main()
{
    inStream.open("input4.txt");
    if (inStream.fail()){
    cout<<"Unable to open file!"<<endl; }
    else 
		{
			int i = 1;
			char word[200];
			while(!inStream.eof())
				{    
					inStream>>word;
					if( word != "+" | word!= '-' ) 
					{
						cout<<i<<". "<<word<<endl;
						i++;
					}else if(word == "+" | word == '-' )
						{
							count_puncuation++;

						}
			}
			cout <<"\n Punctation count = count = "<<count_puncuation;
                          
system("Pause");
return 0;
}
Last edited on
ifstream class has a nice function called peek which look's up for next char but does not read it.

that means you can use it to check wether the next char is number or punctation and according to the result you put endl to create new line!

putback function is used to put the char back on stream after peek function has chek it.
Hi codekiddy

when i try this code

1
2
3
4
5
6
char c;    
		c=infile.peek();
		if ( (c == '+') && (c == '-') )
		{    
				cout << "You have a symbol " << endl;
		}


it doesnt seem to come out of loop.
Thanks guys, I'll try out your suggestions now (: .
@bluecoder
hi,
this is your code:
if ( (c == '+') && (c == '-') )
you are checking here wether variable c is both '+' and '-' what always gives false.

btw I dont see any loop here.
The input might be:
To be, or not to be, that is the question:
Whether 'tis Nobler in the mind to suffer
The Slings and Arrows of outrageous Fortune,
Or to take Arms against a Sea of troubles,
And by opposing end them: to die, to sleep.


Read a whitespace delimited token and check if the token is just a word or a word with punctuation as a suffix. If it is a word with punctuation, split it into two - a word and punctuation. (Hopefully, words in the text would not be split across lines with hyphenation.)

Token: To -> word To
Token: be, -> word with punctuation. word be, punctuation ,
Token: question: -> word with punctuation. word question, punctuation :
Token: outrageous -> word outrageous
Token: 'tis -> word 'tis

Here's a snippet to get you started:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cstring>
#include <cctype>

int main()
{
    char token[128] ;
    while( std::cout << "? " && std::cin.getline( token, sizeof(token) ) )
    {
        const auto len = std::strlen(token) ;
        if( len == 0 ) break ;
        const char last = token[len-1] ;

        char word[sizeof(token)] = {0} ;
        char punct[2] = "" ;
        if( std::ispunct(last) )
        {
           if( len > 1 ) std::strncpy( word, token, len-1 ) ;
           punct[0] = last ;
        }
        else strcpy( word, token ) ;

        std::cout << "token: " << token << '\n' ;
        std::cout << "word: " << word << '\n' ;
        if( punct[0] ) std::cout << "punctuation: " << punct << '\n' ;
    }
}


> I have to use character array to count words as well as punctuation.

Is there any particular reason why you have to use character array instead of std::string?
I wrote this on the fly so it may has some typos....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <ifstream>
#include <cctype>
using namespace std;

int main()
{
    ifstream inStream;
    char temp;

    inStream.open("input4.txt");
    if (inStream.fail())
            return 0;

   while(!inStream.eof())
   {    
           if(isnum(inStream.peek())) continue;
           inStream >> temp;
           cout << temp;
    }
    return 0;
}
Last edited on
@JLBorges:
Because my dad said I have to use a character array :\.
> Because my dad said I have to use a character array

Well, I assumed that you are just beginning to learn C++; the point I was trying to make, based on that assumption, is this:

Even for the professional programmer, it is impossible to first learn a whole programming language and then try to use it. A programming language is learned in part by trying out its facilities for small examples. Consequently, we always learn a language by mastering a series of subsets. The real question is not ‘‘Should I learn a subset first?’’ but ‘‘Which subset should I learn first?’’

One conventional answer to the question ‘‘Which subset of C++ should I learn first?’’ is ‘‘The C subset of C++.’’ In my considered opinion, that’s not a good answer. The C-first approach leads to an early focus on low-level details. It also obscures programming style and design issues by forcing the student to face many technical difficulties to express anything interesting. ...

C++’s better support of libraries, better notational support, and better type checking are decisive against a ‘‘C first’’ approach. However, note that my suggested alternative isn’t ‘‘Pure Object-Oriented Programming first.’’ I consider that the other extreme.
- Stroustrup in 'Learning Standard C++ as a New Language' http://www.research.att.com/~bs/new_learning.pdf

To take an example that illustrates the issue (lifted almost verbatim from the above paper), consider a simple programming problem: read the next white-space delimited token from input.

Robust code using std::string:
1
2
3
        std::string token ;
        std::cin >> token  ;
        std::cout << "the token is: " << token << '\n' ;


Equivalent code using C-style string:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
        int sz = 32 ;
        char* token = static_cast<char*>( std::malloc(sz) ) ;
        if( token == nullptr ) throw std::bad_alloc() ;

        // get rid of leading white-space
        while(true)
        {
            int c = std::getchar() ;
            if( c == EOF ) break ;
            if( !std::isspace(c) )
            {
                std::ungetc( c, stdin ) ;
                break ;
            }
        }

        int pos = 0 ; // current char position

        while(true)
        {
            int c = std::getchar() ;
            if( std::isspace(c) || ( c == EOF ) ) // end of input
            {
                token[pos] = 0 ; // null-terminate the string
                break ;
            }

            token[pos] = c ; // add c at current char position
            ++pos ;

            if( pos == (sz-1) ) // if buffer is full
            {
                // reallocate to a larger size
                sz *= 2 ;
                char* temp = static_cast<char*>( std::realloc( token, sz ) ) ;
                if( temp == nullptr )
                {
                    std::free(token) ;
                    throw std::bad_alloc() ;
                }
                else
                    token = temp ;
            }
        }

        std::cout << "the token is: " << token << '\n' ;

Topic archived. No new replies allowed.