-1

I am really new to programming and this is my first question, I would be grateful for either a code, an article where similar problem is explained or even a suggestions on how to proceed because its for my school and I just don't know what to do because I had covid and wasn't able to attend recent classes.

The problem was described in the title so I will just add what some specifications and what I thought of:

  • the strings are only words with spaces in between them, nothing else, begins and ends with no spaces

So it has to be like this:

#include <stdio.h>
int main() {

  char s[] = "ab bb cc oh hey there dd";
  char w[] = "ab cc dd oho hello";

  int count = delete_words(s, w);

  printf("deleted: %i\nnew_string: %s", count, s);

  /* 
     the printf should print: 

     deleted: 3
     new_string: bb oh hey there 
   */
 return 0;
}

If you took the time to read all this I thank you.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Raier147
  • 1
  • 2
  • 5
    You should talk to your tutor/teacher. If you really don't have an idea how to do this in C, you are going to be in a difficult position as it is not a trivial exercise. – jarmod Nov 19 '21 at 22:12
  • If by that you mean how to create a user defined function then yes the parameters should be int delete_words(char src[], char words[]); just dont know what to do with the body, I would ask my tutor but as its an "marked homework" he wouldnt tell me – Raier147 Nov 19 '21 at 22:13
  • One of the first steps I would do is write a simple loop or two that will iterate through *one* of the input strings and identify all the words in it. To prove to yourself that this code is working correctly, maybe you would print each word on its own line. But don't do this in a simplistic way where you just iterate through each character and change spaces into `\n`; you should actually construct a new string that holds the word, and has a null terminator. – David Grayson Nov 19 '21 at 22:20
  • 1
    `bb oh hey there` misses out on `oho` and `hello`; are you sure that it's not "takes two strings and deletes all word matches in the first string to any of the second string and returns the number of deleted words"? Arranging them in order might help. – Neil Nov 19 '21 at 22:22
  • 1
    yes im sorry bad wording on my part, updated the title, will try your suggestions thank u very much ! – Raier147 Nov 19 '21 at 22:39
  • 3
    I will never understand how / why instructors insist on hamstringing their students by NOT allowing standard headers instead of showing how to employ them efficiently. I blame laziness in creating these "problems". – DevSolar Nov 19 '21 at 22:44
  • If you wanted to be fast for a large number of strings, [split string with delimeters](https://stackoverflow.com/questions/9210528/split-string-with-delimiters-in-c), then put it into [set data structure](https://stackoverflow.com/questions/2630738/c-how-to-implement-set-data-structure) with [hash function for strings](https://stackoverflow.com/questions/7666509/hash-function-for-string). (May be a little overkill.) Or you could just sort each of them. – Neil Nov 19 '21 at 22:44
  • Is the second string constant? Because you could pre-compile that to into a DFA to get very fast speeds. – Neil Nov 19 '21 at 23:17
  • @Neil worth remembering this is someone new to programming. – jarmod Nov 19 '21 at 23:20
  • There are 3 ways to do this, (1) character-by-character identify words in `w`. Then loop through `s` (with a nested loop) to find word in `s`. If found, (say 2 chars in word + 1 space after) loop from current to end of `s` overwriting with `s[i] = s[i+3];` (2) use a combination of `strspn()`, `strcspn()` to find words in `w` and then `strstr()` to find occurrences in `s` and `memmove()` to move end of `s` to current. (3) find words in `w` with `strtok()` (NOTE: `w` is modified) and use same `strstr()` and `memmove()`. That about it. Take you pick. – David C. Rankin Nov 20 '21 at 00:29
  • @DavidC.Rankin which are all declared in `string.h`. :[ – Neil Nov 20 '21 at 00:56
  • 1
    None are declared in `string.h` if you choose option (1), that's simply pointer arithmetic and a couple of counters. – David C. Rankin Nov 20 '21 at 02:28
  • https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – eagle33322 Dec 02 '21 at 22:27

1 Answers1

0

Maybe you should start by solving, ostensibly simpler, problems, then combining them.

Subtract sets

A function that deletes all the numbers from an array of int that appear in a second array, and returns the number of deleted numbers.

#include <stdio.h>

static size_t delete_ints(int *n, const size_t n_size,
    int *sub, const size_t sub_size) {
    /* TODO */
    return 0;
}

int main(void) {
    int n[] = { 1, 2, 3, 10, 11, 12, 4 };
    const size_t n_size = sizeof n / sizeof *n;
    int sub[] = { 1, 3, 4, -14 };
    const size_t sub_size = sizeof sub / sizeof *sub;
    size_t i;
    size_t count = delete_ints(n, n_size, sub, sub_size);

    printf("deleted: %zu\nnew ints: {", count);
    for(i = 0; i < n_size - count; i++) printf("%s %i", i ? "," : "", n[i]);
    printf(" }.\n");
    return 0;
}

The size parameters are required because an array of int doesn't have a sentinel '\0' like a C string, but this is approximately the same thing. It can be solved in several different ways, with varying complexities.

Word matching

Given a string and a word, delete all words that match word from string, and return the number of words deleted. (And by deleted I mean overridden with chars from further on in the array.)

#include <stdio.h>

static size_t delete_word(char *words, char *sub) {
    /* TODO */
    return 0;
}

int main(void) {
    char words[] = "ab bb cc oh hey there dd";
    char sub[] = "cc";
    size_t count = delete_word(words, sub);

    printf("deleted: %zu\nnew string: %s\n", count, words);

    return 0;
}
Neil
  • 1,767
  • 2
  • 16
  • 22