单词切碎/混合程序 [英] Word mincing/mixing program

查看:22
本文介绍了单词切碎/混合程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的输出当前的样子(这不是我想要的样子)以及正确的输出应该是什么样子 -

This is what my output currently looks like (which is not how I want it) and what the correct output should look like -

我希望我目前正在制作的程序从 2 个 .txt 文件中获取输入,然后将它们放入 1 个输出 .txt 文件中,但输出必须看起来像这样

I want my program that I am currently making take the inputs from 2 .txt files and then put them into 1 output .txt file but the output has to look something like this

例如,如果两个输入文件的内容是:现在是时候了."为了敏捷的棕色狐狸."然后我的输出行将是:现在是棕色狐狸的快速时间."

For example, if the two input files contents were: "Now is the time." "For the quick brown fox." Then my output line would be: "Now foR Is thE quick Time browN fox."

程序还应该将第一个文件中每个单词的首字母大写,并将文件中每个单词的最后一个字母大写第二个文件,直到我用完一行,然后将所有剩余的单词从较长的行转移到我的输出文件不变.在每组行的末尾以句点结尾,并向输出文件如上所示

The program should also capitalize the first letter of each word from the first file and the last letter of each word from the second file until I exhaust one line then transfers all remaining words from the longer line to my output file unchanged. At the end of each set of lines end with a period and write a line to the output file like seen above

我目前在c中的代码如下:

My current code in c is as follows:

/* 
*   Copyright (C) 2018 Canton Robinson
*
*   This program is free software: you can redistribute it and/or modify
*   it under the terms of the GNU General Public License as published by
*   the Free Software Foundation, either version 3 of the License, or
*   (at your option) any later version.
*
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details.
*
*   You should have received a copy of the GNU General Public License
*   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h> 
#include <stdlib.h> 
int main( int argc, char *argv[] ) 
{ 
 FILE *fp1 , *fp2, *fp3;
 int ch;

  if (argc !=4) {
        fprintf(stderr, "Usage: mincer File1 File2 destFile\n");
        exit(EXIT_FAILURE);
    }

 if ((fp1 = fopen(argv[1], "rb")) ==NULL) {
            fprintf(stderr, "Can't open %s\n", argv[1]);
            exit(EXIT_FAILURE);
        }

         if ((fp2 = fopen(argv[2], "rb")) ==NULL) {
            fprintf(stderr, "Can't open %s\n", argv[2]);
            exit(EXIT_FAILURE);
        }

  if ((fp3 = fopen(argv[3], "wb")) ==NULL) {
            fprintf(stderr, "Can't open %s\n", argv[3]);
            fclose(fp1);
            fclose(fp2);
            exit(EXIT_FAILURE);
        }

   // Copy contents of first file to file3.txt 
   while ((ch = fgetc(fp1)) != EOF) 
      fputc(ch, fp3); 

   // Copy contents of second file to file3.txt 
   while ((ch = fgetc(fp2)) != EOF) 
      fputc(ch, fp3); 

   printf("Merged %s and %s into %s", argv[1], argv[2], argv[3]); 

   fclose(fp1); 
   fclose(fp2); 
   fclose(fp3); 
   return 0; 
} 

推荐答案

这个循环可以做你想做的.抱歉,无法测试代码.

This loop may do what you want. Sorry, can't test the code.

while(isspace(ch1=fgetc(fp1)));
while(isspace(ch2=fgetc(fp2)));

while(ch1!=EOF || ch2!=EOF) {
    if(ch1!=EOF) {
        fputc(toupper(ch1), fp3);
        while((ch1=fgetc(fp1))!=EOF && !isspace(ch1))
            fputc(ch1, fp3);
        fputc(' ', fp3);
        while(isspace(ch1))
            ch1=fgetc(fp1);
    }
    if(ch2!=EOF) {
        for(lch2=ch2; (ch2=fgetc(fp2))!=EOF && !isspace(ch2); lch2=ch2)
            fputc(lch2, fp3);
        fputc(toupper(lch2), fp3);
        fputc(' ', fp3);
        while(isspace(ch2))
            ch2=fgetc(fp2);
    }
}

这篇关于单词切碎/混合程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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