6 Replies - 1816 Views - Last Post: 04 January 2011 - 01:39 PM Rate Topic: -----

#1 gm1644   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-January 11

Help with Replacing Strings/substrings in C

Posted 04 January 2011 - 10:16 AM

Hi this is my first post on any site like these and i would really appreciate your help. 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];
									
	
	do{
		
		do 
		{
			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; i<strlen(string1); i++)
				{
					if(stringAtStart(string1,string2, i))
					{
						puts(string3);
						i=i+strlen(string2);
						printf("4  \n");
					}
					else
						
					   puts (string1[i]);
				}
		}	
		
	} while (pickednum !=3);
	
	
	if (pickednum = 3)
		exit;
}

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);
			break;
		}
		if ((string1[i] == string2[index]) )
		{
			counter --;
			index ++;
			printf("2 counter = %d    lengthofstring2 = %d index = %d\n", counter,strlen(string2),index);
		}
		else
		{
			counter = strlen(string1);
			index=0;
			printf("3  counter = %d  index = %d\n", counter, index);
		}
	}
		return inString;

}	




Is This A Good Question/Topic? 0
  • +

Replies To: Help with Replacing Strings/substrings in C

#2 jimblumberg   User is offline

  • member icon

Reputation: 5812
  • View blog
  • Posts: 17,743
  • Joined: 25-December 09

Re: Help with Replacing Strings/substrings in C

Posted 04 January 2011 - 12:00 PM

Does this code compile without error/warning messages?

If not please post the complete error/warning message.
(Note: The above code should not compile without error/warnings)

Jim
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Help with Replacing Strings/substrings in C

Posted 04 January 2011 - 12:25 PM

What also might be good here is if you explain your logic.
Was This Post Helpful? 0
  • +
  • -

#4 gm1644   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-January 11

Re: Help with Replacing Strings/substrings in C

Posted 04 January 2011 - 01:06 PM

yea it compiles without any error messages. just a few warnings. my logic is after intaking the requested arrays to pass the code to a seperate function to see if the substring the user chose to replace is even contained in the original array. if it is it runs the function and returns back to the main to print the replacing substring with instead of the original one. when an index is not similar to the requested substring it prints that index in a loop.

thanks for the responses so far. im really having trouble on this code. for days now i cant figure out how to finish it off and i need to finish it today.

thanks!
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg   User is offline

  • member icon

Reputation: 5812
  • View blog
  • Posts: 17,743
  • Joined: 25-December 09

Re: Help with Replacing Strings/substrings in C

Posted 04 January 2011 - 01:23 PM

Quote

just a few warnings.


What are they? Please post the entire warning messages.

Please explain the following code.

	if (pickednum = 3)
		exit;



Jim
Was This Post Helpful? 0
  • +
  • -

#6 gm1644   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-January 11

Re: Help with Replacing Strings/substrings in C

Posted 04 January 2011 - 01:29 PM

im sorry maybe i didnt understand you before. the program compiles fine without any errors. however when i run the program i receive the error:

"Program received signal: “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all"

about half through running my program when it is looping throught the last "else" in my function at the end of my code.

about the "if (pickednum==3) exit;" that was accidentally left in there by me because this code is part of 3 cases of a bigger code of mine.

thanks!
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg   User is offline

  • member icon

Reputation: 5812
  • View blog
  • Posts: 17,743
  • Joined: 25-December 09

Re: Help with Replacing Strings/substrings in C

Posted 04 January 2011 - 01:39 PM

When I compile this code I get the following warning:

Quote

main.c|52|warning: passing argument 1 of ‘puts’ makes pointer from integer without a cast|


See this link on puts().


Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1