2016-03-01 80 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

非常感谢 –