2016-12-10 144 views
0

你能指導我嗎?我有一個文件中的字符串。當我在控制檯上看到字符串時,我需要編寫要更改的單詞,並將結果輸出到另一個文件中。例如:「你好,我的女孩」這個詞我想改變「女孩」的另一個詞「男孩」。我可以使用圖書館 你能告訴我可以幫助我改變這個詞的算法嗎?替換C中的單詞

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int main() 
{ 



    char my_string[256]; 
    char* ptr; 

    FILE *f; 
    if ((f = fopen("test.txt", "r"))==NULL) { 
    printf("Cannot open test file.\n"); 
    exit(1);} 

    FILE *out; 
    if((out=fopen("result.txt","w"))==NULL){ 
     printf("ERROR\n"); 
     exit(1); 
    } 

    fgets (my_string,256,f); 


    printf ("result: %s\n",my_string); 
    ptr = strtok (my_string," "); 


    while (ptr != NULL) 
    { 
     printf ("%s \n",ptr); 
     ptr = strtok (NULL," "); 
    } 

    char old_word [10]; 
    char new_word [10]; 
    char* ptr_old; 
    char* ptr_new; 

    printf ("Enter your old word:\n"); 
    ptr_old= gets (old_word); 
    printf ("Your old word:%s\n",old_word); 

    printf ("Enter new old word:\n"); 
    ptr_new = gets (new_word); 
    printf ("Your new word:%s\n",new_word); 



    fclose(f); 
    fclose(out); 

    return 0; 
} 

我試圖將輸入字符串拆分爲單詞。現在是死路一條。

+0

不錯,但你必須首先要求單詞替換,然後比較'ptr'和你正在尋找的單詞,最後你必須'fprintf',而不是'printf'。 –

+0

在標記字符串時,將每個生成的單詞與「女孩」進行比較。如果匹配,則fprintf「boy」爲result.txt,否則爲fprintf這個詞。確保根據需要添加空格。 – jarmod

+0

「女孩」和「男孩」就是一個例子。我的字符串可以包含不同的wrods .. 看。我需要詢問舊詞和新詞,所以我認爲,我應該爲其中一個單詞創建2個指針。然後改變他們,並寫在我的主要「ptr」 –

回答

0

此代碼將幫助您。您必須在運行時傳遞4個參數。

./a.out 「oldword」 「 」從老拿word文件名「, 」文件名newword「 從哪裏抄」

$ ./a.out女孩男孩的test.txt的Result.txt

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int main(int args, char *argv[4]) 
{ 
    FILE *f1; 
    FILE *f2; 
    char *strings=0; 
    char *newstrings=0; 
    char *token=NULL; 
    strings=(char *)malloc(1000); 
    newstrings=(char *)malloc(1000); 

    if((strings==NULL)||(newstrings==NULL)) 
    { 
     printf("Memory allocation was not successfull."); 
     return 0; 
    } 

    if(args<4) 
    { 
     puts("Error: Not enough input parameters"); 
     puts("Usage: ./change <oldword> <newword> <infile> <newfile>"); 
     return 0; 

    } 

    f1=fopen(argv[3],"r"); 
    f2=fopen(argv[4],"w"); 
    if(f1==NULL) 
    { 
     puts("No such file exists"); 
     return 0; 
    } 
    while(fgets(strings,1000,f1)!=NULL) 
    { 
     if(strstr(strings,argv[1])!=NULL) 
     { 
      token=strtok(strings,"\n\t "); 
      while(token!=NULL) 
       { 
        if(strcmp(token,argv[1])==0) 
        { 
         strcat(newstrings,argv[2]); 
         strcat(newstrings," "); 
        } 
        else 
        { 
         strcat(newstrings,token); 
         strcat(newstrings," "); 
        } 
        token=strtok(NULL,"\n\t "); 
       } 
     } 
     else 
     { 
      strcpy(newstrings,strings); 
     } 
     fputs(newstrings,f2); 
    } 
    free(strings); 
    free(newstrings); 
    printf("New file <%s> generated!\n",argv[4]); 

    fclose(f1); 
    fclose(f2); 
    return 0; 
} 
0

可以在示範程序中使用的功能,如顯示功能下面

#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 

char * replace(const char *s, const char *src, const char *dsn) 
{ 
    size_t n = 0; 
    size_t src_len = strlen(src); 
    size_t dsn_len = strlen(dsn); 

    for (const char *p = s; (p = strstr(p, src)) != NULL; p += src_len) 
    { 
     n++; 
    } 

    char *result = malloc(strlen(s) + n * (src_len - dsn_len) + 1); 

    const char *p = s; 
    char *t = result; 

    if (n != 0) 
    { 
     for (const char *q; (q = strstr(p, src)) != NULL; p = q + src_len) 
     { 
      memcpy(t, p, q - p); 
      t += q - p; 
      memcpy(t, dsn, dsn_len); 
      t += dsn_len; 
     } 
    } 

    strcpy(t, p); 

    return result; 
} 

int main(void) 
{ 
    char s[] = " the girl and boy are relatives"; 

    char *p = replace(s, "girl", "boy"); 

    puts(s); 
    puts(p); 

    free(p); 
} 

程序輸出是

the girl and boy are relatives 
the boy and boy are relatives 
0
#include <stdio.h> 
#include <string.h> 

int main() 
{ 
    char file_path[40] = { 0 }, stf[255] = { 0 }, rtf[255] = { 0 }, str[255] = { 0 }; 
    FILE* file = NULL; 
    FILE *e_f; 

    if((e_f=fopen("result.txt","w"))==NULL){ 
     printf("ERROR\n"); 
     exit(1); 
    } 

    do 
    { 
    printf("Enter file path: "); 
    fgets(file_path, 40, stdin); 
    file_path[strlen(file_path) - 1] = '\0'; 
    file = fopen(file_path, "r+"); 
    } 

    while(file == NULL); 

    printf("Enter text to find: "); 
    fgets(stf, 255, stdin); 
    stf[strlen(stf) - 1] = '\0'; 
    printf("Enter text to replace: "); 
    fgets(rtf, 255, stdin); 
    rtf[strlen(rtf) - 1] = '\0'; 

    while(fgets(str, 255, file) != NULL) 
    { 
    char* tmp_ptr = strstr(str, stf); 
    while(tmp_ptr != NULL) 
    { 
     char tmp_str[255]; 
     strcpy(tmp_str, tmp_ptr + strlen(stf)); 
     strcpy(str + strlen(str) - strlen(tmp_ptr), rtf); 
     strcat(str, tmp_str); 
     tmp_ptr = strstr(str, stf); 
    } 
    printf("%s", str); 
    } 
    fclose(file); 
    fclose(e_f); 

    return 0; 
} 

這是我需要的。感謝大家的幫助!