单词切碎/混合程序

时间:2018-11-15 07:25:32

标签: c merge

这是我当前的输出结果(不是我想要的样子),正确的输出应该是-

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文件中,但输出必须看起来像这样

例如,如果两个输入文件的内容为: “现在是时候了。” “为了那只棕色的狐狸。” 然后我的输出行将是: “现在,这是快速的眉头狐狸。”

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

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

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

1 个答案:

答案 0 :(得分:0)

此循环可能会执行您想要的操作。抱歉,无法测试代码。

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);
    }
}