Question:
Removing parts of strings--C Programming?
Grace
2013-04-10 17:11:30 UTC
Any help with this would be greatly appreciated!!

Write a function called str_remove() that removes all occurrences of the second string from
the first string.
In function main do the following:
a) Declare a character array called list1 of size 500
b) Declare another character called list2 of size 20
c) Read a string from the keyboard and store it in list1
d) Read another string from the keyboard and store it in list2
e) Call function str_remove() to remove all occurrences of list2 from list1
f) Display list1

Function str_remove():
void str_remove(char *str1, char *str2);

Example:
If the user entered for the 1st string: “Hello World”
And the user entered for the 2nd string: “llo”
The output should be: “He World”

Another Example:
If the user entered for the 1st string: “it’s not a bug – it’s an undocumented feature”
And the user entered for the 2nd string: “it’s ”
The output should be: “not a bug – an undocumented feature”

You can using
Five answers:
cja
2013-04-10 17:40:24 UTC
It's great that you can use , because it has functions that will do most of the work for you. So, check your textbook or online references, and learn all about those functions. Your program should look something like this:



#include

#include



#define MAX_LINE_LEN 500



void get_string(const char *, char *, size_t);

void str_remove(char *, const char *);



int main(int argc, char *argv[]) {

    char str1[MAX_LINE_LEN] = { '\0' },

        str2[MAX_LINE_LEN] = { '\0' };



    while (1){

        get_string("str1> ", str1, MAX_LINE_LEN);

        get_string("str2> ", str2, MAX_LINE_LEN);

        str_remove(str1, str2);

        puts(str1);

    }

    return 0;

}



/* remove all occurrences of s2 from s1 */

void str_remove(char *s1, const char *s2) {

    char *p = s1;

   

    do {

        if ((p = strstr(p, s2)) != NULL) {

            memmove(p, p + strlen(s2), strchr(p, '\0') - p);

        }

    } while (p != NULL);

}



void get_string(const char *prompt, char *s, size_t n) {

    char *p;



    do {

        printf("%s", prompt);

        fgets(s, n, stdin);

        if ((p = strchr(s, '\n')) != NULL) *p = '\0';

    } while (strlen(s) == 0);

}





#if 0



Sample run:



str1> it's not a bug - it's an undocumented feature

str2> it's

  not a bug - an undocumented feature

str1> Hello World

str2> llo

He World

str1> 1a2a3a4a

str2> a

1234

str1> abc

str2> abc



str1>



#endif
roger
2013-04-10 17:17:16 UTC
if you are allowed use functions declared in then the use of strstr() would be useful.



The rest is fairly easy...
Paige
2016-03-08 12:34:45 UTC
This should work with some changes (array sizes adjusted to 500 and 20, add feature for recording user input)... #include "stdafx.h" #include int _tmain(int argc, _TCHAR* argv[]) { char * bigString = "Hello World"; char * searchString = "llo"; char output[100]; int offset=0,offset2,begin,offset3=0; while(bigString[offset]) { offset2=0; begin=offset; while(bigString[begin]== searchString[offset2] && bigString[begin]) { begin++;offset2++; if(!searchString[offset2]) { offset=begin; continue; } } output[offset3++]=bigString[offset++]... } output[offset3]='\0'; printf("%s\n",output); system("pause"); return 0; }
anonymous
2013-04-10 18:00:52 UTC
You didn't even show your attempt. This is easy for someone who has been learning and paying attention to their lesson for a month or less.
anonymous
2013-04-10 17:58:34 UTC
here is a simple code with no error checking. just do it yourself.‎

char* str_remove(const char* src, const char* search)

{

unsigned int srclen = strlen(src);

unsigned int searchlen = strlen(search);

char* newstr = (char*)malloc(srclen);

char tempstr = (char*)malloc(srclen);

strcpy(newstr, srcstr);

char format[20];

for(;;)

{

char* loc = strstr(newstr, search);

if (loc == 0) break;

sprintf(format, "%s%d%s", "%.", ((int)loc)-((int)newstr), "s%s");

sprintf(tempstr, format, newstr, &loc[searchlen]);

strcpy(newstr, tempstr);

}

free(tempstr);

return newstr;

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...