倒置句子中的单词 [英] Reversing words in a sentence

查看:33
本文介绍了倒置句子中的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在通过 K.N.King 的 C 编程:一种现代方法.我已经完成了第 8 章(数组)的课文,我很想进入第 9 章,但我还没有解决每章末尾所谓的编程项目".不幸的是,第 14 次......让我感到烦恼.

<块引用>

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

输入一句话:你可以笼养一只燕子不是吗?句子倒置:你不能吞下笼子,对吗?

<块引用>

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

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

代码

/* 包含标准 I/O 库 */#include/* 定义主 */int main(void) {/*** 声明一个存储句子的字符数组,以及* 一个表示当前光标所在字符的字符和* 终止符*/字符句[100] = { ' ' }, c, tc;/*** 声明一个循环计数器已经初始化为 0,一个增量* 变量,以及读取句子的大小*/int i = 0,j = 1,大小 = 0;/* 获取句子 */printf("请输入一句话:\n");for(c = getchar(); (c != '.') && (c != '!') &&(c !='?') &&(c != '\n');c = getchar(), i++) {句子[i] = c;/* 将当前字符存入数组 */尺寸++;/* 增加句子的大小 */}tc = c;/* 获取终止符 *//*** 向后遍历数组,打印每个字符序列* 空格之间*/for(i = 99; i >= 0; i--) {if(sentence[i] == ' ') {while(sentence[i + j] != ' ') {printf("%c", 句子[i + j]);j++;}j = 1;/* 重置增量变量 */printf("");/* 打印尾空格 */}}/*** 删除尾随空格并打印终止符,* 以及一个新行*/printf("\b%c\n", tc);返回0;/* 程序执行成功返回 0 */}

输出:

http://drp.ly/1nYt5J

解决方案

另一种思考方法:

你可以笼养一只燕子不是吗?uoy t'nac wollaws 一个 egac nac uoy?^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^你t'nac wollaws 一个egac nac uoy?^^^你不能让一个 egac nac uoy 成为 wolaws 吗?^^^^^你不能吞下 egac nac uoy 吗?^^^^^^^^你不能吞下 egac nac uoy 吗?^你不能吞下一个笼子 nac uoy?^^^^你不能吞下笼子可以吗?^^^你不能吞下笼子吗?^^^

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

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

因为反转一个字符串块是一个常见的操作,所以让它成为自己的函数是有意义的.由于该函数完成其工作所需的唯一信息是:

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

你认为函数的参数是什么?

另一个需要反复做的常见事情是找到"一些东西,无论是空格还是标点符号.你可能需要自己写这个,或者如果你可以使用库函数,或者想要一个提示,查找:

man strcspn

I'm currently going through K.N. King's C Programming: A Modern Approach. I've made it past the text for the 8th chapter (Arrays), and I'm eager to move on to chapter 9, but I've yet to solve the so-called "programming projects" at the end of each chapter. Unfortunately, the 14th... bugs me.

Write a program that reverses the words in a sentence.

Enter a sentence: you can cage a swallow can't you?
Reversal of sentence: you can't swallow a cage can you?

Hint: Use a loop to read the characters one by one and store them in a one-dimensional char array. Have the loop stop at a period, question mark, or exclamation point (the "terminating character "), which is saved in a separate char variable. Then use a second loop to search backward through the array for the beginning of the last word. Print the last word, then search backward for the next-to-last word. Repeat until the beginning of the array is reached. Finally, print the terminating character.

I've been thinking of defining a word as a sequence of characters between blank spaces. So when a space is reached, go backward, printing each character, until another space is found. My first version of the program only printed the first word. The current version of it only prints the other words. I've been stuck on this for two days, so any help is truly appreciated. Here is my code, as well as an output sample. Hopefully I've properly documented my code. Thanks in advance!

Code

/* 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 */
}

Output:

http://drp.ly/1nYt5J

解决方案

Another methodology to think about:

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?
                             ^^^

For each thing you want to reverse (be it a whole sentence or a word):

  1. Find the beginning and end
  2. Swap the beginning and end characters
  3. Move "inwards" once
  4. keep going until you reach "the middle"

Since reversing a chunk of a string is a common operation, it makes sense to make it its own function. And since the only information the function need to do its job is:

  1. the string
  2. the beginning index
  3. the ending index

What do you think the parameters for the function would be?

The other common thing that needs to be done over and over is "finding" something, be it a space or a punctuation mark. You may need to write this yourself, or if you can use library functions, or want a hint, look up:

man strcspn

这篇关于倒置句子中的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆