|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
Need help reversing a string
My problem is that when I run this program my output is not right, it should reverse the string, but instead it prints out random characters. Any help would be appreciated
PHP Code:
|
|
#2
|
||||
|
||||
|
Ok, since you allocated 11 bytes and print out a message asking for no more than 10 chars, you know that strings in C are NUL-terminated. So far, so good
![]() But in reverse(), you assume that the user will enter exactly 10 chars. If I enter "hey", you'll be copying way past the end of the string, since you start at 9. What you need to do, is look for the terminating NUL-byte, and then work your way back. Below is some code I slapped together for the occasion; don't look at it if you want to work it out yourself first. Code:
void reverse (char *s)
{
int i, j;
char c;
for (i = 0; s[i]; i++)
; /* do nothing */
i--; /* step back from the NUL-byte */
for (j = 0; j < i; i--, j++) {
c = s[j];
s[j] = s[i];
s[i] = c;
}
}
I hope this makes sense to you. ![]()
__________________
"A poor programmer is he who blames his tools." http://analyser.oli.tudelft.nl/ |
|
#3
|
||||
|
||||
|
Quote:
Here's another example: Code:
void reverse(char* s)
{
char temp, *end;
// point to last non-null char
end = s + strlen(s) - 1;
while (s < end) {
temp = *s;
*s++ = *end;
*end-- = temp;
}
}
OR, in C++: Code:
#include <string>
using namespace std;
int main(int argc, char** argv)
{
string s = "raboof";
string::reverse_iterator ri;
string s2;
for (ri = s.rbegin(); ri != s.rend(); ri++)
s2 += *(ri.base()-1);
cout << s2 << endl;
return 0;
}
|
|
#4
|
|||
|
|||
|
thanks to both of you
|
|
#5
|
|||
|
|||
|
Another issue is that you input the string into one instance of "string" and then you reverse another instance that is uninitialized.
|
|
#6
|
|||
|
|||
|
Unless we are doing homework there is
_strrev() from string.h
__________________
The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions. Frank Zappa |
|
#7
|
|||
|
|||
|
If you're not doing homework, you might as well save yourself a headache and use std::string and std::reverse (found in <algorithm>).
|
|
#8
|
|||
|
|||
|
hm...looking back over my problem I fixed this quite easially...
I changed: PHP Code:
into: PHP Code:
|
|
#9
|
|||
|
|||
|
I also needed to change the while loop, and added
PHP Code:
edit: should be != '\0' , but it didn't show up. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Need help reversing a string |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|