LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   c program to generate a string with replaced values in a file. (https://www.linuxquestions.org/questions/programming-9/c-program-to-generate-a-string-with-replaced-values-in-a-file-4175455480/)

arungpillai09054 03-25-2013 05:28 AM

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[]?

pan64 03-25-2013 05:30 AM

i think it is your homework, so do you have any code written?

kooru 03-25-2013 10:38 AM

Quote:

Originally Posted by arungpillai09054 (Post 4918233)
Can anyone provide C code to generate these combinaions ?

Hi arungpillai09054 :)
Have you tried to write a code for this? Where you are stopped?

TB0ne 03-25-2013 10:54 AM

Quote:

Originally Posted by arungpillai09054 (Post 4918233)
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 ?

No..we're not going to do your homework for you, but we will be more than happy to HELP you if you're stuck. So, post what you've written/tried, what results you're getting, etc., and we can assist. Otherwise, do your own homework.

psionl0 03-25-2013 11:33 AM

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.