i need ur help to complete this assignment

Write a C program which, reads from standard input, replaces all the occurrences of the characters supplied by the 1st command line argument, with the corresponding characters supplied by the 2nd command line argument, and writes the output to a file
supplied by the 3rd command line argument, then print out the file.

any help will be much appreciated

i understand this simple program, but unsure of how to modify it to meet my objective.

include <stdio.h>

int main(int argc, char *argv[])
{
    int x;

    printf("%d\n",argc);
    for (x=0; x<argc; x++)
        printf("%s\n",argv[x]);
    return 0;
}

also when I compile the code to an executable file named aaa and type aaa xxx yyy zzz. The code will print the command line parameters xxx, yyy and zzz, one per line. are these the command line arguments that i need to replace?? so xxx would be the 1st command line argument and yyy would be the second command line argument.
is this correct or have i got it completely wrong

>The code will print the command line parameters xxx, yyy and zzz, one per line.
Actually, the first thing that program will print out is the name of the executable. The first argument that's passed to the program is actually the second element in 'argv'.

>are these the command line arguments that i need to replace??
Yes.

i had an idea of puting the command line arguments onto stack. so when i pop them they will be reversed.
would this work??
how do i write code for this can any1 help??

>i had an idea of puting the command line arguments onto stack.
What would be the purpose of that? If you want them reversed, make a reverse loop (counting down to 0).

hi again. i have this code so far. but it doesnt seem to be working as i'd hoped. can anyone give me a few tips. thanks

#include <stdio.h>
#include    <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include    <string.h>
void oops(char *s1, char *s2);
main(int argc, char *argv[] )
{   
 int write_fd, nwrite;    
char buffer[100];    
char *location;   
 int length=strlen(argv[1]);    
if(argc != 4)
{ //check that the arguments are given correctly.        
printf("Error: Too many arguments!\nFilename should have no spaces.\n\nUsage: %s string1 string2 filename\n",argv[0]);        
exit(1);    
}    
if(strlen(argv[1])!=strlen(argv[2]))
{ //check whether the initial and replacement strings are of same size.        
printf("Error: string 1 must be of equal length to string \n");        
exit(1);    
}    
printf("Input text and press enter:\n");    
gets(buffer); 
//put text from stdin into string buffer    
printf("Replacing all occurances of \"%s\" with \"%s\" in \"s\"\n",argv[1],argv[2],buffer);   
 while(location=strstr(buffer,argv[1])) 
//replace all string1's with string2's in buffer  
  {        strncpy(location,argv[2],length);   
 }  
  /*write to file*/    
printf("Writing to file... \n");   
 if ( (write_fd=creat( argv[3], O_WRONLY)) == -1 )    
 oops( "Cannot create file", argv[3]); 
if ( write( write_fd, buffer, strlen(buffer) ) != strlen(buffer) )    
 oops("Write error to ", argv[3]); /* close file */ 
if (close(write_fd) == -1 ) 
oops("Error closing files","");    
printf("Done.\nResult:");    printf("%s\n",buffer); //output result}
void oops(char *s1, char *s2){ fprintf(stderr,"Error: %s ", s1); perror(s2); exit(1);}

>>//check whether the initial and replacement strings are of same size.

That check does not meed the program's spefications -- there was nothing in the assignment you posted that said the replacement string must be the same length as the search string.

>>creat( argv[3], O_WRONLY

why are you using those deprecated functions? Use standard C FILE and associated functions.

>>gets(buffer);
never ever and at no time use gets() because it can cause your program to crash big-time. Use fgets() instead to limit the keyboard input to the size of the input buffer.

Please please format your code so that you and everyone else can read it. I was going to add the code tags for you but it was just too badly formatted for code tags to do any good.

aha but is this the right logic Ancient Dragon ?!
coz looking at his code he seems that he used a totally different logic !

is this the right test for the program ?!

c.exe abc def file.txt
argv[1]=abc
argv[2]=def
argv[3]=file.txt

input: abc
buffer = def
input :cajef
buffer= fdjef?!

because looking at the given "occurrences of the characters" could be the position of the characters or the same characters...
and that what make me lost.

It seems there may be some ambiguity in the requirements (and that occurs all too often) I assumed "occurrence of the characters" really meant the string supplied by the first argument. Example: replace all occurrences "Hello" with "Hello World". When the length of the string in the 2nd argunment is different than that in the 1st argument that means the size of the result string may be different than its original size by either shorting or lengthing the text read from the file.

It seems there may be some ambiguity in the requirements (and that occurs all too often) I assumed "occurrence of the characters" really meant the string supplied by the first argument. Example: replace all occurrences "Hello" with "Hello World". When the length of the string in the 2nd argunment is different than that in the 1st argument that means the size of the result string may be different than its original size by either shorting or lengthing the text read from the file.

aw zn this test is wrong ?!
c.exe abc def file.txt
argv[1]=abc
argv[2]=def
argv[3]=file.txt

input: abc
buffer = def
input :cajef
buffer= fdjef?!

it should bout :
c.exe abc def file.txt
argv[1]=abc
argv[2]=def
argv[3]=file.txt

input: abc
buffer = def
input :cajef
buffer= defef
or
input: abc
buffer = def

what text file are you using for your tests? Did your instructor give you one or did you make up your own ? If you can make up your own I would use a text file that you may already have lying around on your computer. That yway you can have something like this

c.exe Hello Worlds file.txt

instead of just nonsense characters.

what text file are you using for your tests? Did your instructor give you one or did you make up your own ? If you can make up your own I would use a text file that you may already have lying around on your computer. That yway you can have something like this

c.exe Hello Worlds file.txt

instead of just nonsense characters.

no champ, no there is no text file we just have to test...
./a.out hello world ....
thats all

This article has been dead for over six months. Start a new discussion instead.