首页 文章

程序要求用户输入文本文件,然后用相同长度的单词替换单词

提问于
浏览
-3

我知道如何创建一个文本文件并打开它但我不知道如何搜索一个单词并用相同长度的单词替换它是我的代码你可以帮助我,我是一种初学者这里是我的代码你可以帮我吗调整它

#include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
        #include <ctype.h>
        #define S_(n) #n
        #define S(n) S_(n)
        #define MAX_WORD_LENGTH 64
        int main()
        {
          char text;
          FILE *newfile;
          newfile = fopen("c:\\program.txt","w");

          if(newfile == NULL)
            {
           printf("Error! Can open the file ");
             return 0;
              }
            printf("Enter your text: \n");
           while ((text=getchar())!=EOF){ fputc(text,newfile);}
             `enter code here` fclose(newfile);
  newfile=fopen("c:\\program.txt","r");
  newfile=fopen("c:\\program.txt","r");
  while ((text = fgetc(newfile))!=EOF)
  printf("%c",text);
  fclose(newfile);

  printf("\n %c",text);

char searchWord[MAX_WORD_LENGTH+1], replaceWord[MAX_WORD_LENGTH+1];
    //"word" means space delimiter.
    printf("Enter search word\n");
    scanf("%" S(MAX_WORD_LENGTH) "s", searchWord);
    printf("Enter replace word\n");
    scanf("%" S(MAX_WORD_LENGTH) "s", replaceWord);

    long pos = ftell(newfile);//file position at first
    char sentence[1024];
    size_t replace(char *sentence, const char *sword, const char *rword);
    size_t count = 0;

    while(fgets(sentence, sizeof sentence, newfile)){
        size_t r_count = replace(sentence, searchWord, replaceWord);
        if(r_count != 0){
            fseek(newfile, pos, SEEK_SET);//back the file position to the position before reading
            fputs(sentence, newfile);
            fflush(newfile);
            count += r_count;
        }
        pos = ftell(newfile);//save file position
    }
    fclose(newfile);
    if(count)
        printf("\nReplaced %zu times.\n", count);
    else
        printf("\nReplacement was not done.\n");
}

size_t replace(char *sentence, const char *sword, const char *rword){
    char *p =sentence;
    size_t sword_len = strlen(sword);
    size_t rword_len = strlen(rword);
    if(sword_len != rword_len){
        fprintf(stderr, "The length of the word is different.(in %s)\n", __func__);
        exit(EXIT_FAILURE);//just return 0; ?
    }

    size_t rep_count = 0;
    while(p = strstr(p, sword)){
        if((p == sentence || isspace((unsigned char)p[-1])) && //top of sentence or space precedes word
           (!p[sword_len] || isspace((unsigned char)p[sword_len]))){//end of sentence or space is after the word
           //Word is not part of another word
           memcpy(p, rword, rword_len);
           ++rep_count;
        }
        p += sword_len;
    }
    return rep_count;
}

1 回答

  • 0

    像这样

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    //Stringification
    #define S_(n) #n
    #define S(n) S_(n)
    
    #define MAX_WORD_LENGTH 64
    
    int main(void){
        char filename[FILENAME_MAX+1];
    
        printf("Enter text file name\n");
        if(fgets(filename, sizeof filename, stdin)==NULL){
            return EXIT_FAILURE;
        }
        filename[strcspn(filename, "\n")] = 0;//chomp newline
    
        FILE *fp = fopen(filename, "r+");//r+ : Reading and Writing, The file must exist.
        if(fp == NULL){
            perror("fopen:");
            return EXIT_FAILURE;
        }
    
        char searchWord[MAX_WORD_LENGTH+1], replaceWord[MAX_WORD_LENGTH+1];
        //"word" means space delimiter.
        printf("Enter search word\n");
        scanf("%" S(MAX_WORD_LENGTH) "s", searchWord);
        printf("Enter replace word\n");
        scanf("%" S(MAX_WORD_LENGTH) "s", replaceWord);
    
        long pos = ftell(fp);//file position at first
        char sentence[1024];
        size_t replace(char *sentence, const char *sword, const char *rword);
        size_t count = 0;
    
        while(fgets(sentence, sizeof sentence, fp)){
            size_t r_count = replace(sentence, searchWord, replaceWord);
            if(r_count != 0){
                fseek(fp, pos, SEEK_SET);//back the file position to the position before reading
                fputs(sentence, fp);
                fflush(fp);
                count += r_count;
            }
            pos = ftell(fp);//save file position
        }
        fclose(fp);
        if(count)
            printf("\nReplaced %zu times.\n", count);
        else
            printf("\nReplacement was not done.\n");
    }
    
    size_t replace(char *sentence, const char *sword, const char *rword){
        char *p =sentence;
        size_t sword_len = strlen(sword);
        size_t rword_len = strlen(rword);
        if(sword_len != rword_len){
            fprintf(stderr, "The length of the word is different.(in %s)\n", __func__);
            exit(EXIT_FAILURE);//just return 0; ?
        }
    
        size_t rep_count = 0;
        while(p = strstr(p, sword)){
            if((p == sentence || isspace((unsigned char)p[-1])) && //top of sentence or space precedes word
               (!p[sword_len] || isspace((unsigned char)p[sword_len]))){//end of sentence or space is after the word
               //Word is not part of another word
               memcpy(p, rword, rword_len);
               ++rep_count;
            }
            p += sword_len;
        }
        return rep_count;
    }
    

相关问题