字符串hash

#include<stdio.h>

//字符串hasha
unsigned int hasha(char * url, int mod){
    unsigned int n = 0;
    char * b = (char*) &n;
    for(int i = 0; url[i]; i++)
        b[i % 4] ^= url[i];
    
    return n % mod;
}
int main(){ char str[] = "Hello World\n"; //char *str = "Hello World\n";是错误的 //原因:hello world 应该被分配到了常量区中,而声明的str指针在栈里 //而char str[] = "Hello World\n"; 字符串在栈中。 printf("%d", hasha(str, 25013)); }
原文地址:https://www.cnblogs.com/yujinghui/p/2876518.html