Hi guys,
I'm trying to create a simple function that reverses a string without using any in-built functions. I have code that should work but it just crashes on the first loop. I have no idea whats wrong and its rather frustrating being such an easy problem! Any ideas?
#include <iostream>
void reverse(char *p)
{
char temp, *pCopy;
int i, j, sizeOfString;
sizeOfString = 0;
pCopy = p;
while(*pCopy != '\o') // Calculates size of p or pCopy
{
sizeOfString ++;
*pCopy ++;
}
for(i = 0, j = sizeOfString; i < j; i ++, j --) // Reverses the string
{
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
int main()
{
char *testString = "Hello";
reverse(testString);
std::cout << testString << std::endl;
return 0;
}