Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Note: The built-in character analysis functions (isdigit(), islower(), etc.) may

ID: 3796586 • Letter: N

Question

Note: The built-in character analysis functions (isdigit(), islower(),
etc.) may not be used.

*****************************

Exercise 6.1 Write to an output file
Open an output file (stream) and write your identifying information to the
associated output file using a function. Put your standard output informa-
tion in a function named ShowHeader(). The file stream must be passed by
reference to the function. To do this, an & (ampersand) is used between the
data type (ofstream) and the name of the argument (I like to use fOut for
output file stream objects and fIn for input file stream object names).

**********************************
Exercise 6.2 Read from an input file
Read/Process an input file a character at a time. Open an input file (stream)
and read a single character on each pass through a loop (a while() loop is
probably easiest) until the end of file is reached. After reading the character,
write it to the output file created earlier. The output should contain the
same characters as the input file.

***************************************
Exercise 6.3 Character Analysis
Suggestion: Develop the remainder of this program in piecewise manner–
specifically, add one character analysis operation at a time, before adding
the next.
Analyze the characters in a file using functions to display their charac-
teristics, e.g., lower case, digit etc. The logic for analysis will be inside the
loop above.
Specifically, test if the character:
• is a letter.
• test if it is lower case or upper case.
• if the character is a letter, test if it is a vowel or consonant.
• is a digit.
• if the character is a digit, test if the value is odd or even.
• is a punctuation character.
• is a logical operator symbol.
Write a short function for most operations in the list above. If a character
is a letter, then it is either upper or lower case, so we only need to write one
function to test the case of the character. Do not use the built-in functions.

*********************
Note: there are no I/O (input/output) operations in the functions, only a
single character is examined.

Explanation / Answer

Following is the program which incorporates all your 3 questions.I didn't get what you need to do with showHeader().

#include<iostream>
#include<fstream>
using namespace std;

void printInfo(ofstream &,char ch);
int isLetter(char ch);
int isLowerOrUpper(char ch);
int isVowOrConst(char ch);
int isDigit(char ch);
int isOddOrEven(char ch);
int isPuncChar(char ch);
int isLogicOp(char ch);

int main(){

ifstream fin;
fin.open("input.txt", ios::in);

ofstream fout("./output.txt");
  
char ch = ' ';
int type = -1 , ret = 0;
  
while(!fin.eof()){

fin.get(ch);
/********* Process file **********/
if(isLetter(ch)){

ret = isLowerOrUpper(ch);
if(ret == 0){
cout<< ch << " is a lower case Letter ";
}
else if (ret == 1){
cout<< ch << " is a Upper case Letter ";
}
else if(isPuncChar(ch)){
cout << ch << " is a punctuation character ";
}
  
if(isLogicOp(ch)){
cout<< ch << " is a logical operator ";
}

}
else if (isDigit(ch)){
  
ret = isOddOrEven(ch);
if(ret == 1)
cout << ch << " is Even digit ";
else
cout << ch << " is an Odd digit ";

}

/********* Output value **********/
printInfo(fout,ch);
}

fout.close();
return 0;
}

//Print character into the output file
void printInfo(ofstream &fout,char ch)
{
fout << ch;

}

//Returns 1 if the character is a letter
int isLetter(char ch)
{
if( (ch >=65 && ch <=90) || (ch >=97 && ch <=122))
return 1;
else
return 0;
}

// Return 1 for upper case and 0 for lower case and -1 for invalid letter
int isLowerOrUpper(char ch)
{
if(ch>=65 && ch<=90)
return 1;
else if( ch>=97 && ch<=122)
return 0;
else
return -1;
}

//Returns "1" if vowel, "0" if consonant , "-1" if not a letter
int isVowOrConst(char ch)
{
int flag = -1;

if(!isLetter(ch))
return -1;

switch(ch){

case 'A':
case 'a':
case 'E':
case 'e':
case 'I':
case 'i':
case 'O':
case 'o':
case 'U':
case 'u':
flag = 1;
break;
default:
flag = 0;
break;
}

return flag;
}

// Return "1" if the character is digit otherwise "0"
int isDigit(char ch)
{
if(ch>= 48 && ch <= 57)
return 1;
else
return 0;
}


// Returns "1" is even , "0" if odd and "-1" for error case
int isOddOrEven(char ch)
{
if(!isDigit(ch))
return -1;

if( (ch - 38) % 2 == 0)
return 1;
else
return 0;
}

//Returns 1 if punctuation mark else 0
int isPuncChar(char ch)
{
switch(ch){
case ';':
case ',':
case '.':
case ':':
case '-':
case '?':
case '!':
case '/':
case '\':
case ''':
case '"':
case '(':
case ')':
case '_':
case '[':
case ']':
return 1;
default:
return 0;
}
}

//We have three logical operator "&&" , "||" and "!". Out of which only one can be represented as a single charactere
int isLogicOp(char ch)
{
if(ch == '!')
return 1;
else
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote