1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
ollegr [7]
2 years ago
9

A file named 'input.txt' contains a list of words. Write a program that reads the content of the file one word at a time. Check

if the word is a palindrome. If the word is a palindrome write it at the output file Output'.
Computers and Technology
1 answer:
Margarita [4]2 years ago
3 0

Answer:

// here is code in java.

import java.io.*;

import java.util.Scanner;

class Main{  

    // function to check a word is palindrome or not

 private static boolean is_Palind(String str1)

 {

     // if length of String is 0 or 1

   if(str1.length() == 0 || str1.length() == 1)

   {

     return true;

   }

   else

   {

       // recursively check the word is palindrome or not

     if(str1.charAt(0) == str1.charAt(str1.length()-1))

     {

       return is_Palind(str1.substring(1,str1.length()-1));

     }

     

     else

     {

       return false;

     }

   }

 }

// driver function

 public static void main(String[] args)

 {// variable to store word

   String word;

   // BufferedWriter object

   BufferedWriter buff_w = null;

   // FileWriter object

   FileWriter file_w = null;

   // Scanner object

   Scanner sc = null;

   try

   {

       // read the input file name

     sc = new Scanner(new File("input.txt"));

     // output file

     file_w = new FileWriter("output.txt");

     // create a buffer in output file

     buff_w = new BufferedWriter(file_w);

     

     // read each word of input file

     while(sc.hasNext())

     {

       word = sc.next();

     // check word is palindrome or not

       if(is_Palind(word))

       {

           // if word is palindrome then write it in the output file

         buff_w.write(word);

         buff_w.write("\n");

       }

     }

     // close the buffer

     buff_w.close();

     // close the input file

     sc.close();

   }

   // if there is any file missing

   catch (FileNotFoundException e) {

     System.out.println("not able to read file:");

   }

   // catch other Exception

   catch (IOException e) {

     e.printStackTrace();

   }

 }

}

Explanation:

Create a scanner class object to read the word from "input.txt" file.Read a word  from input file and check if it is palindrome or not with the help of is_Palind() function.it will recursively check whether string is palindrome or not. If it is  palindrome then it will written in the output.txt file with the help of BufferedWriter  object. This will continue for all the word of input file.

Input.txt

hello world madam

level welcome

rotor redder

output.txt

madam

level

rotor

redder

You might be interested in
I analyze data, as a consultant, for companies making important business decisions. In order to get my point across, which of th
ladessa [460]

Answer:

3

Explanation:

I've just taken the test and made a 100. 3 is the most logical answer aswell, it has more of an effect than the others.

4 0
3 years ago
Which of the following detects unauthorized user activities, attacks, and network compromises, alerts of the detected attacks, a
Anit [1.1K]

The answer is IPS (Intrusion Prevention Systems)

The Intrusion Prevention Systems and Intrusion Detection Systems (IDS) are two security technologies that secure networks and are very similar in how they work. The IDS detects unauthorized user activities, attacks, and network compromises, and also alerts. The IPS, on the other hand, as mentioned, is very similar to the IDS, except that in addition to detecting and alerting, it can also takes action to prevent breaches.


3 0
3 years ago
Write a program that lets the user enter the total rainfall for each of 12 months (starting with January) into an array of doubl
lianna [129]

Answer:

The C++ code is given below with appropriate comments

Explanation:

#include "stdafx.h"

//Header file section

#include <iostream>

#include <iomanip>

#include <string>

using namespace std;

//Function prototypes

double rainfallTotal(double[], int);

double averageMonthlyRainfall(double[], int);

double highestAmountRainfall(double[], int, int &);

double lowestAmountRainfall(double[], int, int &);

int main()

{

//Initialize variables

const int ALL_MONTHS = 12;

int highIndex = 0;

int lowIndex = 0;

//Declare variables

double totalRf;

double averageRf;

double mostRf;

double leastRf;

double monthlyRf[ALL_MONTHS];

string monthName[ALL_MONTHS] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

for (int i = 0; i < ALL_MONTHS; i++)

{

 cout << "Enter rainfall for " << monthName[i] << ": ";

 cin >> monthlyRf[i];

 // Check the input as negative numbers

 //for monthly rainfall

 while (monthlyRf[i] < 0)

 {

  cout << "Invalid data (negative rainfall) " << endl;

  cout << "Retry the rainfall in " << monthName[i] << ": ";

  cin >> monthlyRf[i];

 }

}

//Call the methods

totalRf = rainfallTotal(monthlyRf, ALL_MONTHS);

averageRf = averageMonthlyRainfall(monthlyRf, ALL_MONTHS);

mostRf = highestAmountRainfall(monthlyRf, ALL_MONTHS, highIndex);

leastRf = lowestAmountRainfall(monthlyRf, ALL_MONTHS, lowIndex);

//Display output

cout << fixed << showpoint << setprecision(2) << endl;

cout << "Total rainfall: " << totalRf << endl;

cout << "Average rainfall: " << averageRf << endl;

cout << "Least rainfall in " << monthName[lowIndex] <<endl;

cout << "Most rainfall in " << monthName[highIndex] <<endl;

system("pause");

return 0;

}

//Method definition of rainfallTotal

double rainfallTotal(double totalRainfall[], int n)

{

double total = 0;

for (int i = 0; i < n; i++)

 total += totalRainfall[i];

return total;

}

//Method definition of averageMonthlyRainfall

double averageMonthlyRainfall(double totalRainfall[], int n)

{

double average = 0.0;

double total = 0;

for (int i = 0; i < n; i++)

 total += totalRainfall[i];

average = total / n;

return average;

}

//Method definition of lowestAmountRainfall

double lowestAmountRainfall(double totalRainfall[], int n, int &monthIndex)

{

double least;

int i = 0;

least = totalRainfall[i];

while (i < n)

{

 if (totalRainfall[i] < least)

 {

  least = totalRainfall[i];

  monthIndex = i;

 }

 i++;

}

return least;

}

//Method definition of highestAmountRainfall

double highestAmountRainfall(double totalRainfall[], int n, int &monthIndex)

{

double high;

int i = 0;

high = totalRainfall[i];

while (i < n)

{

 if (totalRainfall[i] > high)

 {

  high = totalRainfall[i];

  monthIndex = i;

 }

 i++;

}

return high;

}

3 0
2 years ago
What is the coefficient of x101y99 in the expansion of<br> (2x − 3y)200?
Kruka [31]

You basically multiply 2 by 101 Nd 3 by 99 then but put that equation in parentheses then put the 200 on the outside of the parentheses (2•101-3•99)200

5 0
2 years ago
This component of the CPU is responsible for fetching the data, converting it, and de-converting
oksano4ka [1.4K]
<h3>Answer : C.Control Unit</h3>

<h3>Reason</h3>

In CPU there are two components name ALU and CU(Control unit) which ALU's function is to solve mathematic sequence and CU fuction is to vetch data to memory and manage commands and input & output.

#See pic for how the data proceed

8 0
2 years ago
Other questions:
  • The _____ command icon looks like a small clipboard with a page attached.
    6·1 answer
  • / List the seven basic internal components found in a computer tower.
    5·2 answers
  • According to the Bureau of Labor Statistics, how
    14·1 answer
  • A(n) _____________ is a system that prevents a specific type of information from moving between untrusted networks and private n
    9·1 answer
  • Lifelong learning _____. is only important for professionals with advanced degrees can be formal or informal includes formal cla
    12·1 answer
  • Translate We get up at 8 o'clock into Spanish in the box below:​
    9·1 answer
  • In this lab, your task is to complete the following: Enable all of the necessary ports on each networking device that will allow
    9·1 answer
  • Discuss how printing, publishing, and e-learning industries have transformed on evolved due to developments and advances the fie
    14·1 answer
  • For a parking application that lets you pay for parking via your phone, which of these is an example of a functional requirement
    8·1 answer
  • A statement that starts with a # symbol is called
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!