简体   繁体   English

在C中反转二维字符串集的功能不会比第一个和最后一个字符反转更多

[英]Function to reverse a two-dimensional set of strings in C isn't reversing more than the first and last characters

this is my first post in Stack Overflow. 这是我在Stack Overflow中的第一篇文章。 If I am missing anything, please let me know and I will try to add more information. 如果我遗失任何内容,请告诉我,我会尝试添加更多信息。

The function below is only supposed to use pointer operations and not array operations, as part of an assignment. 下面的函数只应该使用指针操作而不是数组操作,作为赋值的一部分。

I have this function in C that is part of a larger program: 我在C中有这个函数,它是更大程序的一部分:

void reverseString(char strings[NUM_STRINGS][STRING_LENGTH])
{
    int i, j;
    char *ptr;                              //Declare pointer variable.
    for (i = 0; i < NUM_STRINGS; i++)
    {
        ptr = strings[i];
        do {                                //Here, we ignore the null terminators in the char array.
            ptr++;
        } while (*ptr != '\0');
        ptr--;                              //Iterate the pointer variable once downward.
        j = i;

        while (strings[j] < ptr)            //While loop for reversing the string
        {
            //printf("ptr: %d\n", ptr);
            //printf("strings[j]: %d\n", strings[j]);
            char temp = *strings[j];
            *strings[j++] = *ptr;
            *ptr-- = temp;
        }
    }
}

What it is supposed to do is accept a 2D char array with 4 strings and with each string holding up to 32 bytes of text. 它应该做的是接受一个包含4个字符串的2D字符串数组,每个字符串最多可容纳32个字节的文本。 Then, it reverses each string in-place in the array. 然后,它在阵列中就地反转每个字符串。 For example, if I input the four strings: 例如,如果我输入四个字符串:

Hello World Good morning 你好世界早上好

It is supposed to then return: 它应该返回:

olleH dlroW dooG gninrom olleH dlroW dooG gninrom

However, what ends up happening is that only the first and last characters of each string are reversed. 然而,最终发生的事情是只有每个字符串的第一个和最后一个字符被反转。 For example: 例如:

oellH dorlW dooG gorninm oellH dorlW dooG gorninm

I have tried different solutions such as using i instead of j in the while loop or using prefix ++ instead of suffix, but nothing has worked yet. 我尝试了不同的解决方案,例如在while循环中使用i代替j或使用前缀++而不是后缀,但还没有任何工作。 Any pointers as to what I should be looking for? 关于我应该寻找什么的任何指示?

Thank you. 谢谢。

The solution is below. 解决方案如下。 As described in my comment below, I added a secondary pointer instead of using strings[j] in the loop. 正如我在下面的评论中所描述的,我添加了一个辅助指针,而不是在循环中使用字符串[j]。 The pointer now references to the beginning of the string instead of the entire array of strings. 指针现在引用字符串的开头而不是整个字符串数组。

void reverseString(char strings[NUM_STRINGS][STRING_LENGTH])
{
    int i, j;
    char *ptr;                              //Declare pointer variable.
    char *ptr2;
    for (i = 0; i < NUM_STRINGS; i++)
    {
        ptr = strings[i];
        ptr2 = strings[i];
        do {                                //Here, we ignore the null terminators in the char array.
            ptr++;
        } while (*ptr != '\0');
        ptr--;                              //Iterate the pointer variable once downward.
        j = i;

        while (ptr2 < ptr)          //While loop for reversing the string
        {
            char temp = *ptr2;
            *ptr2++ = *ptr;
            *ptr-- = temp;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM