By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,649 Members | 2,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,649 IT Pros & Developers. It's quick & easy.

remove substring and character

P: 3
for this c program, i'd like to remove the substring "cc" and character "=" from input string "aaccbbccdd" the code below can only remove "cc" once, can somebody help me figure out how to remove other "cc" in the string also how to remove multiple character '=' ?

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


char *Mystrcpy1(char *s, const char *cs);
void func1(char *src, char *sub);

int main(void)
{
char s[32], *substr = "cc";


Mystrcpy1(s,"==aaccbbccdd");
printf("From the string: \"%s\"\n",s);
printf("We will attempt to remove substring \"%s\"\n",substr);
func1(s,substr);
printf("Using function Mystrcpy1. The result: \"%s\"\n\n",s);

return 0;
}


char *Mystrcpy1(char *s, const char *cs)
{ /* Copy from beginning of string cs to the end */
char *s1;
const char *cs1;


for(s1 = s,cs1 = cs; '\0' != (*s1 = *cs1); s1++,cs1++) ;
return s;
}



void func1(char *src, char *sub)
{ /* Using function Mystrcpy1 */
char *p;


if ((p=strstr(src,sub)) != NULL)
Mystrcpy1(p,p+strlen(sub));
return;
}
Mar 2 '09 #1
Share this Question
Share on Google+
1 Reply


gpraghuram
Expert 100+
P: 1,275
Hi,
You have to call your replace function till you find a substring in your code that is till strstr returns NULL.
After doing replacement of cc you shuld call your function in the same way to replace =.

Raghu
Mar 2 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.