Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C++ Sample code: /* Developer: Name Language: C++ Course: CSE-111 Assignment: Lab #1 Due: MM/DD/YYYY Note: A palindrome is a string of characters that

In C++

image text in transcribedimage text in transcribed

Sample code:

/* Developer: Name Language: C++ Course: CSE-111 Assignment: Lab #1 Due: MM/DD/YYYY

Note: A palindrome is a string of characters that reads the same forward or backward (i.e. radar or noon)

Description: This program reads every word from a word file and determines which words are palindromes and which words are not palindromes. Then writes each type of word to it's appropriate text file. For our the purpose of this assignment we have restricted our palindrome test to single word palindromes (i.e. no white-spaced allowed).

*/

#include // functions to work with single characters // tolower, toupper, isspace, isalpha, isblank #include // cin and cout #include // fstream, ifstream and ofstream #include // string class, string have the following usefully functions // length(), at(i), substr using namespace std;

// function prototypes bool isPalindrome(string); // THIS FUNCTION NEEDS TO BE DEFINED BELOW void open(ifstream&,string); string getInput(const string); string longest(const string,const string); // THIS FUNCTION NEEDS TO BE FINISHED BELOW

// constants const int NUMBER_OF_OUTPUTS = 2;

// main function int main () { // initialize input and output file streams ifstream input; ofstream output[NUMBER_OF_OUTPUTS];

// open input file string filename = getInput( "Please ENTER the name of your input word file: " ); open( input, filename );

// Task #1 - ENTER CODE TO OPEN EACH OUTPUT FILE STREAMS

string word; // variable to store the current word we read in string longestWord; // variable to keep track of longest word string longestPalindrome; // variable to keep track of longest palindrome

// initialize counters keep track of how many of each int numberOfPalindromes = 0; // counter for palindromes int numberOfNonPalindromes = 0; // counter for non-palindromes

/* task #2 repeatedly(for,while or do-while loop required) read each word in and do the following: - test each word to determine if they are or are not palindromes (if/else) - "if" the word is a palindrome write it to the palindrome word file - "else" the non-palindromes should be written to the non-palindrome file - increment the appropriate counters - determine the longest word and longest palindrome */

// ENTER CODE for Task #2 HERE

// close all the files // Task #3 - ENTER CODE TO CLOSE ALL OF THE FILE STREAMS WE OPENED

// display the values we kept track of // Task #4 - ENTER CODE TO DISPLAY THE RESULTS WE TRACKED

return 0; }

/* Determine the longest word. @w1 the first word @w2 the second word @return w1 if w1 is longer than w2, otherwise return w2. */ string longest( const string w1, const string w2 ) { // Task #5 - ENTER code to implement this function }

/* Get string input from the user. @prompt is the prompting message the user will see @return the string response from the user */ string getInput( const string prompt ) { string response;

// prompt the user for the desired input cout > response;

return response; }

/* Opens an input file for reading. @file the input file stream used for reading @name the name of the input text file @return void */ void open( ifstream& file, string name ) { // try to open file with the specified name file.open( name.c_str() );

// if the file is not successfully opened we need to // tell the user the file could not be found and ask // them for another name before trying to open again while ( !file.is_open() ) { cout

/* Checks to see if a word is a palindrome. @word the word that will be evaluated @return true if word is a palindrome, false otherwise */

// Task #6 - ENTER Code for isPalindrome function

Purpose: The goal of this assignment is to review the following concepts: decision making, loops, file I/O, functions string and pointers. Description: Write a program that take the name of a text file as input, read each word from the file and determine if it is a palindrome. A palindrome is a word, phrase, number of sequence of words that reads the same backwards as forwards. Punctuation and spaces between the words or lettering is allowed. For the purpose of this assignment we will strictly implement single word palindromes (i.e. Anna, radar, Level), no punctuation or spacing allowed Program Requirements: 1. The program needs to prompt the user for the name of an input text file. 2. Sequentially read each word from the text file and write all palindromes to a file named "palindromes .txt" and write all non- palindromes to a file named "non-palindromes .txt" 3. Keep track of two running totals named numberOfPalindromes and numberOfNonPalindromes 4. Keep track of the longest word in the list and the longest palindrome in the list 5. Write a function that implements the palindrome algorithm as follows i. if the first and the last characters of the word are different return false ii. if the word's length equals 1 return true ii. if the word's length equals 2 return the equivalence test of the first and last characters iv. else remove the first and last characters of the word v. repeat 6. Display the two running totals that you've tracked 7. Display the longest word and longest palindrome Note: These are the minimum requirements, you should used good style and break your program down into various functions to do different tasks. Sample Program Output: CAUsers\marquisepullen DesktoplA1_Solution bin\Debug A1_Solution.exe Please ENTER the name of your word file: words.txt The number of palindromes are: 412 The number of non-palindromes are: 466132 Longest word: pneumonoultramicroscopicsilicovolcanoconiosis Longest word palindrome: kinnikinnik Process returned (oe) execution time : 15.779 s Press any key to continue

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Demystified

Authors: Andrew Oppel

1st Edition

0072253649, 9780072253641

More Books

Students also viewed these Databases questions

Question

=+ What are the information and consultation requirements?

Answered: 1 week ago

Question

=+ Should the MNE belong (why, why not)?

Answered: 1 week ago