2016-03-01 107 views
-1

現在我可以用nonchalant替換給定字符串中的indifferent這個詞,但是我需要使此函數具有動態性,因此indifferent可以用任何單詞替換。我知道我需要使用malloc來創建一個新的數組,這個數組將會保留我的原始字符串,但是對於如何使用malloc還沒有很好的理解,請解釋如何在這種情況下正確使用malloc。謝謝。使用malloc替換字符串中子字符串的C函數 - 無字符串函數

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

int findPosition(char string[], char sub[]) { 
    int i = 0; 
    int j = 0; 
    int f = 0; 

    for (i = 0; string[i] != '\0'; i++) { 
     if (sub[j] == string[i]) { 
      if (sub[j + 1] == '\0') { 
       f = 1; 
       break; 
      } 
      j++; 
     } else 
      j = 0; 
    } 
    if (f == 1) { 
     return i - j; 
    } 
    return -1; 
} 

int findLength(char sub[]) { 
    int i = 0; 

    for (i = 0; sub[i] != '\0'; i++) { 

    } 
    return i; 
}; 

void replaceWord(char string[], char sub[], char replace[]) { 
    int i = 0; 
    int j = 0; 
    int p = findPosition(string, sub); 
    int l = findLength(sub); 
    int k = p + l - 1; 

    for (i = p; i < k; i++) { 
     string[i] = replace[j]; 
     j++; 
    } 
    while(string[k] != '\0') { 
     string[k] = string[k + 1]; 
     k++; 
    } 
} 

int main(int argc, const char *argv[]) { 
    char stringArray[120] = "\"Mr.Fay, is this going to be a battle of wits?    \"" 
          "\t\"If it is,\" was the indifferent retort, \"" 
          "you have come unarmed!\""; 

    replaceWord(stringArray, "indifferent", "nonchalant"); 

    int i = 0; 
    while (stringArray[i] != '\0') { 
     printf("%c", stringArray[i]); 
     i++; 
    } 
    return 0; 
}; 
+3

的'的malloc()'函數無關字符串替換;它所做的只是分配內存。你將它們混合在一起意味着你沒有做足夠的工作來將問題分解成更小,可管理的(明確定義的)任務。從鍵盤退後一步,考慮一下你的問題(也許用一張白紙和一支鉛筆),分解它並組織自己 - 對於你來說,這遠比任何人的迴應要好得多。 – mah

回答

0

可以使用malloc爲新的字符串分配內存,你用另一個詞替換一個單詞的每一次出現:

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

/* use a local implementation of the string functions: */ 
size_t my_strlen(const char *s) { 
    size_t len; 
    for (len = 0; s[len] != '\0'; len++) 
     continue; 
    return len; 
} 
void *my_memcpy(void *dest, const void *src, size_t n) { 
    size_t i; 
    for (i = 0; i < n; i++) { 
     ((unsigned char*)dest)[i] = ((unsigned char*)src)[i]; 
    } 
    return dest; 
} 
char *my_strdup(const char *s) { 
    size_t n = my_strlen(s) + 1; 
    char *p = malloc(n); 
    if (p) my_memcpy(p, s, n); 
    return p; 
} 
char *my_strstr(const char *s1, const char *s2) { 
    for (;; s1++) { 
     for (size_t i = 0;; i++) { 
      if (s2[i] == '\0') return s1; 
      if (s1[i] != s2[i]) break; 
     } 
     if (*s1 == '\0') return NULL; 
    } 
} 

char *replaceWord(const char *str, const char *s1, const char *s2) { 
    char *res = my_strdup(str); /* return value is always allocated */ 
    char *p, *q; 
    size_t offset = 0; 
    size_t len = my_strlen(str); 
    size_t len1 = my_strlen(s1); 
    size_t len2 = my_strlen(s2); 

    if (len1 == 0) 
     return res; 

    while ((p = my_strstr(res + offset, s1)) != NULL) { 
     offset = p - res; 
     if (len1 == len2) { 
      /* no need to reallocate, replace in place */ 
      my_memcpy(res + offset, s2, len2); 
     } else {     
      /* allocate a new array with the adjusted length */ 
      q = malloc(len + len2 - len1 + 1); 
      /* copy the beginning of the string */ 
      my_memcpy(q, res, offset); 
      /* copy the replacement string */ 
      my_memcpy(q + offset, s2, len2); 
      /* copy the remainder of the string, and the final '\0' */ 
      my_memcpy(q + offset + len2, res + offset + len1, len - offset - len1 + 1); 
      /* free the previous string */ 
      free(res); 
      res = q; 
     } 
     /* search for matches from the end of the replacement */ 
     offset += len2; 
    } 
    return res; 
} 

int main(int argc, const char *argv[]) { 
    char stringArray[120] = "\"Mr.Fay, is this going to be a battle of wits?    \"" 
          "\t\"If it is,\" was the indifferent retort, \"" 
          "you have come unarmed!\""; 

    char *p = replaceWord(stringArray, "indifferent", "nonchalant"); 
    printf("%s", p); 
    free(p); 
    return 0; 
} 
+0

非常感謝您的回答,我之前在我的文章中沒有闡明,但我不允許使用string.h中的任何字符串操作函數。 –

+1

如果您不能使用''中的字符串函數,您可以重命名它們幷包括你自己的實現。 – chqrlie

+0

非常感謝 –