You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言中使用while循环反转字符串的代码问题求助

Fixing Your String Reversal Code & Understanding 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:

  1. Incorrect pointer initialization:
    • char* i = 0; sets i to a null pointer (not the start of the string). You need char* 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; (since strlen(s) gives the length of the string, adding that to s points past the last character, so subtract 1 to get the last actual character before '\0').
  2. Wrong swap logic:
    • char* temp; declares a pointer, but you need to swap the character values (not the pointers themselves). So temp should be a char variable, not a pointer.
    • Your current code swaps the pointers i and j instead 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 0 unless you explicitly mean a null pointer).
  • When modifying string content, work with the values pointed to by the pointers (using *pointer syntax), 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:34:06