c语言如何使字符数组倒叙?

请问如何将字符数组倒叙并忽略标点符号?

比如以下实例

首先,提示用户用英语输入一句话(假设用户输入的单词数小于15,每个单词长度小于15,并且句子结尾有标点符号);
然后,解析用户输入内容中的所有单词(无需保留标点符号),并将其逆序存放到数组result中,每个单词间用空格分隔;
最后,输出result的内容。
例如:如果用户输入:“How are you?”,则系统输出:“you are How”。


#include <stdio.h>
#include <ctype.h>
#include <string.h>

void reverse(char *str)
{
    int len = strlen(str);
    for (int i = 0, j = len - 1; i < j; ++i, --j) {
        while (i < len && ispunct(str[i])) ++i;
        while (j >= 0 && ispunct(str[j])) --j;
        if (i >= j) break;
        str[i] ^= str[j] ^= str[i] ^= str[j];
    }
}

int main()
{
    char input[128];
    printf("Please enter a sentence in English: ");
    fgets(input, sizeof(input), stdin);
    input[strcspn(input, "\n")] = '\0'; // remove trailing newline

    int len = strlen(input);
    if (len > 0 && ispunct(input[len-1]))
        input[len-1] = '\0'; // remove ending punctuation

    char *words[15];
    int word_count = 0;
    char *p = strtok(input, " ");
    while (p != NULL && word_count < 15) {
        words[word_count++] = p;
        p = strtok(NULL, " ");
    }

    char result[128] = {0};
    for (int i = word_count - 1; i >= 0; --i) {
        strcat(result, words[i]);
        if (i > 0) strcat(result, " ");
    }

    reverse(result);
    printf("%s\n", result);

    return 0;
}

首先,从标准输入获取用户输入的英文句子。
然后,将句子结尾的标点符号(如果有的话)删除。
接着,使用strtok()函数解析输入内容中的所有单词,并将它们存入一个字符指针数组中。
然后,使用strcat()函数将每个单词逆序添加到result字符数组中,并在单词之间添加空格。
最后,使用reverse()函数将result字符数组反转