Thread: Struggling w/ string_replacement function

  1. #1
    Registered User
    Join Date
    Oct 2007

    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.

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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Think about this case

    Original=Hello World

    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.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    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, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM