Hi. I need help in my c++ hw. Thanks! \"Iplement the following functions. Each f
ID: 3675542 • Letter: H
Question
Hi. I need help in my c++ hw. Thanks!
"Iplement 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 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:
Note: You may not use any c-string functions other than strlen(). If you use any other c-string functions (for example, strstr()), you will not get credit. Note, however, that functions such as toupper(), tolower(), isalpha(), and isspace() are NOT c-string functions, so you can use them.
Another note: in most cases it will be better to use a while loop that keeps going until it hits a '', 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.
Note that 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.
Here are the functions:
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.
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"
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.
This function returns the index in string s where the substring can first be found. For example if s is "Skyscraper" and substring is "ysc" the function would return 2. It should return -1 if the substring does not appear in the string.
This function returns true if the argument string is a palindrome. It returns false if it is no. A palindrome is a string that is spelled the same was as its reverse. For example "abba" is a palindrome. So is "hannah", "abc cba".
Do not get confused by white space characters. They should not get any special treatment. "abc ba" is not a palindrome. It is not identical to its reverse.
This function converts the c-string parameter to all uppercase.
This function returns the number of letters in the c-string.
Explanation / Answer
#include <iostream>
#include <cstring>
using namespace std;
int lastIndexOf( char const * const str , char const target );
void reverse( char * const str );
int replace( char * str , char const target , char const replacementChar );
int findSubstring( char const * const str , char const substring[] );
bool isPalindrome( char const * const str );
void toupper( char * str );
int numLetters( char const * str );
int main()
{
char string[] = "lightweight";
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): ";
char string3[10];
cin.getline( string3 , 10 );
cout << "your word: " << string3 << endl;
cout << "replace 'e' with '1': " << replace( string3 , 'e' , '1' ) << " replaced, " << string3 << endl;
if ( isPalindrome( string3 ) )
{
cout << string3 << " is a palindrome" << endl;
}
else
{
cout << string3 << " is not a palindrome" << endl;
}
cout << "number of letters: " << numLetters( string3 ) << endl;
cout << "last index of 't': " << lastIndexOf( string3 , 't' ) << endl;
reverse( string3 );
cout << "reversed: " << string3 << endl;
toupper( string3 );
cout << "to upper: " << string3 << endl;
return 0;
}
/*
Description: Returns the index of the last occurence of the target character in
str. The index count starts at zero. If the target character is not in the
given string then -1 is returned. It is assumed that the cstring is
terminated with the null character.
*/
//***************************************************************************//
int lastIndexOf( char const * const str , //in
char const target )// in
{
int lastIndex = -1 , index = 0;
while ( str[ index ] != 0 )
{
if ( str[ index ] == target )
{
lastIndex = index;
}
index++;
}
return lastIndex;
}
/*
Description: Takes in a cstring and reverse the order in place. It is assumed
that the string is terminated with the null character
*/
//***************************************************************************//
void reverse( char * const str ) //in
{
int length = strlen( str );
int replacements = length / 2;
char temp;
for ( int i = 0; i < replacements; i++ )
{
temp = str[ i ];
str[ i ] = str[ length - i - 1 ];
str[ length - i - 1 ] = temp;
}
}
/*
Description: Takes in a cstring, a target character and a replacement character
Everytime the target character occurs in the string, it is replaced with
the replacement character. It does not support replacing a character with the
null character. It is assumed that the string is terminated with the null
character.
*/
//***************************************************************************//
int replace( char * str , //inout
char const target , //in
char const replacementChar ) //in
{
int count = 0;
if ( target != 0 && replacementChar != 0 )
{
while ( *str != 0 )
{
if ( *str == target )
{
*str = replacementChar;
count++;
}
str++;
}
}
return count;
}
/*
Description: Searches for the occurence of substring in str. If the substring
is found then it returns the index of the first character of first occurence
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 str , //in
char const substring[] ) //in
{
int index = 0 , subIndex, foundAt = -1;
int strLength = strlen( str );
int subStrLength = strlen( substring );
while ( index <= strLength - subStrLength && foundAt < 0 )
{
subIndex = 0;
while ( str[ index + subIndex ] == substring[ subIndex ]
&& foundAt < 0 )
{
subIndex++;
if ( subIndex == subStrLength )
{
foundAt = index;
}
}
index++;
}
return foundAt;
}
/*
Description: Takes in a cstring and checks if the string is the same forwards
and backwards (aka a palindrome). Capitalization is taken into account.
Returns true if the string is a palindrome.
*/
//***************************************************************************//
bool isPalindrome( char const * const str ) //in
{
int index = 0;
int length = strlen( str );
int checks = length / 2;
while ( str[ index ] == str[ length - index - 1 ] && index < checks )
{
index++;
}
return index == checks;
}
/*
Description: Takes in a cstring 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 * str ) //inout
{
while ( *str != 0 )
{
if ( *str >= 'a' && *str <= 'z' )
{
*str -= 32;
}
str++;
}
}
/*
Description: Takes in a cstring and returns the total number of lower case and
upper case letters. All other characters are ignored. It is assumed that the
string is terminated with the null character.
*/
int numLetters( char const * str ) //in
{
int numLetters = 0;
while ( *str != 0 )
{
if ( ( *str >= 'A' && *str <= 'Z' ) || ( *str >= 'a' && *str <= 'z' ) )
{
numLetters++;
}
str++;
}
return numLetters;
}
Output
word: lightweight
last index of 't': 10
last index of 'z'-1
find substring 'ight': 1
reversed: thgiewthgil
find substring 'thgi': 0
find substring 'ight': -1
replace 'g' with 'h': 2 replaced, thhiewthhil
number of letters: 11
replace 'h' with '6': 4 replaced, t66iewt66il
number of letters: 7
replace 'p' with 'z': 0 replaced, t66iewt66il
to upper: T66IEWT66IL
T66IEWT66IL is not a palindrome
word: rotator
rotator is a palindrome
to upper: ROTATOR
ROTATOR is a palindrome
enter a string (10 characters or less): level
your word: level
replace 'e' with '1': 2 replaced, l1v1l
l1v1l is a palindrome
number of letters: 3
last index of 't': -1
reversed: l1v1l
to upper: L1V1L
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.