Struggling w/ string_replacement function

This is a discussion on Struggling w/ string_replacement function within the C Programming forums, part of the General Programming Boards category; Hello all, this is the last problem on a hmwk assignment of mine, and I am really getting frustrated over ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    8

    Struggling w/ string_replacement function

    Hello all, this is the last problem on a hmwk assignment of mine, and I am really getting frustrated over it. I am having probs writing my str_replace function...

    Basically, what this program needs to do is the following:

    Input sentence : This is very fun.
    S (search) = very
    R (replace) = not
    Output sentence: This is not fun.

    This is my thought process for this program:

    -have it look through the string you give to the function
    -if you find a character that matches the first character of the word to replace then keep checking to see if the whole word is there.
    -If it is, then don't add that new word to a temp string, but add the rest and insert the replacement string.

    Below is what I have up to now... my replace function may be really wrong, I honestly do not know for sure.... any help would be great. If anyone can walk me through the darn function that would help a lot too, I'm just confused.


    Code:
    #include<stdio.h>
    #define MAX_LEN 100
    
    int strlen(char *s)
    {
    	char *p=s;
    	
    	while(*p!='\0')
    		p++;
    		return p-s;
    }
    
    void strcopy(char *x, char *y)
    {
    	int i=0;
    	
    	while(*x!='\0') {
    		*y=*x;
    		x++;
    		y++;
    	}
    	*y='\0';
    } 
     
    int strcomp(char *x, char *y)
    {
    	while((*x==*y)&&(*x!='\0')&&(*y!='\0')) {
    		x++;
    		y++;
    	}
     
    	if(*x<*y)
    		return 1;
    	else if(*x==*y)
    		return 0;
    	else
    		return -1;
    }
      
    string_replace(char *orig_text, char *search, char *replace)
    {
    	int *p, *temp, len;
    	char array;
    	p=orig_text;
    	*temp=array[0];
    	
    	len=strlen(search);
    	
    	while(p!='\0') {
    		if(*p==*search) {
    			temp=&temp[0];
    				for(i=1; i<=strlen(search); i++)
        				*temp=*p;
        					p++;
        					temp++;
    			strcomp(temp, search);
    		else if(*p!=*search)
    			p++;
    		
    		/*  I am really gettin confused w/ this function at this point...  how do I start to check character by character, etc? */
    			
    		}
    		
    		
    	}	
    }
    
    main()
    {
    	char original_str[MAX_LEN], s_str[MAX_LEN], r_str[MAX_LEN], outp_str[MAX_LEN];
    	int index;
    	
    	printf("Original text:");
    	gets(original_str);
    	
    	printf("s=:");
    	gets(s_str); 
    	
    	printf("r=:");
    	gets(r_str);
    	
    	printf("New Sentence: %s %s, %s\n", original_str, s_str, r_str);
    }
    Attached Files Attached Files

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Think about this case

    Original=Hello World
    Find=Hello
    Replace=Goodbye

    The thing to think about is what are you going to do with " World" when you replace 5 characters with 7 characters.

    Use a separate string for the result, it will be so much easier.

    On other notes
    1. NEVER USE gets() - see the FAQ
    2. Be explicit about the return types of all your functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Some things to consider/be aware of. Is the function meant to replace multiple instances of the search string with the replace string? For example:

    String = "When the hen is then."
    Search = "he"
    Replace = "i"
    Is the result = "Win the hen is then."
    Or is it = "Win ti in is tin."

    And then combine multiple replacement with the possible effects of the search string being a part of the replace string? For example:
    String = "bababa"?
    Search = "ba"
    Replace = "abba"
    1. Find first "ba" = "bababa"
    2. Replace with "abba" = "abbababa"
    3. Find next "ba" to replace
      • Is it "abbababa"?
      • Or is it "abbababa"?
      • The first assumption will lead to infinite replacement, the second won't
    4. Etc., etc., etc.

    Does your requirement address any of those issues or even need to address them at all? Just wondering. If you're just considering a single replacement then none of this matters.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  4. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21