Thread: compares 2 arguments and replaces characters

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    4

    compares 2 arguments and replaces characters

    it doesn't work -help??????????????

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>  
    #include <stdlib.h>
    
    
    main (int argc, char *argv[]) 
    {
    
    nt k, j ,i, len , siz, rep = 0; 
    
    char *s1; 
    char *s2;
    char s3[30];
    
    
    s1 = malloc (strlen(argv[1]) +1);
    strcpy (s1,argv[1]);
    
    s1 = malloc (strlen(argv[2]) +1);
    strcpy (s2,argv[2]); 
      
    len = strlen(s1); 
    siz = strlen(s2);
    
    while (rep <= 5)
    
    {
       while (NULL != fgets(s3, 30, stdin))   
    
        {
    
    if (strcmp(s1[0], '-')) && (strcmp(s1[1], 'f'))
    	
    	{
    	  
    	if (argc != 4)
    	 {
    		printf("invlaid number of arguments");
    		exit(1); 
    	 } 	
    		
    		for (k=0; k<= len; k++)
    
    			{
    	
    						
    			for (i=1; i<=len; i++)
     				
    				{
    		   		if ( 0==strcmp(s1[k], s1[i]) )
    					{
    									
    						strcpy(s1[k], s2[j]);
    					} 
    				j++; 
    				
    				}
    
    			fputs(s1,stdout);		
    				 
    		        }						            	    
     	}
       	
    }
    
    
    if ( 0==strcmp (s1[0], '-')) && ( 0==strcmp (s1[1],'f')) 
    {
    
    if (argc != 3)
    
    	{
    		printf("invalid number of arguments");
    		exit(1); 
    	} 	
    	
    
    		for (k=0; k<= len; k++)
    
    			{
    			   strcpy(s1[k], s2[j]);		
    			
                               j++; 
                            }
     
    		fputs( s1, stdout);
    }
    
    }
    	
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Code:
    s1 = malloc (strlen(argv[2]) +1);
    strcpy (s2,argv[2]);
    You got a small typo for s1/s2, meant to be:
    Code:
    s2 = malloc (strlen(argv[2]) +1);
    strcpy (s2,argv[2]);
    Also more: make sure you specify the return type for main, explictly use int main(int argc, char* argv[])

    Also did you bother to compile it? You got more syntax errors:

    Code:
    if ( 0==strcmp (s1[0], '-')) && ( 0==strcmp (s1[1],'f'))
    You are missing an extra set of brackets:
    Code:
    if (( 0==strcmp (s1[0], '-')) && ( 0==strcmp (s1[1],'f')))
    Last edited by 0rion; 04-13-2005 at 11:01 PM.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    thanks for that

    it won't compile thats part of the problem

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Well the compiler should generate errors when it encounters syntax errors and usually it tells you which lines its on so its usually easy to fix without further help. You should get used to reading the errors since you'll be stumped on what each error means if you got no one around ya

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    we are compiling in SSH
    none of the comments make sense
    it doesn't like my assignments strcpy etc it treats them as an error

    any other tips

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    If you want to compare individual characters you don't need to use strcmp().
    Likewise for strcpy(), what you are doing is this:
    Code:
       strcpy(s1[k], s2[j]);
    This would not work unless s1 is an array of strings. If you wanted to copy over the character in s2[j] into s1[k] then you just simply need to use the assignment operator:
    Code:
    s1[k] = s2[j];

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    s1 is a pointer to an array of strings

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    orion is right

    s1 is a pointer which acts like an array

    s1[1] it is equal to s1 +1

    u are incrmenting the s1 pointer by offset 1 to point to next element of the string

    s.s.harish

  9. #9
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Quote Originally Posted by ssharish
    s1[1] it is equal to s1 +1
    Not quite.

    s1[1] evaluates to *(s1+1)

    (s1+1) is equivalent to &s1[1]

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    yes u are right joshdick

    i am an idiot

    thax for correcting me

    s.s.harish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple command line arguments with switches
    By Ene Dene in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2009, 03:28 AM