C++ Word finder and Word counter: List the words in a sentence and count the total number of words found

80

By eternaltreasures

Source code:

/*
PROGRAM: Word finder and Word counter
PURPOSE: List the words in a sentence and count the total number of words found
LANGUAGE: C++
AUTHOR: eternaltreasures
DATE: 2010 October 2
*/

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

#define newline '\n'

char sentence [124];

int i;
int character_x;
int first_write_character = 0;
int wordcounter = 0;

void extractwords ()
{
 //Start testing the sentence from the first character until the null character \0
 for (i=0; sentence[i] != '\0'; i++)
 {
 //if a space is encountered, then write the words
  if (sentence[i] == ' ')
   {   
    wordcounter = wordcounter + 1;    
    for (character_x = first_write_character; character_x < i; character_x ++)
     {
      cout << sentence[character_x];
     }
       cout << newline;
    first_write_character = i + 1;
    }
  }
}

//once the program detects the null character or empty string \0
//then this is the last word in the sentence input from the user
void extractlastword ()
{
 wordcounter = wordcounter + 1;
 for (character_x = first_write_character; character_x < i; character_x ++)
 {
  cout << sentence[character_x];
 
 }
}

//this function displays the total word count in the given sentence
void countwords ()
{
 cout << newline;
 cout << newline;
 cout << "Total words found: " << wordcounter;
}

//this is the main function of execution in the program
int main ()
{
cout << newline;

cout << "Please enter a sentence: ";
cout << newline;
gets (sentence);

cout << newline;
cout << "List of words found:";
cout << newline;
   
extractwords ();

extractlastword ();

countwords ();

return 0;
}

Programming Microsoft Visual C++
Amazon Price: $76.05
List Price: $49.99
Microsoft Visual C++ Windows Applications by Example
Amazon Price: $35.99
Microsoft Visual C++ .NET Standard 2003 [Old Version]
Amazon Price: $59.90
List Price: $109.00

Output of the program

The user is prompted to enter a sentence then press the 'Enter' key. The program then tests the presence of words, lists the words and counts the total number of words found.
The user is prompted to enter a sentence then press the 'Enter' key. The program then tests the presence of words, lists the words and counts the total number of words found.

C++ TUTORIAL FOR BEGINNERS:

How to capture the system date and time and display them in useful common format

How to determine a given number if it is Odd or Even number

How to display characters on the screen

How to generate a random number, maximum random number & random numbers within a given range

How to read a file and store the records in a character array

How to read a file and store the records in a string

Program to calculate Circumference, Area of a Circle, and the Volume of a Sphere

Program to compute for Total Monthly Income (Revenue), Expenses, and Net Income (Profit)

Variables and their practical applications in calculations


Other interesting C++ Programs:

C++ Word finder and Word counter: List the words in a sentence and count the total number of words found

C++ Database Name Search Program: User inputs last, first name, search from a name file, match using string function

C++ String Manipulation functions: Card Verification System - Examples of String append & Substring, Reading database


CHESS ENGINE PROGRAMMING IN C++: Winboard

CHESS: Best Openings and Defenses:

White's best opening first moves - most successful first moves with most wins

Black's best opening defence reply moves against White's first moves

White's Top 5 Best Openings

Top 5 Best Defense Openings


Endgame Chess Puzzles for beginners:

Mate in 3 moves chess problems:

Bishop and Knight

King and two Bishops

King and Rook

King and Queen

King, two Knights, and Pawn

Queen and Pawn

Queen and Knight

Queen and Bishop

Queen and Rook

Rook and Knight

Rook, Bishop and Pawn

Rook and Rook (two rooks)


Beginner Chess:

How to win chess games quickly by checkmate or material advantage:

Early opening Queen & Bishop mate 1

Early opening Queen & Bishop mate 2

Early opening Queen & Knight mate 1

Early opening Queen & Knight mate 2

Bishop and Knight mate

Bishop and Knight mate 2

Knight captures queen's rook

Queen captures king's rook


Learn How to Play Chess - Tutorial for Beginners & Kids:

RANKS, FILES, ALGEBRAIC BOARD NOTATION & INITIAL PIECES SETUP

KING moves and captures

QUEEN moves and captures

BISHOP moves and captures

ROOK moves and captures

PAWN moves and captures

KNIGHT moves and captures


HOW TO PLAY BLINDFOLD CHESS:

How to Play Blindfold Chess: Basic Tips Tricks, Tactics, Techniques, Memory Trainer, Learning Methods to Visualize Board

Comments

No comments yet.

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    • No HTML is allowed in comments, but URLs will be hyperlinked
    • Comments are not for promoting your Hubs or other sites

    Please wait working