exclude people names from a C array

Asked

Viewed 74 times

0

Hello. I have a C algorithm to delete a user but not interact in the array:

#include<stdio.h>  
#include<stdlib.h>  
#include<locale.h>  
#include<conio.h>  

#define numeroUSER 100  

char nome[numeroUSER][100] = {"João Carlos","Maria Luisa","Pedro José", "Gerente"};  

int main() {  
excluirUsuario();  
}  

void excluirUsuario() {  
int indice=0;  
printf("informe o indice: \n");  
scanf("%d", &indice);  

for (int i=0; i<numeroUSER; i++) {  
nome[i]100] = nome[i+1][100];  
}  
}  

If I type the index 0, I want you to delete John from the list, since it is the first and remain the rest. The algorithm does not exclude and I end up deleting part of the names, that is, I can position myself in the string, but I can’t position myself between the indexes with the names. How do I delete this. Note, it is a programming logic treatise.

  • Hello Andre you have to enter the name and on top of that name find and delete from the array? It will have 100 names each array?

2 answers

2


Your code some mistakes and confusions.

  • nome[i]100] = nome[i+1][100]; - here lacked a [ so that the syntax was right, but I suspect that this was lost when building the question here ?

    Anyway, each string is only given by the first input, so nome[i], for the 100 already has to do with the letters of each one. By doing nome[i][100] would just copy the letter in position 100 not touching the rest.

    And I couldn’t do it either nome[i] = nome[i+1] as it is an array of chars, so you have to use strcpy, which is the function indicated for copying strings. Thus:

    strcpy(nome[i], nome[i+1]);
    

    That copies the string in nome[i+1] for nome[i].

  • for (int i=0; i<numeroUSER; i++) { - If you want to copy the names to the previous positions one by one, you have to start with the one you want to delete and not in the 0.

    Whether you are using the front member on for also can not go to the last because there is no front, so the end should be numeroUser - 1.

Correcting these points your for would look like this:

for (int i=indice; i < numeroUSER - 1; i++) {
    strcpy(nome[i], nome[i+1]); //copia o de i+1 para i
}

Now it’s important to remember that when you show the names that are left, you have to show less 1, which corresponds to the updated size after deletion:

printf("Usuarios restantes: \n");
int tamanho = numeroUSER - 1; //tamanho atualizado devido ao usuario removido
for (int i=0; i< tamanho; i++) {
    printf("%s\n", nome[i]);
}

See this example working on Ideone

Recommendations:

  • You included the header locale.h but ended up not using the function setlocale to define the encoding to be used.
  • Avoid using conio.h, because it is specific for windows. In the code shown there is not even any function that depends on this header.
  • Indent your code. This is far more important than you might think, and the code you have in the question does not have any indentation.
  • Do not set functions below your calls. Note that the function excluirUsuario comes after the main, when it is used in the main. You must declare the function before the main, or moves it completely to before the main.

0

1º Create an auxiliary char variable;

2º You must take the index that enters the parameter in the function and go through the array of the , until the , corresponding; (If there are numerous "," it may be easier to create an int array to store the positions that occur the ",")

3º The moment you find the name can inside the auxiliary;

4º Make a loop (similar to a Bubble Sort) and pass the last position of the , until the first;

Or you can use the Strtok (or strtoke) function to separate...

Example:

/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
  }
  return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.