********************************************** hello there is a homework problem
ID: 3692476 • Letter: #
Question
**********************************************
hello there is a homework problem that i do not understand the issue with my code if you can give any insight it qould be very much appreciated.
********************************************
Part 2 - Write a C++ function that will reverse the characters in a C++ string. The function prototype is: void reverse( char word[] ) Note: - This function cannot have any local variables inside the function that are arrays. An Algorithm is outlined below: Declare two integer subscripts... left and right Use a loop to find the position in the word array where '' is located Set right to be one less than than this Set left to 0 Start second Loop switch word[left] with word[right] inc left by 1 decr right by 1 End loop Consider this sample word containing the string "together" ...the 't' character is the first char but in position 0 ...the NULL character "/0" is the 9th char but in position 8 left will be 0, right will be 8 pass 1="RogetheT" left = 1 right = 7 pass 2="rEgethOt" left = 2 right = 6 pass 3="reHetGot" left = 3 right = 5 pass 4="rehTEgot" left = 4 right = 4 ...
****************************************************************************
if you could look at the code and help to show where im going wrong.
***************************************************************************
#include <iostream>
using namespace::std;
const int MAX_SIZE = 15; // Max word size of word, 14 chars plus ''
void reverse(char *); // function prototype
void main()
{
char word[MAX_SIZE];
cout << endl << "Enter a word : ";
cin >> word;
cout << "You entered the word " << word << endl;
reverse(word);
cout << "The word in reverse order is " << word << endl;
}//END MAIN
void reverse(char *word)
{
// you cannot have any local array variables inside this function
int temp = strlen(word);
char swap;
for (int i = 0; i != '' / 2; i++)
{
swap = word[i];
word[i] = word[temp - i - 1];
word[temp - i - 1] = swap;
}
}
/*
current output
Enter a word : hello
You entered the word hello
The word in reverse order is hello
Press any key to continue
*/
Explanation / Answer
Please follow the code and comments for description :
#include <iostream>
#include <cstring> // need to enter the header file to eliminate the scope error of the strlen function
using namespace::std;
const int MAX_SIZE = 15; // Max word size of word, 14 chars plus ''
void reverse(char *); // function prototype
int main()
{
char word[MAX_SIZE];
cout << endl << "Enter a word : ";
cin >> word;
cout << "You entered the word " << word << endl;
reverse(word);
cout << "The word in reverse order is " << word << endl;
return 0;
}//END MAIN
void reverse(char *word)
{
// you cannot have any local array variables inside this function
int temp = strlen(word);
char swap;
for (int i = 0; i != temp / 2; i++) // in the loop the nul character has been taken which does not make the loop iterable so I have changed it to temp where the length of the word is saved
{
swap = word[i];
word[i] = word[temp - i - 1];
word[temp - i - 1] = swap;
}
}
The changes of the code have been bolded for clear understanding.
Hope this is helpful.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.