How to delete a string in an array in C

I am trying to delete a string (name) from an array using the below code. AddArray is to get an array from the user and RemoveArray is to delete the string

#include <stdio.h>
#include <stdlib.h>
#define LENGTH 5
unsigned char num = 0;
unsigned char numact = 0;
#include <string.h>
#define STR_MAX_SIZE 255
#define Names 5
#define Groups 5

void AddArray() {
  int i = 0;
  char word[Names][30] = {""};
  char word2[Groups][50] = {""};
  for (i = 0; i < Names; i++) {
    printf("--Input Name = ");
    if (!fgets(word[i], sizeof word[i], stdin))
      break;
    size_t len = strlen(word[i]);
    if (word[i][len - 1] == '\n')
      word[i][--len] = 0;
    printf("--Input Group = ");
    if (!fgets(word2[i], sizeof word2[i], stdin))
      break;
    size_t len2 = strlen(word2[i]);
    if (word2[i][len2 - 1] == '\n')
      word2[i][--len2] = 0;
  }
  putchar('\n');
}

void RemoveArray() {
  int position, i = 0, n = 5;
  char word[Names][30];
  char word2[Groups][50];
  printf("Enter the location where you wish to delete name\n");
  scanf("%d", &position);
  if (position >= n + 1)
    printf("Deletion not possible.\n");
  else
  {
    for (i = position - 1; i < n - 1; i++)
    {
      word[Names][i] == word[Names][i + 1];
      word2[Groups][i] == word2[Groups][i + 1];
      printf("Resultant array:\n");
      for (i = 0; i < n - 1; i++)
        printf("%d, %s, %s\n", i, word[i], word2[i]);
    }
  }
}

However, for some reason,

word[Names][i] == word[Names][i+1];
word2[Groups][i] == word2[Groups][i+1];

is not working (probably this is doing nothing), and my results only deletes the last string because of for (i = position - 1; i < n - 1; i++)

How can I delete a string from an array in C?



Read more here: https://stackoverflow.com/questions/64930342/how-to-delete-a-string-in-an-array-in-c

Content Attribution

This content was originally published by cathy at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: