In: Computer Science
No initial opening comment with program details is required however, function specific documentation is still required for all functions.
Don't forget to use correct programing formatting style and documentation.
Directions:
Implement the following functions. Each function deals with null terminated C-Style strings. You can assume that any char array passed into the functions will contain null terminated data. Place all of the functions in a single file and then (in the same file) create a main() function that tests the functions thoroughly. You will lose points if you don't show enough examples to convince me that your function works in all cases.
Please note the following:
1) You may not use any variables of type string. This means that you should not #include <string>. Also, you may not use any c-string functions other than strlen(). If you use any other c-string functions, you will not get credit. Note, however, that functions such as toupper(), tolower(), isalpha(), isspace(), and swap() are NOT c-string functions, so you can use them. Also note that this prohibition is only for the functions that you are assigned to write. You can set-up a testing interface any way you want in your main() function. Make sure the algorithm is simple to follow and includes program statements that tests all the assigned functions.
2) In most cases it will be better to use a while loop that keeps going until it hits a '\0', rather than using a for loop that uses strlen() as the limit, because calling strlen() requires a traversal of the entire array. You could lose a point or two if you traverse the array unnecessarily.
3) None of these function specifications say anything at all about input or output. None of these functions should have any input or output statements in them. The output should be done in the calling function, which will probably be main(). The only requirement about main() is that it sufficiently test your functions. So, you can get user input in main() to use as arguments in the function calls, or you can use hard-coded values -- up to you, as long as the functions are tested thoroughly.
4) Here's a hint about how to work with c-strings in main(). There are several different ways that you could assign values to c-string variables, but I think the easiest is just hardcoding a lot of examples. For example:
char str1[] = "C++ is fun!"; char reverseTest[] = "13 dwarves"; char containerString[] = "Lord of the Strings"; char replacemenTest[] = "abaadabcd"; char palindrome[] = "radar";
Whatever you do, don't try to create and initialize a c-string on one line using pointer notation, like this:
char* str1 = "C++ is fun!";
This is dangerous (and officially deprecated in the C++ standard) because you haven't allocated memory for str1 to point at.
5) Since your program is being used for testing, in this case it is fine to have a very long main() function.
As mentioned, you can set-up a testing interface any way you want in your main() function. as long as the end result is that you demonstrate that the functions are tested and work correctly. Stepping through the sample program output below should be helpful as you begin putting together the testing interface in the main () function.
Final note: Make sure the algorithm and testing interface is simple to follow and the program is well formatted with appropriate documentation.
Here are the functions:
1) This function finds the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. The function should be case sensitive (so 'b' is not a match for 'B').
int lastIndexOf(const char* inString, char target)
2) This function alters any string that is passed in. It should reverse the string. If "flower" gets passed in it should be reversed in place to "rewolf". For efficiency, this must be done "in place", i.e., without creating a second array.
void reverse(char* inString)
3) This function finds all instances of the char 'target' in the string and replace them with 'replacementChar'. It returns the number of replacements that it makes. If the target char does not appear in the string it should return 0.
int replace(char* inString, char target, char replacementChar)
4) This function returns true if the argument string is a palindrome. It returns false if it is not. A palindrome is a string that is spelled the same as its reverse. For example "abba" is a palindrome. So are "hannah" and "abc cba".
Do not get confused by white space characters, punctuation, or digits. They should not get any special treatment. "abc ba" is not a palindrome. It is not identical to its reverse.
Your function should not be case sensitive. For example, "aBbA" is a palindrome.
You must solve this problem "in place", i.e., without creating a second array. As a result, calling your reverse() function from this function isn't going to help.
bool isPalindrome(const char* inString)
5) This function converts the c-string parameter to all uppercase.
void toupper(char* inString)
6) This function returns the number of letters in the c-string.
int numLetters(const char* inString)
Sample Screen Output:
Testing the lastIndexOf() function. The last index in the cstring 0123456789a1 with the character '0' is 0 The last index in the cstring 0123456789a1 with the letter 'a' is 10 The last index in the cstring 0123456789a1 with the character '1' is 11 The last index in the cstring 0123456789a1 with the letter 'x' is -1 Testing the reverse() function. The original string is "reverse". The reversed string is "esrever". The original string is "13 dwarves". The reversed string is "sevrawd 31". Testing the replace() function. The number of replacements of 'a' for 'z' in abaadabcd is 4 Now the string is zbzzdzbcd The number of replacements of 'a' for 'z' in zbzzdzbcd is 0 Now the string is zbzzdzbcd Testing the isPalindrome() function. wertytre is not a palindrome radar is a palindrome abc ba is not a palindrome ra ar is a palindrome abccBa is a palindrome abcdefgha is not a palindrome (an empty string) is a palindrome Testing the toupper() function. Lord of the Strings becomes LORD OF THE STRINGS Testing the numLetters() function. 0123456789a1 has this many letters: 1 Process returned 0 (0x0) execution time : 0.065 s Press any key to continue.
Code:-
/ Header file section
#include <iostream>
#include <cstring>
using namespace std;
// Phase-1 prototype declarations
// #1
int lastIndexOf(char const * const inString, char const target);
// #2
void reverse(char * const inString);
// #3
int replace(char * inString, char const target, char const replacementChar);
// #4
int findSubstring(char const * const inString, char const substring[]);
// #5
bool isPalindrome(char const * const inString);
// #6
void toupper(char * inString);
// #7
int numLetters(char const * inString);
// Phase-2 prototype declarations
// #1
void read(char*& readMe);
// # 2
void concatenate(char*& left, const char* right);
// main function
int main()
{
// Declare a variable
char *string = NULL;
// calling read() function
read(string);
// Example-1 for all cases
// Function call
cout << " Example-1 for all cases: " << endl;
cout << "word: " << string << endl;
cout << "last index of 't': " << lastIndexOf(string, 't')
<< endl;
cout << "last index of 'z'" << lastIndexOf(string, 'z')
<< endl;
cout << "find substring 'ight': "
<< findSubstring(string, "ight") << endl;
reverse(string);
cout << "reversed: " << string << endl;
cout << "find substring 'thgi': "
<< findSubstring(string, "thgi") << endl;
cout << "find substring 'ight': "
<< findSubstring(string, "ight") << endl;
cout << "replace 'g' with 'h': "
<< replace(string, 'g', 'h')
<< " replaced, " << string << endl;
cout << "number of letters: " << numLetters(string)
<< endl;
cout << "replace 'h' with '6': "
<< replace(string, 'h', '6') << " replaced, "
<< string << endl;
cout << "number of letters: " << numLetters(string)
<< endl;
cout << "replace 'p' with 'z': "
<< replace(string, 'p', 'z') << " replaced, "
<< string << endl;
toupper(string);
cout << "to upper: " << string << endl;
if (isPalindrome(string))
{
cout << string << " is a palindrome" << endl;
}
else
{
cout << string << " is not a palindrome" << endl;
}
char string2[] = "rotator";
cout << endl << "word: " << string2 << endl;
if (isPalindrome(string2))
{
cout << string2 << " is a palindrome" << endl;
}
else
{
cout << string2 << " is not a palindrome" << endl;
}
toupper(string2);
cout << "to upper: " << string2 << endl;
if (isPalindrome(string2))
{
cout << string2 << " is a palindrome" << endl;
}
else
{
cout << string2 << " is not a palindrome" << endl;
}
cout << endl
<< "enter a string (10 characters or less): ";
// Declare variable
char string3[10];
// prompt and read string from the user
cin.getline(string3, 10);
cout << " Example-2 for all cases: " << endl;
// Display a string
cout << "your word: " << string3 << endl;
// Call replace() function
cout << "replace 'e' with '1': "
<< replace(string3, 'e', '1') << " replaced, "
<< string3 << endl;
// Call isPalindrome() function
if (isPalindrome(string3))
{
cout << string3 << " is a palindrome" << endl;
}
else
{
cout << string3 << " is not a palindrome" << endl;
}
// Call numLetters() function
cout << "number of letters: " << numLetters(string3)
<< endl;
// Call lastIndexOf() function
cout << "last index of 't': "
<< lastIndexOf(string3, 't') << endl;
// Call reverse() function
reverse(string3);
cout << "reversed: " << string3 << endl;
// Call toupper() function
toupper(string3);
cout << "to upper: " << string3 << endl;
// declaring another string
char *right = NULL;
// reading right
read(right);
// now concatenating string and right
concatenate(string, right);
cout << "Concatenate string: " << string << endl;
system("pause");
return 0;
}
// Returns the index of the last occurrence of the target
// character in str.
int lastIndexOf(char const * const inString, //in
char const target)// in
{
// The index count starts at zero.
int lastIndex = -1, index = 0;
//If the target character is not in the
// given string then -1 is returned. It is assumed that
// the c-string is terminated with the null character.
while (inString[index] != 0)
{
if (inString[index] == target)
{
lastIndex = index;
}
index++;
}
return lastIndex;
}
// Takes in a c-string and reverse the order in place. It is
// assumed that the string is terminated with the null
// character
void reverse(char * const inString) //in
{
int length = strlen(inString);
int replacements = length / 2;
char temp;
for (int i = 0; i < replacements; i++)
{
temp = inString[i];
inString[i] = inString[length - i - 1];
inString[length - i - 1] = temp;
}
}
// Takes in a c-string, a target character and a replacement
// character does not support replacing a character with the // null character.
int replace(char * inString, //inout
char const target, //in
char const replacementChar) //in
{
// Declare variables
int count = 0;
// Every time the target character occurs in the
// string, it is replaced with the replacement character.
if (target != 0 && replacementChar != 0)
{
//It is assumed that the string is terminated with
// the null character.
while (*inString != 0)
{
// Replace with a character
if (*inString == target)
{
*inString = replacementChar;
count++;
}
inString++;
}
}
return count;
}
// Searches for the occurrence of substring in str. If the
// substring is found then it returns the index of the first
// character of first occurrence of the match. If no match is
// found, -1 is returned. It is assumed that the strings
// passed in are terminated with the null character.
int findSubstring(char const * const inString,
char const substring[])
{
// Declare variables
int index = 0, subIndex, foundAt = -1;
int strLength = strlen(inString);
int subStrLength = strlen(substring);
while (index <= strLength - subStrLength && foundAt < 0)
{
subIndex = 0;
while (inString[index + subIndex] ==
substring[subIndex]
&& foundAt < 0)
{
subIndex++;
if (subIndex == subStrLength)
{
foundAt = index;
}
}
index++;
}
return foundAt;
}
// Takes in a c-string and checks if the string is the same
// forwards and backwards (abba a palindrome). Capitalization
// is taken into account. Returns true if the string is a
// palindrome.
bool isPalindrome(char const * const inString)
{
// Declare variables
int index = 0;
int length = strlen(inString);
int checks = length / 2;
while (inString[index] == inString[length - index - 1] &&
index < checks)
{
index++;
}
return index == checks;
}
// Takes in a c-string and converts all the lower case letters
// to upper case. All other characters are ignored. It is
// assumed that the string is terminated with the null character.
void toupper(char * inString)
{
// repeat this loop until end of the string
while (*inString != 0)
{
// Covert characters into uppercase
if (*inString >= 'a' && *inString <= 'z')
{
*inString -= 32;
}
inString++;
}
}
// Takes in a c-string and returns the total number of lower
// case and upper case letters.
int numLetters(char const * inString)
{
int numLetters = 0;
// It is assumed that the string is terminated with
// the null character.
while (*inString != 0)
{
// Check the number of letters and all other
// characters are ignored.
if ((*inString >= 'A' && *inString <= 'Z') ||
(*inString >= 'a' && *inString <= 'z'))
{
numLetters++;
}
inString++;
}
return numLetters;
}
// Takes in a c-string and the function will stop reading when
// it gets to a newline character. It is assumed that the
// string is terminated with the null character.
void read(char*& readMe)
{
if (readMe != NULL)
{
// if it is not null, then first delete it
delete readMe;
}
// allocating memory to readMe
readMe = new char[81];
// reading user input
cout << "Enter input string: " << endl;
cin.getline(readMe, 80);
}
// Takes in a c-string and function modifies left
therefore
// that it is equal to the concatenation of left and right.
void concatenate(char*& left, const char* right)
{
// getting size of left
// using strlen method
int lSize = strlen(left);
// getting size of right
int rSize = strlen(right);
// now allocating new block of memory of size equal to
//(lSize+rSize+1)
char *res = new char[lSize + rSize + 1];
// now copying left to new array
// using strcpy function
strcpy(res, left);
// now concating right to res
// using strcat function
strcat(res, right);
// now assigning res to left
left = res;
}
Output Screenshot:-
--------------------------
Please give me a UPVOTE. Thank you :)