How to receive a string and by the switch to check in C?

5

I have to develop an algorithm that gets the name of a place, for example "School", and based on this, make a check of the string in switch , and if it is "School ", then he sends a message to the user about what to do, for example," Studying ".

Code:

 int main() {

    setlocale(LC_ALL,"portuguese");

    char lugar;

    printf("Digite o nome de um lugar : ");
    scanf("%c",&lugar);

    switch(lugar) {

    case 'Escola' :
    printf("Estudar");
    break;  


    }


 }

Error:

  

[Warning] character constant too long for its type

     

[Error] switch quantity not an integer

From what I've seen, in C the concept of string is very different, and ends up being more like an array than string >, the concept being vague

    
asked by anonymous 28.03.2017 / 00:07

2 answers

7

The code has several problems. You need to store the string , need to read in the correct format, need to use if and strcmp() to compare strings .

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

int main(void) {
    char lugar[20]; //cria o array de caracteres para armazenar o texto
    printf("Digite o nome de um lugar: ");
    scanf("%20s", lugar); //não precisa da referência porque o array já é uma, precisa %s
    if (strcmp(lugar, "Escola") == 0) { //use a função para comparar todos os caracteres
        printf("\nEstudar");
    }
}

See running on ideone . And No Coding Ground . Also put it on GitHub for future reference .

    
28.03.2017 / 00:36
2

In C, you can only by scalar constants in switch . A string is a pointer variable, so we will have difficulties using this method exactly.

In Stackoverflow in English, who replied suggested that a hash function is used to be able to use switch with the string; or use the so-called "ladder" if-else .

Wikipedia has a entry explaining hash functions . A function of hash for a generic string can be defined like this (I will not null-safe ):

int hash_function(char *str) {
    int acc = 0;
    int peso = 1;
    int base = 256;
    int len = strlen(str);
    int modulus = 65000;

    int i;
    for (i = 0; i < len; i++) {
        int char_at = str[i]; /* char é um número de 8 bits, enquanto que int é um número de 16 ou 32 bits, dependendo do compilador */
        acc = (acc*base + peso*char_at) % modulus;
        peso = (peso % 10) + 1; /* peso varia de 1 a 10 */
    }

    return acc;
}

Having the function of hash pre-determined, we already know what the value of "School" can be and creating the HASH_ESCOLA macro:

int tratar_escola(char *lugar) {
    int hash_lugar = hash_function(lugar);
    switch (hash_lugar) {
    case HASH_ESCOLA:
        /* vamos checar se deu positivo verdadeiro? */
        if (strcmp(lugar, "Escola") == 0) {
            printf("Estudar\n");
            return 1;
        }
    }
    return 0;
}

An algorithm of hash / scattering algorithm can generate collisions ( Wikipedia article ), which in this case is another object that returns the same value as its desired object. To avoid colliding with a value other than Escola , I make the comparison with the original string.

    
28.03.2017 / 01:13