anemos
asked on
string manipulation
do {
while (*copy=='F' && *copy!=0) ++copy;
if (*copy==0) break;
while (*copy!='F' && *copy!=0) {*ptr=*copy; ++ptr; ++copy;};
if (*copy==0) break;
++ptr;
*ptr=0;
strcat(ptr,transform);
} while (1); /* condition of the loop is tested in the 2nd statement */
========================== ========== ===
When this loop is compiled I get a bus error.
What this is meant to do is the following.
copy holds a string.
ptr is empty.
transform holds a string as well.
they have all been declared as char * and have been allocated enough space using malloc.
I want to copy the contents of the variable copy into the variable ptr but whenever there is a char 'F' in the variable copy instead of copying 'F' into ptr copy the whole contents of transform into ptr.
for example
copy = "ggfhhhFoooFl"
transform="555"
so the result I want should be:
ptr = " ggfhhh555ooo555l"
I hope I am clear enough.
while (*copy=='F' && *copy!=0) ++copy;
if (*copy==0) break;
while (*copy!='F' && *copy!=0) {*ptr=*copy; ++ptr; ++copy;};
if (*copy==0) break;
++ptr;
*ptr=0;
strcat(ptr,transform);
} while (1); /* condition of the loop is tested in the 2nd statement */
==========================
When this loop is compiled I get a bus error.
What this is meant to do is the following.
copy holds a string.
ptr is empty.
transform holds a string as well.
they have all been declared as char * and have been allocated enough space using malloc.
I want to copy the contents of the variable copy into the variable ptr but whenever there is a char 'F' in the variable copy instead of copying 'F' into ptr copy the whole contents of transform into ptr.
for example
copy = "ggfhhhFoooFl"
transform="555"
so the result I want should be:
ptr = " ggfhhh555ooo555l"
I hope I am clear enough.
ASKER
I am 100% sure that I have allocated enough space for all of them.
Hi anemos,
Iexpert is right, remove the
++ptr;
before th
*ptr=0;
ZOPPO
Iexpert is right, remove the
++ptr;
before th
*ptr=0;
ZOPPO
1)try to use '\0' instead of 0
2)i thing that you got a logic mistake in the first while:
>> while (*copy=='F' && *copy!=0) >>++copy;
'cause if *copy=='F' it ofcourse !=0
3)you do ++ptr then *ptr=0 why do you do the ++ptr ptr points to the next empty place each time so you don't need to do the ++ptr
4)you do strcat(ptr,transform) but ptr doesn't point to the biggining of the string
you have to keep another pointer that will point to the biggining of ptr like
char *start
start=ptr
and then enter you loop
and instead of strcat(ptr,transform)
do strcat(start,transform);
5)after you do strcat you don't move ptr to the end of the string
6) did you hear about a func named strtok
and about strcat don't forget that it doesn't put '\0' at the end
7)
char *start=ptr;
while (*copy!='\0'){
while(*copy!='F'&&*copy!=' \0'){
*ptr=*copy;
ptr++;
copy++;
}
*ptr='\0';
if (*copy=='F'){
strcat(start,transform);
ptr+=strln(transform)+1;
}
}
i think that should work for you
<i writed it here i mean i didn't compile it but i think it will work>
but you can use strtok and strcat it will be much easyer i just don't know if you want to use them so i didn't use it
2)i thing that you got a logic mistake in the first while:
>> while (*copy=='F' && *copy!=0) >>++copy;
'cause if *copy=='F' it ofcourse !=0
3)you do ++ptr then *ptr=0 why do you do the ++ptr ptr points to the next empty place each time so you don't need to do the ++ptr
4)you do strcat(ptr,transform) but ptr doesn't point to the biggining of the string
you have to keep another pointer that will point to the biggining of ptr like
char *start
start=ptr
and then enter you loop
and instead of strcat(ptr,transform)
do strcat(start,transform);
5)after you do strcat you don't move ptr to the end of the string
6) did you hear about a func named strtok
and about strcat don't forget that it doesn't put '\0' at the end
7)
char *start=ptr;
while (*copy!='\0'){
while(*copy!='F'&&*copy!='
*ptr=*copy;
ptr++;
copy++;
}
*ptr='\0';
if (*copy=='F'){
strcat(start,transform);
ptr+=strln(transform)+1;
}
}
i think that should work for you
<i writed it here i mean i didn't compile it but i think it will work>
but you can use strtok and strcat it will be much easyer i just don't know if you want to use them so i didn't use it
the follwing should solve your problem its a function which i use two swap a single quote in a string with 2 quotes (escaping sql)
char *replace(char *in_string, char *rep_with, char rep)
it return a new string leaving the old one in tackt..
////////////////////////// //////////
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <conio.h>
char *replace(char *string, char *rep_with, char rep_on)
{
char *new_s = NULL;
int i = 0, a = 0;
new_s = (char*)malloc(strlen(strin g) + 1);
if(new_s)
{
for(i=0;i<strlen(string);i ++)
{
if(string[i] != rep_on)
{
new_s[a] = string[i];
new_s[a + 1] = '\0';
a++;
}
else
{
new_s = (char*)realloc(new_s,strle n(rep_with ) + 2 + strlen(new_s));
if(new_s)
{
strcat(new_s,rep_with);
a = a + strlen(rep_with);
}
}
}
}
return new_s;
}
void main()
{
printf("%s\n",replace("hel loFpaulFho wFareFyouF todayFmumm a"," - ",'F'));
getch();
}
}
hope this helps
char *replace(char *in_string, char *rep_with, char rep)
it return a new string leaving the old one in tackt..
//////////////////////////
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <conio.h>
char *replace(char *string, char *rep_with, char rep_on)
{
char *new_s = NULL;
int i = 0, a = 0;
new_s = (char*)malloc(strlen(strin
if(new_s)
{
for(i=0;i<strlen(string);i
{
if(string[i] != rep_on)
{
new_s[a] = string[i];
new_s[a + 1] = '\0';
a++;
}
else
{
new_s = (char*)realloc(new_s,strle
if(new_s)
{
strcat(new_s,rep_with);
a = a + strlen(rep_with);
}
}
}
}
return new_s;
}
void main()
{
printf("%s\n",replace("hel
getch();
}
}
hope this helps
SORRY the above is wrong.. this is right. iut works ive just tested it .>
char *replace(char *buf, char *with, char rep)
{
int i = 0;
char *c, *t, temp[2];
/* get the number of ampersnads */
t = buf;
do
{
c = strchr(t,rep);
if(c)
{
i++;
t = c + 1;
}
}while(c);
c = (char*)malloc((i * strlen(with)) + (strlen(buf) - i) + 10);
if(c)
{
strcpy(c,"");
for(i=0;i<=strlen(buf);i++ )
{
if(buf[i] == rep)
{
strcat(c,with);
}
else
{
temp[0] = buf[i];
temp[1] = '\0';
strcat(c,temp);
}
}
}
return c;
}
doews this solve your probelem
char *replace(char *buf, char *with, char rep)
{
int i = 0;
char *c, *t, temp[2];
/* get the number of ampersnads */
t = buf;
do
{
c = strchr(t,rep);
if(c)
{
i++;
t = c + 1;
}
}while(c);
c = (char*)malloc((i * strlen(with)) + (strlen(buf) - i) + 10);
if(c)
{
strcpy(c,"");
for(i=0;i<=strlen(buf);i++
{
if(buf[i] == rep)
{
strcat(c,with);
}
else
{
temp[0] = buf[i];
temp[1] = '\0';
strcat(c,temp);
}
}
}
return c;
}
doews this solve your probelem
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
also the malloc grabs an extra 10, thats a typo it should be 1 for the NULL
ASKER
I am afraid Makerp that I'll have to give the point to ntdragon as he first gave me a satisfactory answer.
Thanx for your help guys..
catch u later...
Thanx for your help guys..
catch u later...
ASKER
well if i could cause i can't see a label saying accept comment as answer..
the most important is that we were able to help you:>
Are you sure you allocated enough space for ptr ?
Note in your e.g. it must be @ least be strlen(copy)*strlen(transf