Автор Тема: Программа для замены символов в строке  (Прочитано 4992 раз)

0 Пользователей и 1 Гость просматривают эту тему.

ginger22

  • Гость
Пытаюсь написать программу для замены символов в строке, столкнулся с непонятными ошибками, ввиде появляющегося мусора в строках.
В частности, проблемы при замене 'r' на 'р', и последней пары в структуре 'Th' -> 'в'.
Вот типичный пример моего "дебага":
RES2: Thиs иs a '#URL#' strиng "#ID#" HA-HA-HA, #ID# for testиng #IDCODE#---
FROM: 'r' TO: 'р'
PCH: rиng "#ID#" HA-HA-HA, #ID# for testиng #IDCODE#---
DEBUG0: Len TMP: 25 Len RES 70, LEN: 70
NEXT ->
TMP2: Thиs иs a '#URL#' stЪЪЪЪриng "#ID#" HA-HA-HA, #ID# for testиng #IDCODE#---
RES2: Thиs иs a '#URL#' stЪЪЪЪриng "#ID#" HA-HA-HA, #ID# for testиng #IDCODE#---
PCH: r testиng #IDCODE#---
DEBUG0: Len TMP: 57 Len RES 74, LEN: 74
NEXT ->
TMP2: Thиs иs a '#URL#' stЪЪЪЪриng "#ID#" HA-HA-HA, #ID# fostир testиng #IDCODE#---
RES2: Thиs иs a '#URL#' stЪЪЪЪриng "#ID#" HA-HA-HA, #ID# fostир testиng #IDCODE#---
FROM: 't' TO: 'т'
PCH: tЪЪЪЪриng "#ID#" HA-HA-HA, #ID# fostир testиng #IDCODE#---
DEBUG0: Len TMP: 26 Len RES 77, LEN: 77
NEXT ->
TMP2: Thиs иs a '#URL#' sЪЪЪЪЪртЪЪЪЪриng "#ID#" HA-HA-HA, #ID# fostир testиng #IDCODE#---

Вот эти твердые знаки - это мусор. Ни как не могу от него избавится :-(
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <memory.h>

typedef struct
{
    char from[32];
    char to[64];
} tagStruct;

char * tagReplacer(char *src, tagStruct * tagS, int count)
{
    bool replaced=true;
    char *tmp=NULL;
    char *res=NULL;
    char *pch=NULL;
    tmp=(char*) malloc(sizeof(char)+1);
    res=(char*) malloc(sizeof(char)+1);
    res=(char*)realloc(res,((strlen(src))*sizeof(char*)));
    //strncpy (res,src,strlen(src));
    //printf("STAGE1: Len SRC: %i TEXT: %s\n", strlen(src), src);
    strcpy (res,src);

    for(int i=0;i<count;i++) {
        printf("FROM: '%s' TO: '%s'\n",tagS[i].from,tagS[i].to);

        while (replaced) {
            //printf("RES1: %s\n", res);
            int lenres=strlen(res);
            if(pch = strstr (res,tagS[i].from)) {
                printf("PCH: %s\n", pch);
                replaced=true;
                tmp=(char*)realloc(tmp, (lenres-strlen(pch)+1)*sizeof(char));
                strncpy(tmp,res,lenres-strlen(pch));
                tmp=(char*)realloc(tmp, (strlen(tmp)+strlen(tagS[i].to)+1)*sizeof(char));
                strncat(tmp,tagS[i].to,strlen(tagS[i].to));
                printf("DEBUG0: Len TMP: %i Len RES %i, LEN: %i\n", strlen(tmp), strlen(res),strlen(res)+strlen(tagS[i].to)-strlen(tagS[i].from));
                if((strlen(tmp))<(lenres+strlen(tagS[i].to)-strlen(tagS[i].from))){
                    //printf("DEBUG1: Len TMP: %i Len RES: %i(%i), TEXT: <%s>\n", strlen(tmp), strlen(res), lenres, res);
                    tmp=(char*)realloc(tmp, (lenres+strlen(tagS[i].to)-strlen(tagS[i].from)-1)*sizeof(char));
                    //printf("DEBUG2: Len TMP: %i Len RES: %i(%i), TEXT: <%s>\n", strlen(tmp), strlen(res), lenres, res);
                    //printf("DEBUG3: Len TMP: %i Len RES %i, LEN: %i\n", strlen(tmp), strlen(res),strlen(res)+strlen(tagS[i].to)-strlen(tagS[i].from));
                    for(int z=(strlen(res)-strlen(pch)+strlen(tagS[i].to));z<((strlen(res)));z++){
                        tmp[strlen(tmp)]=res[z];
                        //printf("TMP1: %s\n", tmp);
                        //printf("STAGEX: Len TMP: %i, SYMB: %c TEXT: <%s>\n", strlen(tmp), src[z], tmp);
                    }
                }
            }
            else {
                replaced=false;
            }
            if(replaced) {
                printf("NEXT ->\n");
                printf("TMP2: %s\n",tmp);
                res=NULL;
                res=(char*)realloc(res,(strlen(tmp)+1));
                //memset(res,0,sizeof(res));
                strncpy (res,tmp,strlen(tmp));
                tmp=NULL;
                pch=NULL;
                //printf("TMP3: %s PCH: %s\n",tmp, pch);
                printf("RES2: %s\n", res);
            }
        }
        replaced=true;
    }
    return res;
}

int main ()
{
    tagStruct *tagStr=NULL;
    tagStr=(tagStruct*) malloc(sizeof(tagStruct));
    strcpy(tagStr[0].from,"i");
    strcpy(tagStr[0].to,"и");
    tagStr=(tagStruct*)realloc(tagStr, (sizeof(tagStruct))*2);
    strcpy(tagStr[1].from,"r");
    strcpy(tagStr[1].to,"р");
    tagStr=(tagStruct*)realloc(tagStr, (sizeof(tagStruct))*3);
    strcpy(tagStr[2].from,"t");
    strcpy(tagStr[2].to,"т");
    tagStr=(tagStruct*)realloc(tagStr, (sizeof(tagStruct))*4);
    strcpy(tagStr[3].from,"Th");
    strcpy(tagStr[3].to,"В");

    //printf("FROM: '%s' TO: '%s'\n",tagStr[0].from,tagStr[0].to);

    const char *foo=tagReplacer("This is a '#URL#' string \"#ID#\" HA-HA-HA, #ID# for testing #IDCODE#---",tagStr,4);
    //char *foo=tagReplacer("#ID#",tagStr,2);
    printf("Len DST: %i\n",strlen(foo));
    printf("%s\n",foo);
}