1

Firstly, I'm trying to create a program which takes two char arrays and removes from the first any chars that shew up in the second array. I have added comments to make it easier to understand what's happening.

   #include <stdio.h>

void rcfs(char s[], char t[]){
    int i=0, j=0;
    while (t[i] != '\0')
        ++i; /*find size of t*/
    for (int x=0;x<i;++x){ /*go through both vals of x and
    check vals of s to see if they equal a val of x, if so set j of s to 0*/
        while (s[j]!=0){
            if (s[j]==t[x]){
                s[j]=0;
            }
            j++;
        }
        if (x<i-1)
            j=0;
    }
    char new[8]; /*new char arr to be assigned to s*/
    int x=0; //seperate counter that increments only when chars are added to new
    for (int q=0;q<j;q++){
        if (s[q]!=0)
            new[x++]=s[q];
    }
    new[7]=0;
    s=new; //maybe these need be pointers
}

int main(){
    char s[]="I eat food";
    char t[]="oe";
    printf("Pre: %s\n", s);
    rcfs(s, t);
    printf("Post: %s", s);
    return 0;
}

Here is the output: img Instead of 'Post: I' it should be: 'Post: I at fd' To summarise, I'm getting the wrong output and I'm currently unaware of how to fix it.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    First: you did well adding comments in your code. Next thing is start debugging: did you check what's going wrong using a debugger? – Dominique Jan 25 '22 at 08:21
  • I use notepad so I don't have a debugger – rookieProgrammer Jan 25 '22 at 08:26
  • 2
    Please don't post graphics containing a text that you can easily copy from the command prompt. – harper Jan 25 '22 at 08:26
  • 2
    You change what `s` points to within the function scope, but that change will not be visible outside of the function scope. The easiest solution would be not to modify what `s` points to, but simply put all values to the start of the array and move the zeroes to the end. – Cheatah Jan 25 '22 at 08:27
  • [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/q/285551/995714). Notepad isn't a tool for programming, why don't just use VS when you already have cl.exe? – phuclv Jan 25 '22 at 09:01
  • Or better yet, use (neo)vim. Just try it. You'll never go back. – Roflcopter4 Jan 25 '22 at 09:14
  • @rookieProgrammer: I'm sorry, but that won't work: you might get some small things done, but very soon you'll realise you need a debugger to get the work done. – Dominique Jan 25 '22 at 10:44
  • 1
    'use notepad so I don't have a debugger'....you are unable to program computers because you don't have the required tools. Trying is wasting everyone's time. Debuggers like gdb can be used in terminal windows - you don't absolutely need an IDE, (though it's a very good idea to use one). – Martin James Jan 25 '22 at 10:48

3 Answers3

3

I suggest that you turn the algorithm around and loop over s in the outer loop. I also suggest that you use strlen to find the length of strings and strchr to find out if a character is present in a string.

Example:

#include <string.h>

void rcfs(char *s, char t[]) {   
    char *sw = s; // current writing position

    for(; *s != '\0'; ++s)  {        // s is the current reading pos
        if(strchr(t, *s) == NULL) {  // *s is not in t
            *sw = *s;                // save this character at sw
            ++sw;                    // and step sw
        }        
    }

    *sw = '\0'; // null terminate
}

Demo

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
3

Your approach is entirely wrong and erroneous.

For example this while loop

    while (s[j]!=0){
        if (s[j]==t[x]){
            s[j]=0;
        }
        j++;
    }

can be unable to find a character equal to the character t[x] if already within the string s you set some preceding character to 0.

For example if s = "12" and t = "12" then after the first iteration of the for loop

for (int x=0;x<i;++x){

the array s will look like s = "\02" So the while loop in the second iteration of the for loop will exit at once due to the condition

while (s[j]!=0){

This declaration of a character array with the magic number 8

char new[8];

does not make a sense. The user can pass to the function strings of any length.

In this assignment statement

s=new

the local variable s of the pointer type char * (due to adjusting by the compiler the parameter having an array type to pointer to the array element type) is assigned by the address of the first element of the local array new. This does not change the source array s declared in main.

Without using standard C string functions your function can be declared and defined the following way as it is shown in the demonstration program below.

#include <stdio.h>

char *rcfs( char s1[], const char s2[] )
{
    if (*s2)
    {
        char *src = s1, *dsn = s1;
        do
        {
            size_t i = 0;
            while (s2[i] != '\0' && s2[i] != *src) ++i;
            if (s2[i] == '\0')
            {
                if (src != dsn) *dsn = *src;
                ++dsn;
            }
        } while (*src++);
    }

    return s1;
}

int main( void )
{
    char s[] = "I eat food";

    printf( "Pre: %s\n", s );

    printf( "Post: %s\n", rcfs(s, "oe"));
}

The program output is

Pre: I eat food
Post: I at fd
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

To begin with:

Instantiate char new[8] outside of function rcfs, pass it to that function, and print it after the function returns:

char new[8];
rcfs(s, t, new);
printf("Post: %s", new);

The function should be declared as void rcfs(char s[], char t[], char new[]) of course.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • I haven't referred to the rest of your function `rcfs` implementation, which seems rather unsafe. –  Jan 25 '22 at 08:27
  • Why not just fix the function instead of changing the signature? – Gerhardh Jan 25 '22 at 09:08
  • @Gerhardh: The function generates the (attempted) output in a statically allocated local array, which becomes invalid outside the scope of this function. In order to fix it without changing its prototype, you'd need to allocate that array either in global scope, or dynamically. I find the first option bad, and the second option confusing for this dude under the given context (although, it is correct to say that the output should probably be allocated dynamically, because its exact maximum length is unknown during compilation). –  Jan 25 '22 at 09:12
  • 1
    Hence I've figured to start by pointing out the very basic issue that this function suffers from. –  Jan 25 '22 at 09:13
  • 1
    The intention of the function is to provide the result back to the caller in the first array. This means a straight forward solution would be to copy the content into `s[]` from the local temp variable. (Additionally the size of that `new[8]` should be changed to the actual needed size.) There is no need to put the burden of allocating temporary memory to the caller if you can do it in the called function. – Gerhardh Jan 25 '22 at 09:23