Computing.Net > Forums > Programming > C programming question HELP!

C programming question HELP!

Reply to Message Icon

Original Message
Name: ToPo
Date: February 20, 2003 at 09:37:18 Pacific
Subject: C programming question HELP!
OS: Win XP
CPU/Ram: 512SD
Comment:

Ok here's the problem i'm having...
I have to create a program that will remove all the occurences of one string in another string. I have to use pointers and include the strstr() and strcpy functions also.

What i got so far is:

#include
#include

main()
{
char msg1[] = "Could all the people in the room please stand";
char msg2[] = "the", *ps;
ps = msg1;
while(ps!=NULL)
{
....code to remove all occurrences???
}
printf("%s\n", msg1);
}

this is what i got so far, but i don't know what to write in the while loop... i've tried for a while now.. if anyone can help me it would be greatly appreciated.

Thanx,
ToPo


Report Offensive Message For Removal


Response Number 1
Name: gpp
Date: February 20, 2003 at 11:29:40 Pacific
Subject: C programming question HELP!
Reply: (edit)

I dont have time to write this, but heres an example that might help. The function locates the position of s1 within s2.

char *strstr(const char s1[], const char s2[]);

main(){
char *test;
char s1[MAX_LENGTH], s2[MAX_LENGTH];

cin.getline(s1, MAX_LENGTH);
cin.getline(s2, MAX_LENGTH);

if ((test=strstr(s1,s2)) != NULL){
cout "Substring found in position" test - s1 '\n';
}
else{
cout "No substring found\n";
}
}


char *strstr(const char s1[], const char s2[]){


const char *p1 = s1;
const char *p2 = s2;

while(*p1 != '\0'){

if(*p2 == *p1){


const char *p3;
p3 = p1;

while(*p2 != '\0'){

if(*p2 == *p3 ){

p2++;
p3++;
}
else{
p2=s2;
break;
}
}

if(*p2 == '\0'){

return p1;
}
}

*p1++;
}
return 0;

}


Report Offensive Follow Up For Removal

Response Number 2
Name: borelli34
Date: February 20, 2003 at 11:30:27 Pacific
Subject: C programming question HELP!
Reply: (edit)


==========================================================================================
ToPo,

I haven't had a chance to run this through the compiler yet (I have to rush off in just a few minutes) but this might get you started if its not perfect.


int n=0;
string st;

n==strstr(str1,str2);
while (n>0){
st=='';
st==strcpy(str1,1,n-1);
st==st+strcpy(str1,n-1+strlen(str2),strlen(str2));
return(st);

borelli34


Report Offensive Follow Up For Removal

Response Number 3
Name: ToPo
Date: February 20, 2003 at 13:33:43 Pacific
Subject: C programming question HELP!
Reply: (edit)

Thanks gpp and borelli34... I'll try it out and let u know how what happened.


Report Offensive Follow Up For Removal

Response Number 4
Name: ToPo
Date: February 20, 2003 at 13:48:49 Pacific
Subject: C programming question HELP!
Reply: (edit)

I tried both suggestions but i still can't get it to work... I couldn't really follow you gpp with what u were doing.. I'm not that advanced at this, and borelli i tried what you wrote but:

int n = 0;
n == strstr(str1,str2)

this doesn't work because it gives me the error saying that i cannot convert from a char* to an int. Do u guys have any other suggestions.. I'll keep working on it, but if you know something that could help please let me know.

Thanks,
ToPo


Report Offensive Follow Up For Removal

Response Number 5
Name: gpp
Date: February 21, 2003 at 07:52:20 Pacific
Subject: C programming question HELP!
Reply: (edit)

Close, but cant make this work. Its been a long time since I've written anything in C, and I just cant remember some of tricks for manipulating strings.. so far, the function divideStr will tokenize msg1 and return it to main.. from there, you need to compare each string within aParts to see if it has the substring msg2 in it... and if aParts has that token, jsut dont print it.

So far, all it does is print out each token.

#include iostream
#include string

void divideStr(char *s, char **aParts)
{
char *token = strtok(s, " ");
char **tempString;
while (token != NULL)
{
*aParts++ = token;
token = strtok(NULL, ".");
}

}


int main(){

char msg1[] = "Could all the people in the room please stand";
char msg2[] = "the";
char *aParts[9];
char sBuf[2000]; // make it big enough

strcpy(sBuf, msg1); // take a copy then
divideStr(sBuf, aParts);

for (int i = 0; i 8; i++){
cout aParts[i] " ";
}
cout endl;
return 0;
}


Report Offensive Follow Up For Removal


Response Number 6
Name: elric
Date: February 23, 2003 at 08:14:08 Pacific
Subject: C programming question HELP!
Reply: (edit)

G'day,

I'm not sure this will help too much, but it does work in QBASIC.I'm with gpp:it's a long time since I've used C. Take it as psuedo code or an algorithm if you like and convert it to C.
'Sring manipulation again...

CLS
'INITIALISATION
'##############

A$ = "Will all the men in the room stand up"
b$ = "the"
m% = 1
prev% = 1

'MAIN ALGORITHM
'##############

DO
P% = INSTR(m%, A$, b$)
m% = P% + LEN(b$)
IF P% > 0 THEN
msg$ = msg$ + MID$(A$, prev%, P% - prev%)
prev% = P% + LEN(b$)
END IF
LOOP UNTIL P% = 0

PRINT msg$ + MID$(A$, prev%, LEN(A$) - prev% + 1)

I think the instr() command is similar to the C strstr() function. LEN is similar to strlen() and you will need to use strcpy() to build the response message. You will then have to deal with C's obbsession with pointers yourself...

regards,
Elric


Report Offensive Follow Up For Removal

Response Number 7
Name: Gerd
Date: March 2, 2003 at 03:05:04 Pacific
Subject: C programming question HELP!
Reply: (edit)

Hi ToPo -
/* DO YOU STILL MISS YR SOLUTION? I READ ABOUT IT YESTERDAY(march 1st), and decided
to help you.
* BELOW IS A WORKING PROGRAM (REMEMBER: there are always more than 1 right solution)
* (running under DOS, compiled with MS C 6.0, but should work with any c compiler )
* BEST WISHES /Gerd
*/
#include
#include
char *removestring(char*,char*);/*FUNCTION DECLARATION*/
void main() /*NO ARGUMENTS...*/
{
char *cleaned_one; /*THE CLEANED CHARACTER STRING*/
cleaned_one=removestring("Adam og Eva gik i paradis og gik og lo","gik ");
printf("%s",cleaned_one); /* PRODUCES THE DESIRED OUTPUT*/
}

char* removestring(char *str1,char *str2)
{ char *oldstr,*remstr,*temp1str,*temp2str,*newtemp; /*CHAR PTRS*/
int oldlen,remlen,temp2len,part1len,part2len;/*STRING LENGTHS*/
int flag=1; /*FLAG FOR SEARCH LOOP*/
int i,j; /*COUNTERS*/

oldlen=strlen(str1);
remlen=strlen(str2);
temp1str=str1;
while(flag==1)
{
if(strstr(temp1str,str2)!=NULL)
{temp2str=strstr(temp1str,str2);
temp2len=strlen(temp2str);
part1len=oldlen-temp2len;
part2len=oldlen-part1len-remlen;
for(i=0;ipart1len;i++)
newtemp[i]=temp1str[i];
for(j=part1len;j(part1len+part2len);j++)
newtemp[j]=temp1str[j+remlen];
newtemp[part1len+part2len]='\0';
oldlen=oldlen-remlen;
strcpy(temp1str,newtemp);
}
else flag=0;
}
return(temp1str);
}


Report Offensive Follow Up For Removal

Response Number 8
Name: ToPo
Date: March 6, 2003 at 21:09:19 Pacific
Subject: C programming question HELP!
Reply: (edit)

Thanks a lot everyone who helped me but i figured it out and it was pretty easy after all... in fact the fuction i used only took 4 lines:

while (ps != NULL)
{
ps = strstr(msg1, msg2);
if (ps!= NULL)
strcpy (ps, ps + strlen (msg2));

}

i used this and it seemed to do the trick!


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: C programming question HELP!

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge