C语言中使用while循环反转字符串的代码问题求助
char* s Parameters Hey there! Let's walk through what's off with your current code and how to fix it, plus clarify how to work with the char* s parameter.
First: Understanding the char* s Parameter
In C, a string is a null-terminated ('\0') array of characters. The char* s parameter is a pointer that points to the first character of this string. You can use it two ways:
- Like an array:
s[0]gives the first character,s[1]the second, etc. - As a pointer you can increment/decrement:
s++moves the pointer to the next character,s--moves it back.
Important note: If you pass a string literal (like "hello") directly to this function, you can't modify it (they're stored in read-only memory). Always use a mutable character array (like char myStr[] = "abc de fgh";) when calling this reversal function.
What's Wrong With Your Current Code?
Let's break down the bugs one by one:
- Incorrect pointer initialization:
char* i = 0;setsito a null pointer (not the start of the string). You needchar* i = s;to point to the first character of the input string.char* j = strlen(s)-1;tries to assign an integer to a pointer. You need to calculate the address of the last valid character:char* j = s + strlen(s) - 1;(sincestrlen(s)gives the length of the string, adding that tospoints past the last character, so subtract 1 to get the last actual character before'\0').
- Wrong swap logic:
char* temp;declares a pointer, but you need to swap the character values (not the pointers themselves). Sotempshould be acharvariable, not a pointer.- Your current code swaps the pointers
iandjinstead of the characters they point to. This doesn't modify the original string at all—you're just moving where the pointers point, not changing the string's content.
Fixed Code
Here's the corrected version of your function, with comments explaining each step:
#include <string.h> // Required for strlen() void stringReverse(char* s) { // Point i to the start of the string char* i = s; // Point j to the last valid character (before the null terminator '\0') char* j = s + strlen(s) - 1; // Temp variable to hold a character during the swap char temp; // Swap characters while i is positioned before j while (i < j) { // Save the character at i's position to temp temp = *i; // Copy the character at j's position to i's position *i = *j; // Copy the saved character from temp to j's position *j = temp; // Move i forward one character i++; // Move j backward one character j--; } }
How to Use This Function
Here's an example of calling it in a main function to test the reversal:
#include <stdio.h> int main() { // Use a mutable character array (not a string literal) so we can modify it char str[] = "abc de fgh"; printf("Original string: %s\n", str); stringReverse(str); printf("Reversed string: %s\n", str); return 0; }
This will output:
Original string: abc de fgh Reversed string: hgf ed cba
Key Takeaways
- Always initialize pointers to valid memory locations (not
0unless you explicitly mean a null pointer). - When modifying string content, work with the values pointed to by the pointers (using
*pointersyntax), not the pointers themselves. - Remember that string literals are read-only—use character arrays if you need to modify the string's content.
内容的提问来源于stack exchange,提问作者user11361420

