Thread: Help with Replacing Strings/substrings in C

  1. #1
    Registered User
    Join Date
    Jan 2011

    Help with Replacing Strings/substrings in C

    Hi this is my first post on this site. I am a beginner in c in college. I am writing the program in XCode.

    My assignment is to ask the user for a string, ask for a substring to replace, ask what to replace it with, and then print out the original string with the replaced substring. I am allowed to use any functions in <string.h> aside from strstr.

    i have had errors with my code for a while now and now am receiving the error

    Program received signal: “EXC_BAD_ACCESS”.

    at the "else" part of the "stringAtStart" function"

    for some reason it is not looping properly i think. and when i try debugging it manually "with printf's" that specific "else" keeps running continuously.

    i really need your help.

    thank you in advance!

    #include <stdio.h>
    #include <string.h>
    #define Max 40
    int stringAtStart(char string1[], char string2[], int Si);
     function name: main
     the input: none
     the output: none
     the function operation: asking the user to pick a number between 1 and 3 and then based on the number picked computing 3 different options 
    int main (void) 
    	int pickednum, i;
    	char string1[Max], string2 [Max], string3 [Max];
    			printf("Please enter option\n");
    			scanf("%d", &pickednum);
    			if (pickednum>3)
    				printf("illegal option \n");
    		} while (pickednum > 3);
    		switch (pickednum) 
    			case 1: 
    				printf("Please enter a string\n");
    				scanf("%s", string1);
    				printf("Please enter string to replace\n");
    				scanf("%s", string2);
    				printf("Please enter replacing string\n");
    				scanf("%s", string3);
    				//for (i=0; string1 [i] != '\0'; i++)
    				//printf("%d", contains (string1, string2, i));
    				for(i=0; i<strlen(string1); i++)
    					if(stringAtStart(string1,string2, i))
    						printf("4  \n");
    					   puts (string1[i]);
    	} while (pickednum !=3);
    	if (pickednum = 3)
    int stringAtStart(char string1[], char string2[], int Si)
    	int i = Si, index=0, counter = strlen(string1), inString = 0;
    	for (i ; index < strlen(string2); i++)
    		if (counter == 0)
    			inString =1;
    			printf("1 %d\n", counter);
    		if ((string1[i] == string2[index]) )
    			counter --;
    			index ++;
    			printf("2 counter = %d    lengthofstring2 = %d   string2= %d index = %d\n", counter,strlen(string2), strlen(string1),index);
    			counter = strlen(string1);
    			printf("3  counter = %d  index = %d\n", counter, index);
    		return inString;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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
    Join Date
    Sep 2006
    I'm not sure what your logic is trying to do. Here's how I'd suggest doing it:

    get the strings
    get the length of each string
    for i < stringlen(string1) - stringlen(string2);i++) {
      if (string1[i]== string2[0))  //you have found the base of the string to be replaced
                                                //if string2[0] is the "target" string to be replaced 
                                                //otherwise, use a target string for this
        now call your replace function
        and replace the chars in the called function,
      end if  
    }//end of for loop
    That way you never have to mix the logic of replacing char's, with the logic of finding the starting point to replace the char's. When you work with strings, it's important to keep it simple, but efficient.

  4. #4
    Registered User
    Join Date
    Jan 2011
    the logic you gave as i understand it only seems to work with replacing the substring with one index of the new substring. how would you replace the original substring with an array of more than 1 index?

    thanks for the response before!

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    if (pickednum = 3)
    There are many things wrong with that:
    1. You probably mean to use == instead of =
    2. exit is a function and to call it you must do so with parenthesis, e.g.: exit();
    3. Given how it is placed within your main function, the code would seem to serve no useful purpose... the program will end on its own just fine without this bit present
    "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

  6. #6
    Registered User
    Join Date
    Sep 2006
    It's always best to have an example, some pseudo-pseudo code.

    In string1, replace "Frank" with string2

    char string1[]= "Abbey, Bob, Charlie, Daniel, Frank, Geraldine"
    char string2[]= "David"
    len1 = strlen(string1)
    len2 = strlen(string2)
    for(int i=0;i<len1-len2;i++) {
      if(string1[i] == string2[0]) {
         //works because string2[0] is the only "D" in string1. If there were other D's in string1
         //then use a 
         int match=0;
           call replace();
        //in any case, when the char's line up correctly, call replace()
        replace(string1, string2, len2);
        //and make the replacements. Since both Frank and David have 5 letters, no additional
       //moving of the char's within string1 is necessary. If you were replacing
       //Frank with Franklin, then you'd have to first move the char's after "Frank" in 
       //string1, up by 3 spaces, and that includes the unseen end of string char: '\0'
       //which is located at the end of all strings in C.
    The idea is the same - keep the replace logic, separate from the "line up" logic. Makes it easier (by far).

    If that logic doesn't meet your needs, please post up an example of what you want to do. Descriptions can only go so far.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replacing character is character is different
    By s10062971 in forum C# Programming
    Replies: 4
    Last Post: 11-07-2010, 06:45 AM
  2. Replacing switch statement with a nested if/else
    By dev123 in forum C Programming
    Replies: 27
    Last Post: 09-06-2010, 12:30 AM
  3. Replacing Characters
    By carrotcake1029 in forum C Programming
    Replies: 3
    Last Post: 04-28-2008, 01:08 PM
  4. replacing a line in a file
    By hannibar in forum C Programming
    Replies: 2
    Last Post: 05-19-2005, 10:38 AM
  5. Replacing a notebook drive
    By RoD in forum Tech Board
    Replies: 2
    Last Post: 03-29-2005, 07:07 AM