LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-25-2013, 05:28 AM   #1
arungpillai09054
LQ Newbie
 
Registered: Nov 2012
Posts: 7

Rep: Reputation: Disabled
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[]?

Last edited by arungpillai09054; 03-26-2013 at 05:39 AM.
 
Old 03-25-2013, 05:30 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
i think it is your homework, so do you have any code written?
 
Old 03-25-2013, 10:38 AM   #3
kooru
Senior Member
 
Registered: Sep 2012
Posts: 1,385

Rep: Reputation: 275Reputation: 275Reputation: 275
Quote:
Originally Posted by arungpillai09054 View Post
Can anyone provide C code to generate these combinaions ?
Hi arungpillai09054
Have you tried to write a code for this? Where you are stopped?
 
Old 03-25-2013, 10:54 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by arungpillai09054 View Post
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.
 
Old 03-25-2013, 11:33 AM   #5
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
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.
 
  


Reply

Tags
cprogramming, pattern, programing



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
generate a password string to be used by the useradd command powah Linux - Security 9 06-27-2023 06:23 AM
Why program received SIGQUIT quited but did NOT generate the core file ???? cryincold Programming 2 02-27-2008 04:26 AM
generate new string from a text file walterwaston Programming 2 10-08-2007 02:14 AM
Really strange change of string values in C realos Programming 34 08-08-2005 09:32 PM
i make a program using forte how can i generate from that program an rpm abdelhlims Programming 0 07-13-2004 12:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration