How to separate vowel consonants into a matrix in C

Asked

Viewed 717 times

0

Hello, I have a problem with my code. I have a matrix[6][3] that reads letters, after reading I want to separate the vowels from the consonants. However I did not succeed because all the letters go to the consonant variable. Someone can take a look at the code and give me a light...

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

int main(){
  setlocale(LC_ALL,"Portuguese");
  char torto[6][3]; //TORTO[LINHA][COLUNAS]
  int c, l, vogalcont=0, consoantecont=0;
  char vogal[8], consoante[10];
  int x = 0;

  printf("\nInsira aqui as letras para a composicao da matriz do jogo([LINHA][COLUNA]).....\n");
  for (l = 0; l < 6; l++){
    for (c = 0; c < 3; ++c){
      printf("[%d][%d]: ", l,c);scanf("%s", &torto[l][c]);
    }
  }
  //VALIDAÇÃO DA MATRIZ
  for (l = 0; l < 6; l++){
    for (c = 0; c < 3; ++c){
      if((torto[l][c] == "A") || (torto[l][c] == "E") || (torto[l][c] == "I") || (torto[l][c] == "O") || (torto[l][c] == "U")){
        vogal[vogalcont] = torto[l][c];
        vogalcont++;
      }
      else{
        consoante[consoantecont] = torto[l][c];
        consoantecont++;
      }
    }
  }
  return 0;
}

2 answers

1

You stated torto as

char torto[6][3];

See what kind of torto is char. Then the mistake starts.

To compare strings, use the function strcmp(), library string.h:

int strcmp ( const char * str1, const char * str2 );

Compare two strings

Compares the C string str1 to the C string str2.

This Function Starts Comparing the first Character of each string. If they are Equal to each other, it continues with the following pairs until the characters differ or until a terminating null-Character is reached.

Return value indicates
< 0 the first Character that does not match has a Lower value in ptr1 than in ptr2
0 the Contents of Both strings are Equal
> 0 the first Character that does not match has a Greater value in ptr1 than in ptr2

Source: here

Comparing strings with equal sign is the same as comparing memory addresses. Since no string has the same address as another (unless it is exactly two strings from the same memory position), every comparison evaluates as 0 and the block if corresponding to comparisons is not performed:

#include <stdio.h>
#include <string.h>

int main()
{
    char* stringum = "minha terra tem palmeiras onde canta o sabia";
    char stringdois[46];
    FILE *f;
    if ((f = fopen("teste.txt", "w")) == NULL)
    {
        printf("Erro ao criar arquivo.\n");
        return -1;
    };
    fprintf(f, "minha terra tem palmeiras onde canta o sabia");
    fflush(f);
    fclose(f);

    if ((f = fopen("teste.txt", "r")) == NULL)
    {
        printf("Erro ao criar arquivo.\n");
        return -1;
    };
    fgets(stringdois, 46, f);
    fclose(f);

    printf("stringum: %s\n", stringum);
    printf("stringdois: %s\n", stringdois);
    printf("stringum == stringdois avalia como %d\n", stringum == stringdois);
    printf("strcmp(stringum, stringdois) avalia como %d\n", strcmp(stringum, stringdois));

    return 0;
}

0

Why do you define vowel and consonant arrays with 8 and 10 positions respectively? If they are all vowels, shouldn’t they be vowel[18]? Idem for if they are not, then according to [18]?

If you want to work character by character then use the format %c in the scanf and in the comparison use ' and not ".

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

int main(){
  setlocale(LC_ALL,"Portuguese");
  char torto[6][3]; //TORTO[LINHA][COLUNAS]
  int c, l, vogalcont=0, consoantecont=0;
  char vogal[8], consoante[10];
  int x = 0;

  printf("\nInsira aqui as letras para a composicao da matriz do jogo([LINHA][COLUNA]).....\n");
  for (l = 0; l < 6; l++){
    for (c = 0; c < 3; ++c){
      printf("[%d][%d]: ", l,c);scanf("%c", &torto[l][c]);
    }
  }
  //VALIDAÇÃO DA MATRIZ
  for (l = 0; l < 6; l++){
    for (c = 0; c < 3; ++c){
      if((torto[l][c] == 'A') || (torto[l][c] == 'E') || (torto[l][c] == 'I') || (torto[l][c] == 'O') || (torto[l][c] == 'U')){
        vogal[vogalcont] = torto[l][c];
        vogalcont++;
      }
      else{
        consoante[consoantecont] = torto[l][c];
        consoantecont++;
      }
    }
  }
  return 0;
}

Register that this is a "read and forget" program. You don’t even print the amounts of typefaces found.

Browser other questions tagged

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