Switch statement using string on an array

前端 未结 8 1514
礼貌的吻别
礼貌的吻别 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: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 
    #include 
    #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;
    }
    

提交回复
热议问题