c program to generate a string with replaced values in a file.
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". Can anyone provide C code to generate these combinaions ? I have tried once...but i'm getting undesired output. here is my 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[]) { //printf("%d\n",strlen(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; //printf("size : %ld\n",p-str+(k-2)); strncpy(buffer, str, p-str+(k-2)); buffer[(p-str)+(k-2)+1] = '\0'; //printf("Buffer[%ld] : %c\n",p-str+(k-2),buffer[p-str+(k-2)]); sprintf(buffer+(p-str)+(k-2), "%c%s", rep, p+strlen(orig)+(k-2)); //printf("res main : %s \n",buffer); /*for(i=(p-str)+(k-2)+1;i<strlen(buffer);i++) { printf("i = %d\n",i); if(buffer[i]=='a') { replace(buffer,"a",rep,i-3); printf("res : %s \n",buffer); } }*/ 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() { //freopen("replace.txt","r",stdin); 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) { //printf("%s",str); //printf("%s",start); //printf("\n"); 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 { //printf("%c %d %ld\n",str[i],i,findfirst(str,'a',i)); 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; 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# can anyone provide a solution to this? Also how to use char* instead of char[]? |
i think it is your homework, so do you have any code written?
|
Quote:
Have you tried to write a code for this? Where you are stopped? |
Quote:
|
It sounds like recursion might help you best. When your pointer reaches the first replaceable character in the string, substitute in turn each character from replace.txt (including the character to be replaced) then do a recursive call for the remainder of the string. You would still have to eliminate the string that ended up with no characters replaced.
|
All times are GMT -5. The time now is 10:03 AM. |