#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    C program to generate a string with replaced values in a file.


    Respective members,

    I am trying to replace a particular character in a string with all the letters read from a file.

    for example, i have a replace.txt which contains all the replacing characters like '#' and '&'.
    If my string is "array",character 'a' has to be replaced. The program should generate combinations like "#rray","#rr&y","#rr#y","&rr&y","&rr#y","&rray","arr#y" and "arr&y".

    I have tried once...but i'm getting undesired output.

    here is my code,

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    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;
    }
    when i give input as "samba", i'm getting output as,

    "#
    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#

    also if i give input as "arganda", it shows,

    "#
    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

    I'm totally confused with this pattern generation.

    can anyone provide a solution to this?
    Also on how to use char* instead of char[]? :confused: :confused:
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    First, learn how to indent code

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    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;
    }

    Pay close attention to this loop.
    Code:
          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;
          }
    When you call Guess recursively (using res as a parameter), you end up calling replace() with a pointer to it's own internal buffer!

    This is almost certainly a bad thing to do.

    Also in main(), you need to make sure you do clear(guess) before trying to add characters to it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo