0

我试图用从文件中读取的所有字母替换字符串中的特定字符。

例如,我有一个 replace.txt,其中包含所有替换字符'#''&'. 如果我的字符串是“数组”,则必须替换字符“a”。该程序应生成组合,如

#rray
#rr&y
#rr#y
&rr&y
&rr#y
&rray
arr#y 
arr&y

我试过一次,但我得到了不想要的输出。

这是我的代码,

int Guess(char str[],char start[],int startPos);
char* replaceChar(char* str, char ch1, char ch2);

void clear(char str[])
{
    strcpy(str, "");
}

int size(char str[])
{
    return strlen(str);
}

void append(char str[], char c)
{
    int len = strlen(str);
    str[len] = c;
    str[len+1] = '\0';
}
char* replace(char* str, char *orig, char rep,int k)
{
    static char buffer[4096];
    char *p;
    int i,count;

    if(!(p = strstr(str, orig)))
        return str;
    strncpy(buffer, str, p-str+(k-2));
    buffer[(p-str)+(k-2)+1] = '\0';
    sprintf(buffer+(p-str)+(k-2), "%c%s", rep, p+strlen(orig)+(k-2));
    return buffer;
}
long int findfirst(char s[], char c, int k)
{
    int i,flag=0;
    char* pch;
    pch=strchr(s+k-1,c);
    return (pch-s+1);
}


char start[1000];
int main()
{
    FILE *fp;
    fp=fopen("replace.txt","r");
    char line[1000];
    int i=0;
    while (fgets(line, sizeof(line), fp))
    {
        start[i] = line[0];
        i++;
    }
    printf("%s",start);
    printf("\n");
    fclose(fp);
    char guess[100];
    /*---------------------Read Input by Character-----------------------*/
    int c;
    while ((c = getchar())!=EOF)
    {
        if (c=='\n')
        {
        Guess(guess,start,0);
        clear(guess);
        }
        else
        {
        char c1=(char)c;
        append(guess,c1);
        }
    }
    if (size(guess)!=0)
    {
        Guess(guess,start,0);
    }
    return 0;
}

int Guess(char* str,char start[],int startPos)
{
    char* baseString = str;
    int match=1;
    int i,j;
    char* res;
    for (i=startPos; i<strlen(str); i++) //go through the rest of the string looking for replacements
    {
        if ((findfirst(baseString,'a',i)-1)==i) //replacement
        {
            for (j=0; j<2; j++) //go through all of the replacements
            {
            res = replace(baseString,"a",start[j],i);
            printf(" res : %s \n",res);//c_str(baseString));
            Guess(res,start,i+1);
            baseString=str;
            }
        }
    }
    return 0;
}

当我输入为“samba”时,我得到的输出是,

"#

samba

res : s"mba

res : s"mba (here i need o/p as s"mb")

res : s"mba (here i need o/p as s"mb#)

res : s#mba

res : s#mba (here i need o/p as s#mb")

res : s#mba (here i need o/p as s#mb#)

res : samb"

res : samb#

另外,如果我将输入输入为“arganda”,它会显示,

"#

arganda

res : "arganda

res : "ar"

res : "ar#

res : #arganda

res : #ar"

res : #ar#

res : ar"anda

res : ar"an"

res : ar"an#

res : ar#anda

res : ar#an"

res : ar#an#

res : argan"a

res : argan#a

我对这种模式生成完全感到困惑。

任何人都可以提供解决方案吗?

还有关于如何使用 char* 而不是 char[]?

4

1 回答 1

0

检查我的代码。它按预期工作。看到我没有使用文本文件来读取符号,我正在全局初始化它。如果需要,您可以按照自己的方式更改它。

int Guess(char str[],char start[],int startPos);
char* replaceChar(char* str, char ch1, char ch2);

void clear(char str[])
{
    strcpy(str, "");
}

int size(char str[])
{
    return strlen(str);
}
long int findfirst(char s[], char c)
{
    int i,flag=0;
    char* pch;
    pch=strchr(s,c);
    return (pch-s);
}


char start[1000] = "\"#";
int main()
{
    //FILE *fp;
    //fp=fopen("replace.txt","r");
    char line[1000];
    int i=0;
    //while (fgets(line, sizeof(line), fp))
   // {
    //    start[i] = line[0];
     //    i++;
   // }
    printf("%s",start);
    printf("\n");
    //fclose(fp);
    char guess[100]={0};
    /*---------------------Read Input by Character-----------------------*/
    int c;
    while ((fgets(guess, sizeof(guess), stdin))!=NULL)
    {
        Guess(guess,start,0);
        clear(guess);
    }
    if (size(guess)!=0)
    {
        Guess(guess,start,0);
    }
    return 0;
}

int Guess(char* str,char start[],int startPos)
{
    char* baseString = str;
    int match=1;
    int i,j;
    for (i=startPos,j=0; i<strlen(str) && j < strlen(start); i++) //go through the rest of the string looking for replacements
    {
        if ((findfirst(baseString ,'a'))==i) //replacement
        {
            baseString[i] = start[j++];
            printf(" res : %s \n",baseString);
        }
    }
    return 0;
}
于 2013-03-27T09:51:12.387 回答