C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 4th, 2009, 12:51 PM
insidemain(c) insidemain(c) is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Posts: 3 insidemain(c) User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 30 sec
Reputation Power: 0
Replacing substring in string

I was going through the program (its solved in a book). It is supposed to take two string inputs from the user and then then search for the first string in the array of pointers to string and if its there replace it with teh second string. But i always keep getting exceptions and it does not seem to work.

Code:

#include <stdio.h>
#include <string.h>

int main(){

	char *s[]= {

			"We will teach you how to...",
			"Move a mountain",
			"Level a building",
			"Erase the past",
			"Make a million",
			"...all through C!"
		};
	char str1[20], str2[20];
	char *p, *news, t[20];
	int i, j, len1, len2;

	printf("\n\rEnter a string");
	scanf("%s", &str1);

	printf("\n\r Enter another string");
	scanf("%s", &str2);

	if(strlen(str2) > strlen(str1)){
		printf("\n Enter a string with only %d characters", strlen(str1));
		exit();
	}

	for(i = 0;i<6;i++){
		
		p = strstr(s[i], str1);
		printf("\n\r%d", p);
		if(p){
			
			news = p+strlen(str1);
			printf("\n\r%d",news);
			printf("\n%s",news); 
			strcpy(t, news);
			printf("\n\r%s", t);
			printf("\n\r%s", p);
			strcpy(p, str2);
			strcat(p, t);
			break;
		}
	}

	//printf("\n\rThe strings after removal are");
	//for(i=0;i<6;i++){
	//	printf("\n\r%s", s[i]);
	//}
}

Reply With Quote
  #2  
Old July 4th, 2009, 01:28 PM
salem's Avatar
salem salem is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jun 2005
Posts: 2,139 salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 1 Month 2 Weeks 1 Day 23 h 22 m 21 sec
Reputation Power: 861
> strcpy(p, str2);
> strcat(p, t);
p is a pointer to some s[i]

Those are string literals in read-only memory. Trying to modify them will get you a segfault.

You need to do the search/replace on a copy of those strings.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Reply With Quote
  #3  
Old July 4th, 2009, 04:31 PM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Click here for more information.
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 2,387 jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 5 h 48 m 31 sec
Reputation Power: 563
It used to be undefined whether such a program would work or not. I wouldn't be surprised if it's still undefined behavior. Many compilers used to store string literals in writable memory allowing for the OP's program to run. Notice the compiler didn't raise any errors or warnings for trouncing around in the string literal storage?

Even the target environment can make a difference. The compiler might mark the memory as "read only", but the linker/OS might not have any way to enforce that condition. These days, most systems are pretty good about enforcing the read-only status of the specified memory, but the compilers are still lax about detecting potential faults of this nature.

Embedded systems compilers often allow you to define whether a particular string literal be stored in writable RAM or some read-only memory. I sometimes wish the standards would allow something along the lines of:

char * volatile arr[] = { "Something", "Something else" }

The argument against this of course is that it creates potential security risks.
__________________
My worst nightmare was a pointless infinite loop.
Work in progress; don't poke the curmudgeon!
http://www.odonahue.com/

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Replacing substring in string


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
Stay green...Green IT