Problem statement: Write a C++ function library to manipulate and test character
ID: 3548540 • Letter: P
Question
Problem statement: Write a C++ function library to manipulate and test characters. In particular, a
menu?based system should be developed to perform the following operations on characters/strings:
1. MYisupper(char ch): This function tests whether the character (ch) passed as an argument is an
uppercase letter. It returns true (or non?zero number) if the argument is uppercase letter. Otherwise
return false (or zero) [10 points]
2. MYislower(char ch): This function tests whether the character (ch) passed as an argument is a
lowercase letter. It returns true (or non?zero number) if the argument is lowercase letter. Otherwise
return false (or zero) [5 points]
3. MYisalpha(char ch): This function tests whether the argument is a letter of the alphabet. [5 points]
4. MYisdigit(char ch): This function tests whether the argument is a digit from 0 through 9. [5 points]
5. MYtoupper(char ch): This function upper equivalent of its argument. [10 points]
6. MYtolower(char ch): This function lower equivalent of its argument. [10 points]
7. MYstrlen(char string[]): This function returns length of the string (ch) passed as an argument. [10
points]
8. MYstrcat(char string1[],char string2[]): This function accepts two strings as arguments and appends
the contents of the second string (string2) to the first string (string1). After the operation the first
string will be altered and the second string will be left unchanged. Make sure the size of the first
string should be large enough to hold both strings. Otherwise, print an error message that string1 is
not large enough to append string2. [10 points]
9. MYstrcpy(char string1[],char string2[]): This function accepts two strings as arguments and copies
the contents of the second string (string2) to the first string (string1). The second string will be left
unchanged after the operation. Make sure the size of the first string should be large enough to hold
string2. Otherwise, print an error message that string1 is not large enough to hold string2. [10
points]
10. MYstrcmp(char string1[],char string2[]): This function accepts two strings as arguments. If string1
and string2 are the same, the function returns 0. If string2 is alphabetically greater than string1 it
returns a negative number otherwise returns positive number [10 points]
11. Quit the program: This option terminates the program. [5 points]
Explanation / Answer
#include<iostream>
using namespace std;
bool MYisupper(char ch)
{
if(ch>='A' && ch<='Z')
return true;
return false;
}
bool MYislower(char ch)
{
if(ch>='a' && ch<='z')
return true;
return false;
}
bool MYisalpha(char ch)
{
if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
return true;
return false;
}
bool MYisdigit(char ch)
{
if(ch>='0' && ch<='9')
return true;
return false;
}
char MYtoupper(char ch)
{
if(ch>='a' && ch<='z')
return ch-32;
return ch;
}
char MYtolower(char ch)
{
if(ch>='A' && ch<='Z')
return ch+32;
return ch;
}
int MYstrlen(char string[])
{
int length=0;
while(string[++length]!='');
return length;
}
void MYstrcpy(char string1[],char string2[])
{
int i;
if(MYstrlen(string1) < MYstrlen(string2))
cout <<"string1 is not large enough to hold string2 " << endl;
else
{
for(i=0; i<MYstrlen(string2); i++)
string1[i] = string2[i];
}
string1[i] = '';
}
void MYstrcat(char string1[],char string2[])
{
int length1 = MYstrlen(string1);
int length2 = MYstrlen(string2);
int i;
for (i = 0; i <= length2; i++)
{
/* if(string1[length1+i] =='')
{
cout << "string1 is not large enough to hold string2 " << endl;
break;
}
*/
string1[length1+i] = string2[i];
}
string1[length1+i] = '';
}
bool MYstrcmp(char string1[],char string2[])
{
int i;
for(i=0;string1[i] == string2[i];i++)
if(string1[i] == '')
return 0;
return string1[i] - string2[i];
}
int main()
{
cout << MYisupper('V') << endl;
cout << MYislower('d') << endl;
cout << MYisalpha('s') << endl;
cout << MYisdigit('5') << endl;
cout << MYtoupper('t') << endl;
cout << MYtolower('F') << endl;
cout << MYstrlen("aswani") << endl;
char str1[5];
char str2[10] = "Ashok";
MYstrcpy(str1,str2);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.