C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了翻译句子中的单词大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我现在要经过K.N. King’s 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?

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

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

输出

http://drp.ly/1nYt5J

解决方法

另一种思方法
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?
                             ^^^

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

>找到开始和结束
>交换开头和结尾字符
>一次“向内”移动
>坚持到达“中间”

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

>字符串
>开始索引
>结束指数

您认为该功能的参数是什么?

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

@H_773_2@man strcspn

大佬总结

以上是大佬教程为你收集整理的翻译句子中的单词全部内容,希望文章能够帮你解决翻译句子中的单词所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。