|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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]);
//}
}
|
|
#2
|
||||
|
||||
|
> 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. |
|
#3
|
||||
|
||||
|
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/ |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Replacing substring in string |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|