Write a function in C language to store strings in reverse order

Questions 1-12

C Language Basic Example Questions 1-3 - Pointers
C Language Basic Example Questions 4-5 - Two-Dimensional Arrays
C Language Basics Example Questions 6-7 - Structure Chapter
C Language Basic Example Questions 8-9-Major Assignment
C Language Basic Example Questions 10-11-String , Pointers
12 Basic Examples of C Language - Linked List

Example 13

Please write a function void reverseString(char* str) that stores the input string in reverse order. Do not use any library function except stdio.

For example, for the input string "Hello, World!", after the function reverses the result, str should be "!dlroW,olleH".

test case

#include <stdio.h>

void reverseString(char* str);

int main() {
    
    
    char str[] = "Hello, World!";
    printf("Original string: %s\n", str);
    reverseString(str);
    printf("Reversed string: %s\n", str);
    return 0;
}

预期输出:
Original string: Hello, World!
Reversed string: !dlroW ,olleH

Answer to Example 13

Answer one

#include <stdio.h>

void reverseString(char *str);

int main()
{
    
    
    char str[] = "Hello, World!";
    printf("Original string: %s\n", str);
    reverseString(str);
    printf("Reversed string: %s\n", str);
    return 0;
}
void reverseString(char *str)
{
    
    
    char *first=str,*last=str,temp;
    while(*last)
        *last++;
    while(first!=(--last))
    {
    
    
        temp=*first;
        *first=*last;
        *last=temp;
        first++;
    }
}

char *first = str, *last = str, temp;:
defines two pointers first and last, pointing to the beginning and end of the string respectively, and a variable temp is used to swap characters.

while (*last):
Through the loop, the pointer last is moved to the end of the string, that is, pointing to the null character ‘\0’ at the end of the string.

while (first != (–last)):
Start a loop if the first pointer has not reached the last pointer, that is, it has not reached the midpoint of the string.

temp = *first;
*first = *last;
*last = temp;:
will The character pointed to by the first pointer is exchanged with the character pointed by the last pointer. After the swap, the first pointer moves backward and the last pointer moves forward.

first++;:
Move the first pointer backward one bit and continue the next loop.

Through the above operations, the reversal of the string is completed.

Answer two

#include <stdio.h>

void reverseString(char *str);

int main()
{
    
    
    char str[] = "Hello, World!";
    printf("Original string: %s\n", str);
    reverseString(str);
    printf("Reversed string: %s\n", str);
    return 0;
}

void reverseString(char *str)
{
    
    
    int length = 0;
    char *tempPtr;
    char temp;

    // 计算字符串的长度
    tempPtr = str;
    while (*tempPtr != '\0')
    {
    
    
        length++;
        tempPtr++;
    }

    // 交换字符串字符
    tempPtr = str;
    char *endPtr = str + length - 1;
    while (tempPtr < endPtr)
    {
    
    
        temp = *tempPtr;
        *tempPtr = *endPtr;
        *endPtr = temp;
        tempPtr++;
        endPtr--;
    }
}

int length = 0;:
defines a variable length, which is used to record the length of the string.

char *tempPtr;:
defines a pointer tempPtr, used to traverse the string.

char temp;:
defines a variable temp for exchanging characters.

tempPtr = str;:
Point the pointer tempPtr to the beginning of the string, prepare to traverse the string and calculate the length.

while (*tempPtr != ‘\0’):
By looping, traverse the string until it encounters the null character ‘\0’, which is the end of the string.

length++;
tempPtr++;:

For each non-null character, increase the value of length by 1 and move the tempPtr pointer backward to Next character.

tempPtr = str;:
Redirect the tempPtr pointer to the beginning of the string to prepare for character exchange operations.

char *endPtr = str + length - 1;:
defines a pointer endPtr, pointing to the last character of the string.

while (tempPtr < endPtr):
Perform character exchange operations through loops. In each loop, the characters pointed to by the tempPtr pointer and endPtr pointer are swapped, and the two pointers are moved backward and forward by one bit respectively.

temp = *tempPtr;
*tempPtr = *endPtr;
*endPtr = temp;:
Swap The character pointed to by the tempPtr pointer and endPtr pointer.

tempPtr++; endPtr–;:
Moves the tempPtr pointer one position backward and the endPtr pointer one position forward.

Guess you like

Origin blog.csdn.net/weixin_44499065/article/details/134346915