Write a program that accepts a C-string input from a file and reverses the contents of the string. Your program should work by using two pointers. The head pointer should be set to the address of the first character in the string, and the tail pointer should be set to the address of the last character in the string (i.e., the character before the terminating null). The program should swap the characters referenced by these pointers, increment head to point to the next character, decrement "tail" to point to the second-to-last character, and so on, until all characters have been swapped and the entire string is reversed.You are not allowed to use pointer arithmetic.
Here, temp is assigned to the first character of cstr. cstr is then set to point to the address of the object pointed to by tail. tail is then assigned the address of the object pointed to by temp. After this assignment, both tail and temp point to the same location (pointing to the same char).
Try simplifying your program. Think about it, you're reversing a string; cstr is a pointer to the start of the string. Make temp point to the end. Then, while temp greater than or equal to the position of cstr, decrement temp. You don't need tail.
One you are using a c++ string instead of a c-string.
1 2 3 4 5 6 7 8 9 10 11 12 13
char someString[30]; // c-style string.
string strString; // c++ style string.
// init the string to zeros or nulls;
for(int i = 0; i < 30; i++)
{
someString[i] = (char)0;
}
char *pStartofString = &someString[0]; //points to the first character in the c -style string.
char *pEndofString = &someString[28]; //points to the second to the last character in a c-style string.
if I wanted to know where the end of a entered string was I would scan the string character by character, either with a simple counter or pointer math, I would search for the first null position from the start of the string. I hope this clears up the issues you are having given the instructions you are given.