Write a program with three functions: upper, lower, and reverse. The upper funct
ID: 3623034 • Letter: W
Question
Write a program with three functions: upper, lower, and reverse.The upper function should accept a pointer to a c-string as an argument. It should step through each character in the string, converting it to uppercase.
The lower function, to should accept a pointer to a c-string as an argument. It should step through each character in the string converting it to lower case.
Like upper and lower, reverse should also accept a pointer to a c-string. As it steps through the string, it should test each character to determine whether it is upper or lowercase. If a character is uppercase, it should be converted to lowercase. Likewise, if a character is upper case it should be converted to lowercase. Likewise, if a character is lowercase is should be converted to uppercase.
Test the function by creating a file called seuss.dat
Paste the text: "Then the Voom...It went VOOM! And, oh boy! What a VOOM! Now, don't ask me what a Voom is. I never will know. But, boy! Let me tell you. It does clean up SNOW!
into the file.
Count each character as it is read into the file and then dynamically allocate an array that will hold these characters
=>Pass the array to the functions in the following order: reverse, lower, and upper.
Explanation / Answer
please rate - thanks
you never got back to me
#include <iostream>
#include <fstream>
using namespace std;
void upper(char*,int);
void lower(char*,int);
void reverse(char*,int);
void print(char*,int,string);
int main()
{ifstream input;
int n=0,i,max=30;
char* a=new char[max];
input.open("seuss.dat"); //open file
if(input.fail()) //is it ok?
{ cout<<"file did not open please check it ";
system("pause");
return false;
}
while(input.get(a[n]))
{n++;
if(n>=max)
{max+=10;
char* temp=new char[max];
for(i=0;i<n;i++)
temp[i]=a[i];
delete[] a;
a = temp;
}
}
input.close();
print(a,n,"original ");
reverse(a,n);
print(a,n,"reversed ");
lower(a,n);
print(a,n,"after lower ");
upper(a,n);
print(a,n,"after upper ");
delete[] a;
system("pause");
return 0;
}
void upper(char* a,int n)
{int i;
for(i=0;i<n;i++)
if(islower(*(a+i)))
*(a+i)=toupper(*(a+i));
}
void lower(char* a,int n)
{int i;
for(i=0;i<n;i++)
if(isupper(*(a+i)))
*(a+i)=tolower(*(a+i));
}
void reverse(char* a,int n)
{int i;
for(i=0;i<n;i++)
if(isupper(*(a+i)))
*(a+i)=tolower(*(a+i));
else
if(islower(*(a+i)))
*(a+i)=toupper(*(a+i));
}
void print(char* a,int n,string mess)
{cout<<mess;
int i;
for(i=0;i<n;i++)
cout<<*(a+i);
cout<<endl<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.