What is the incorrect action and why does it occur?

Specification: Ask the user for a character array and then reverse the characters. If the user entered Hello World, the new string would read dlroW olleH.
```
#include
using namespace std;
int main()
{
char saying[50],revSaying[50];
int i;
cout << ”\nEnter a saying.”;
cin.getline(saying,50);
for(i = 0; i< 50; ++i)
{
revSaying[i] = saying[50-i];
}
return 0;
}
```


A problem is that reversing action shown here will reverse all fifty characters. The leading characters in the revSaying will contain “trash” from the trailing portion of saying as well as the ‘\0’. If you tried to write the reversed C-string, you’d maybe see trash only.

To fix this, we should find the length of the input. This length is used to reverse just the valid characters in the string, like this:

```
int length = strlen(saying); // reports # of letters

for(i = 0; i < length; ++ i)
{
revSaying[i] = saying[ length – 1 - i ] ;
}
//then put a ‘\0’ at end of the revSaying
revSaying [ length – 1 ] = ‘\0’;
```

Computer Science & Information Technology

You might also like to view...

Once the ____________________ is complete, you can define the texture, transparency, and color for each of its surfaces.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

? ? Referring to the figure above, the minimum width for the element is ____.

A. 44em B. 60em C. 0 D. You cannot tell from the figure.

Computer Science & Information Technology

How are disk clusters numbered by Microsoft file structures?

What will be an ideal response?

Computer Science & Information Technology

A telephone microphone converts analog sound into electrical waveforms that cycle 2400 times a second. Each cycle is a(n) _______________.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology