int match(const char *s, const char *p, int overlap) { int c = 0; int a=strlen(p
ID: 3542763 • Letter: I
Question
int match(const char *s, const char *p, int overlap)
{
int c = 0;
int a=strlen(p);
while (*s != '') {
if (strncmp(s++, p, a)){
continue;
}
if (!overlap){
s += a - 1;
c++;
}
}
return c;
}
int main()
{
char string1[80];
char string2[80];
printf("enter strings1");
scanf("%s",string1);
printf("enter strings2");
scanf("%s",string2);
printf("%d ", match(string1,string2, 0));
system("Pause");
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <string.h>
char *replace_str(char *str, char *orig, char *rep)
{
int current_index = 0;
static char buffer[10000];
if (!strstr(str, orig)) // Is 'orig' even in 'str'?
{
return str;
}
while (1)
{
char *p;
if (!(p = strstr(str + current_index, orig))) // Is 'orig' even in 'str'?
{
return buffer;
}
strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$
buffer[p-str] = '';
sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
printf("%d -> %s ", current_index, buffer);
current_index = (p - str) + strlen(rep);
str = buffer;
}
return buffer;
}
int main(void)
{
puts(replace_str("hello world world", "world", "world2"));
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.