字符串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)); }

 

posted on 2013-01-25 13:42  于敬晖  阅读(157)  评论(0编辑  收藏  举报

导航