在一个句子里单词逆转 [英] Reversing words in a sentence

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

问题描述

我目前正在经历K.N.国王的 C程序设计:一种现代方法的。我做了它过去的文本第8章(阵列),我渴望继续前进到第9章,但我还没有解决所谓的编程项目在每章的结尾。不幸的是,14 ...的我的错误


  

编写逆转用到一个句子中词的程序。


 输入了一句:你可以笼一只燕子不是吗?
句子的逆转:你不能吞下一个笼子可以吗?


  

提示的:使用循环逐个读取字符,并将其存储在一维的的阵列。有在周期循环停止,问号或惊叹号(以下简称结束符),这是保存在单独的字符的变量。然后用第二个循环通过数组的最后一个单词的起始处向后搜索。打印的最后一个字,那么下一个到最后一个字向后搜索。重复,直到达到阵列的开头。最后,打印的终止符。


我一直在想定义一个字作为空格之间的字符序列。因此,达到空间的时候,就去落后,打印每一个字符,直到另一个空间被找到。我的第一个的版本的程序只印的第一个字。在电流的它的版本只打印等字样。我一直停留在这两天,所以任何帮助是真正的AP preciated。这里是我的code,以及输出样本。但愿我已经正确记录我的code。在此先感谢!

code

  / *包括标准I / O库* /
#包括LT&;&stdio.h中GT;/ *定义主* /
诠释主要(无效){    / **
     *声明字符存储句子的数组,以及
     *重新$ P $字符psenting下光标当前字符和
     *终止字符
     * /
    焦炭句子[100] = {''},C,TC;    / **
     *声明在0已经初始化一个循环计数器,增量
     *变量,以及所述读出句子的大小
     * /
    INT I = 0,J = 1,大小= 0;    / *获取句子* /
    的printf(请输入一个句子:\\ n);
    为(C =的getchar();!'。'(C =)及及(C =)及!!;&放大器;
        (C =!'?')及与放大器; (C ='\\ n'!); C =的getchar(),我++){        句子[I] = C; / *阵列中存储当前字符* /
        大小++; / *增加句子的大小* /
    }    TC = C; / *获取终止符* /    / **
     *去向后遍历数组,打印字符的每个序列
     *空间之间
     * /
    对于(I = 99; I> = 0;我 - ){        如果(句子[I] ==''){            而(全文[I + J]!=''){                的printf(%C,一句[I + J]);
                J ++;
            }            J = 1; / *复位增量变量* /
            的printf(); / *打印拖尾空间* /
        }
    }    / **
     *删除拖尾的空白,并打印结束符,
     *以及一个新的线
     * /
    的printf(\\ B%ç\\ n,TC);    返回0; / *返回0在成功执行程序* /
}

输出:

http://drp.ly/1nYt5J


解决方案

要考虑其他的方法:

 可以笼子里一只燕子不是吗?
uoy t'nac wollaws一个EGAC NAC uoy?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
你t'nac wollaws一个EGAC NAC uoy?
^^^
你不能wollaws一个EGAC NAC uoy?
    ^^^^^
你不能吞下一个EGAC NAC uoy?
          ^^^^^^^
你不能吞下一个EGAC NAC uoy?
                  ^
你不能吞下一个笼子NAC uoy?
                    ^^^^
你不能吞下一个笼子可以uoy?
                         ^^^
你不能吞下一个笼子可以吗?
                             ^^^

有关要反转(可以是整个句子或一个字)每一件事情:


  1. 找到的开头和结尾

  2. 交换的开始和结束字符

  3. 将向内一旦

  4. 继续下去,直到你达到中间的

由于扭转一个字符串的一大块是一种常见的操作,这是有道理的,以使自己的功能。而且,由于只有信息的功能需要做的工作是:


  1. 字符串

  2. 开始指数

  3. 结束索引

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

这需要被一遍又一遍地做其他的共同点是发现的东西,无论是空格或标点符号。你可能需要写自己,或者您可以使用库函数,或者想一个提示,查找:

 男人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天全站免登陆