Switch statement using string on an array

前端 未结 8 1418
礼貌的吻别
礼貌的吻别 2021-01-13 06:28
#include

int main(){

    char name[20];

    printf(\"enter a name \");
    scanf(\"%s\",name);
    switch(name[20]){
        case \"kevin\" : 
             


        
相关标签:
8条回答
  • 2021-01-13 07:05

    Remember the rules while using switch statements.

    Switch constraints

    1. The controlling expression of a switch statement must have "integer type".

    2. The expression of each case label shall be an integer constant expression and no two of the case constant expressions in the same switch statement shall have the same value after conversion. There may be at most one default label in a switch statement.

    3. Any enclosed switch statement may have a default label or case constant expressions with values that duplicate case constant expressions in the enclosing switch statement.

    0 讨论(0)
  • 2021-01-13 07:07

    You can use "hash-string.h" library that converts strings into hash code integer. Create a header file and paste this code: http://www.opensource.apple.com/source/gcc/gcc-5484/intl/hash-string.h

    #include <stdio.h>
    #include <stdlib.h>
    #include "hash-string.h"
    
    int main(){
    
      char name[20];
    
      printf("Enter a name: ");
      scanf("%s",name);
    
      unsigned long nameInt = hash_string(name);
    
      switch(nameInt){
        case 7458046 /* "kevin" */: { printf("Hello %s", name); break; }
        default: { printf("You are not kevin"); }
      }
    
      printf("\n");
      return 0;
    }
    
    0 讨论(0)
  • 2021-01-13 07:11

    Switch statements work on int values (or enum), but not on char arrays.

    You could do

    if (strcmp(name, "kevin")==0) {
        printf("hello");
    }
    else if (strcmp(name, "Laura")==0) {
        printf("Allo");
    }
    else if (strcmp(name, "Mike")==0) {
        printf("Good day");
    }
    else  {
        printf("Help!");
    }
    
    0 讨论(0)
  • 2021-01-13 07:17

    since the name is declared as a char type ,it would be better if you use "%c" instead of using "%s" inside the scanf() method.

    0 讨论(0)
  • There are plenty of ways to go about this! For example, use a...

    3-letter hash

    #include <stdio.h>
    
    int main(){
    
        char name[20];
    
        printf("enter a name ");
        scanf("%s",name);
        switch((int)*name * (int)*(name+1) * (int)*(name+2)){
              case (1275226) : // "kevin"
                printf("hello %s.\n", name);
                break;
              case (1293980) : // "astro"
                printf("welcome %s.\n", name);
                break;
        }
        printf("%d",(int)*name * (int)*(name+1) * (int)*(name+2));
    }
    
    0 讨论(0)
  • 2021-01-13 07:21

    No, you cannot use the switch statement in C with the value of a string or character array. The closest alternative is to use some sort of data structure mapping strings to function pointers. The function pointer could be called after a string is used to look it up.

    0 讨论(0)
提交回复
热议问题