A function serves to encapsulate a sequence of statements. Calling the function
ID: 3557891 • Letter: A
Question
A function serves to encapsulate a sequence of statements. Calling the function executes the statements within the function. Variables defined within the function are local they only exist within the function and are independent of any other variables defined elsewhere. Write a program that will read a text file line by line and count the uppercase and lowercase characters in the file. An integer function should be written to count the uppercase characters and a integer function to count lower case both have an array as the parameter. The file is specified by the user and the counts are printed at the end Write a program that will read a text file line by line and is able to capitalize all the lines in the file or convert them lower case. The input file is specified by the user and also specified whether he wants the output in lower case or capitalized, i.e., your program should be able to do either one and you must query the user. Write a function to capitalize a string and a separate function to convert lower case. For both of these functions, you will send a string and return nothing. There should be no i/o in those functions. You will also need to count the number of consonants in the file and count the number of commas in the file. A function should be written to count the consonants and one to count the number of commas in the file. A function should be written to count the consonants and one to count the commas. In both cases, you will send a string to the function and return an integer. There should be no i/o in the functions. All i/o in your program should be done using streams. Good program structure, indentation, comments, your name, meaningful variable names and prompts are required.Explanation / Answer
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <fstream>
#include <algorithm>
using namespace std;
int commas(char *buffer)
{
int comma = 0;
for(int i = 0;buffer[i] != ''; i++)
{
if(buffer[i] == ',')
comma++;
}
return comma;
}
int consonents(char *buffer)
{
int consonent = 0;
for(int i = 0; buffer[i] != ''; i++)
{
if(( buffer[i] >= 'a' && buffer[i] <= 'z') || (buffer[i] >= 'A' && buffer[i] <= 'Z'))
{
switch (buffer[i])
{
case 'a':
case 'e':
case 'o':
case 'u':
case 'i':
case 'A':
case 'E':
case 'O':
case 'U':
case 'I':
continue;
default:
consonent++;
}
}
}
return consonent;
}
void con_uc(char * buffer)
{
for(int i = 0; buffer[i] != ''; i++)
{
if( buffer[i] >= 'a' && buffer[i] <= 'z')
buffer[i] = buffer[i] - 'a' + 'A';
}
}
void con_lc(char * buffer)
{
for(int i = 0; buffer[i] != ''; i++)
{
if( buffer[i] >= 'A' && buffer[i] <= 'Z')
buffer[i] = buffer[i] - 'A' + 'a';
}
}
int main()
{
int comma = 0, consonent = 0;
char name[80], buffer[80];
cout<<"Enter a file name"<<endl;
cin>>name;
int line = 1;
ifstream infile(name);
ofstream outfile("temp.txt");
cout<<"Enter 1 to convert to Uppcase and 2 to Lowercase"<<endl;
int case_flag ;
cin>>case_flag;
while( infile.getline(buffer, 80))
{
comma += commas(buffer);
consonent += consonents(buffer);
if(case_flag == 1)
con_uc(buffer);
else
con_lc(buffer);
outfile<<buffer<<' ';
}
cout<<"The total number of commas and consonents in file are "<<comma<<" "<<consonent<<endl;
infile.close();
outfile.close();
ifstream inf("temp.txt");
ofstream outf(name);
while(inf.getline(buffer, 80))
outf << buffer<<' ';
inf.close();
outf.close();
remove("temp.txt");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.