c++ program Part Five. Programs. Be able to write some short program segments Po
ID: 3757023 • Letter: C
Question
c++ program
Part Five. Programs. Be able to write some short program segments Possible Problems: (Study all homework problems) o Read in data and perform a calculation given some equation o Produce some design using. (from homework two) o Open a file, read and print some data from the file o Given some block of text find and replace a given substring. (string Operations) Count the number words in a paragraph Count the number of times an input letter (e.g. 'm) appears in a block of text. o Find and replace a substring within a paragraph. . The program header will be provided. #includes etc.Explanation / Answer
//Program to search a input letter in a block of text
#include <iostream>
#include<stdio.h>
using namespace std;
int main()
{
char a[200],b;
int i,count = 0;
cout<<"Enter a block of text: ";
gets(a); //Input of block of text
cout<<" Enter a character to search: ";
cin>>b; //input of search character
for(int i = 0; a[i] != ''; ++i) //Loop to search the block of text
{
if(a[i] == b)
++count;
}
cout << " The number of times " << b << " appeared is: " << count;
return 0;
}
// Program to count words in paragraph
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
char para[200];
int i,flag=1;
cout<<"Enter the paragraph: "; //input of paragraph
gets(para);
for(i=0;para[i]!='';++i) //Loop to count number of words
{
if(para[i]==' ') // Counting the number of spaces
flag++;
}
cout<<" The number of words in the paragraph are: "<<flag;
return 0;
}
// Open a file, reading and printing from it
#include <iostream>
#include <fstream>
using namespace std;
int main () {
char x[100];
ifstream file; //To read a file
file.open("xyz.dat"); //opening of file xyz.dat
if(!file)
cout<<"Unable to open file";
else
{
file.seekg(0); //Moving to the start of file
cout << "Reading from the file " << endl;
while(file.eof()==0)
{
file>>x; //From file to array
cout<<x<<" "; //From array to output
}
file.close(); //Closing the file
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.