Write a C++ function library to manipulate and test characters. In particular, a
ID: 3548010 • Letter: W
Question
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)
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)
3. MYisalpha(char ch): This function tests whether the argument is a letter of the alphabet.
4. MYisdigit(char ch): This function tests whether the argument is a digit from 0 through 9.
5. MYtoupper(char ch): This function upper equivalent of its argument.
6. MYtolower(char ch): This function lower equivalent of its argument. 7. MYstrlen(char string[]): This function returns length of the string (ch) passed as an argument.
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.
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. 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
11. Quit the program: This option terminates the program.
Explanation / Answer
Make two files say mystringlibrary.h and mystringlibrary.cppmystringlibrary.h has just function declaration like
Eg: int MYisupper(char ch);
mystringlibrary.cpp has the full function definition like
Eg: int MYisupper(char ch) { int flag = 0; int ascii = ch; if(ascii >=65 && ascii <= 90) flag = 1; return(flag); } Compile the .cpp file and don't write any main() function in it. It will generate a mystringlibrary.o file after compiling. This .o and the .h files are all you need to use the library. The .cpp file is only needed to make some changes to the functions in it or adding some new functions. Then include the .h file in the program in which you want to use it by #include "mystringlibrary.h" int flag = 0; int ascii = ch; if(ascii >=65 && ascii <= 90) flag = 1; return(flag); } Compile the .cpp file and don't write any main() function in it. It will generate a mystringlibrary.o file after compiling. This .o and the .h files are all you need to use the library. The .cpp file is only needed to make some changes to the functions in it or adding some new functions. For any further clarification, just comment in the question, you have asked, I will be happy to help. The following are the codes for the functions : int MYisupper(char ch) { int flag = 0; int ascii = ch; if(ascii >=65 && ascii <= 90) flag = 1; return(flag); }
int MYislower(char ch) { int flag = 0; int ascii = ch; if(ascii >=97 && ascii <= 122) flag = 1; return(flag); }
int MYisalpha(char ch) { int flag = 0; int ascii = ch; if((ascii >= 65 && ascii <= 90) || (ascii >=97 && ascii <= 122)) flag = 1; return(flag); }
int MYisdigit(char ch) { int flag = 0; int ascii = ch; if(ascii >=48 && ascii <= 57) flag = 1; return(flag); }
char MYtoupper(char ch) { int i = ch; i = i - 32; char new_ch = (char) i; return(new_ch); }
char MYtolower(char ch) { int i = ch; i = i + 32; char new_ch = (char) i; return(new_ch); }
void MYstrcat(char string1[],char string2[]) { int i,j; for(i = 0;string1[i] != '';++i); for(j = 0;string2[j] != '';++i,++j) string1[i] = string2[j]; string1[i] = ''; }
void MYstrcpy(char string1[],char string2[]) { int i; for(i = 0;string2[i] != '';++i) string1[i] = string2[i]; string1[i] = ''; }
int MYstrcmp(char string1[],char string2[]) { int i,flag = 0; for (i = 0;(string1[i] != '') && (string2[i] != ''); ++i) { if(string1[i] != string2[i]) { if(string1[i] < string2[i]) flag = -1; else flag = 1; break; } } if((string1[i] == '') && (string2[i] != '')) flag = -1; else if((string1[i] != '') && (string2[i] == '')) flag = 1; return(flag); }
void quit() { exit(0); } int MYisupper(char ch) { int flag = 0; int ascii = ch; if(ascii >=65 && ascii <= 90) flag = 1; return(flag); }
int MYislower(char ch) { int flag = 0; int ascii = ch; if(ascii >=97 && ascii <= 122) flag = 1; return(flag); }
int MYisalpha(char ch) { int flag = 0; int ascii = ch; if((ascii >= 65 && ascii <= 90) || (ascii >=97 && ascii <= 122)) flag = 1; return(flag); }
int MYisdigit(char ch) { int flag = 0; int ascii = ch; if(ascii >=48 && ascii <= 57) flag = 1; return(flag); }
char MYtoupper(char ch) { int i = ch; i = i - 32; char new_ch = (char) i; return(new_ch); }
char MYtolower(char ch) { int i = ch; i = i + 32; char new_ch = (char) i; return(new_ch); }
void MYstrcat(char string1[],char string2[]) { int i,j; for(i = 0;string1[i] != '';++i); for(j = 0;string2[j] != '';++i,++j) string1[i] = string2[j]; string1[i] = ''; }
void MYstrcpy(char string1[],char string2[]) { int i; for(i = 0;string2[i] != '';++i) string1[i] = string2[i]; string1[i] = ''; }
int MYstrcmp(char string1[],char string2[]) { int i,flag = 0; for (i = 0;(string1[i] != '') && (string2[i] != ''); ++i) { if(string1[i] != string2[i]) { if(string1[i] < string2[i]) flag = -1; else flag = 1; break; } } if((string1[i] == '') && (string2[i] != '')) flag = -1; else if((string1[i] != '') && (string2[i] == '')) flag = 1; return(flag); }
void quit() { exit(0); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.