翻译句子中的单词

XLR*_*04S 6 c arrays

我目前正在通过KN King的C编程:现代方法.我已经超过了第8章(阵列)的文本,我很想转到第9章,但我还没有在每一章的最后解决所谓的"编程项目".不幸的是,第14个... 让我烦恼.

编写一个程序来反转句子中的单词.

Enter a sentence: you can cage a swallow can't you?
Reversal of sentence: you can't swallow a cage can you?
Run Code Online (Sandbox Code Playgroud)

提示:使用循环逐个读取字符并将它们存储在一维char数组中.让循环停止在一个句点,问号或感叹号("终止字符"),它保存在一个单独的char变量中.然后使用第二个循环向后搜索数组以查找最后一个单词的开头.打印最后一个单词,然后向后搜索倒数第二个单词.重复,直到到达数组的开头.最后,打印终止字符.

我一直在考虑将一个单词定义为空格之间的一系列字符.因此,当到达空间时,向后移动,打印每个字符,直到找到另一个空格.我的第一个版本的程序只打印了第一个单词.它的当前版本只打印其他单词.我已经坚持了两天,所以任何帮助都真的很感激.这是我的代码,以及输出示例.希望我已正确记录我的代码.提前致谢!

/* Include the standard I/O library */
#include<stdio.h>

/* Define main */
int main(void) {

    /**
     * Declare an array of characters storing the sentence, as well as
     * a character representing the current character under cursor and
     * the terminating character
     */
    char sentence[100] = { ' ' }, c, tc;

    /**
     * Declare a loop counter already initialized at 0, an incremental
     * variable, as well as the size of the read sentence
     */
    int i = 0, j = 1, size = 0;

    /* Get the sentence */
    printf("Enter a sentence: \n");
    for(c = getchar(); (c != '.') && (c != '!') && 
        (c != '?') && (c != '\n'); c = getchar(), i++) {

        sentence[i] = c; /* Store the current character in the array */
        size++; /* Increase the sentence's size */
    }

    tc = c; /* Get the terminating character */

    /**
     * Go backward through the array, printing each sequence of characters
     * between spaces
     */
    for(i = 99; i >= 0; i--) {

        if(sentence[i] == ' ') {

            while(sentence[i + j] != ' ') {

                printf("%c", sentence[i + j]);
                j++;
            }

            j = 1; /* Reset the incremental variable */
            printf(" "); /* Print a tailing space */
        }
    }

    /**
     * Delete the tailing blank space and print the terminating character,
     * as well as a new line 
     */
    printf("\b%c\n", tc);

    return 0; /* Return 0 upon successful program execution */
}
Run Code Online (Sandbox Code Playgroud)

输出:

http://drp.ly/1nYt5J

Quo*_*nux 5

将每个字推入堆栈并从索引0读取堆栈到N-1


eru*_*orm 3

另一种值得思考的方法论:

you can cage a swallow can't you?
uoy t'nac wollaws a egac nac uoy?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
you t'nac wollaws a egac nac uoy?
^^^
you can't wollaws a egac nac uoy?
    ^^^^^
you can't swallow a egac nac uoy?
          ^^^^^^^
you can't swallow a egac nac uoy?
                  ^
you can't swallow a cage nac uoy?
                    ^^^^
you can't swallow a cage can uoy?
                         ^^^
you can't swallow a cage can you?
                             ^^^
Run Code Online (Sandbox Code Playgroud)

对于您想要反转的每件事(无论是整个句子还是单词):

  1. 找到开头和结尾
  2. 交换开始字符和结束字符
  3. “向内”移动一次
  4. 继续前进,直到到达“中间”

由于反转字符串块是一种常见操作,因此将其设为自己的函数是有意义的。由于该函数完成其工作所需的唯一信息是:

  1. 字符串
  2. 起始索引
  3. 结束索引

您认为该函数的参数是什么?

需要一遍又一遍地完成的另一件常见事情是“查找”某些内容,无论是空格还是标点符号。您可能需要自己编写此代码,或者如果您可以使用库函数,或者需要提示,请查找:

man strcspn
Run Code Online (Sandbox Code Playgroud)