How to separate a vowel of consonants into a matrix in C

0

Hello, I have a problem with my code. I have an array [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. Can someone 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;
}
    
asked by anonymous 23.06.2018 / 17:13

1 answer

0

You have declared torto as

char torto[6][3];

Notice that the type of torto is char . Then the error begins.

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

  

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 they are exactly two strings of the same memory location), every comparison is evaluated as 0 and the if block corresponding to the comparisons is not executed:

#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;
}
    
23.06.2018 / 17:32