d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Hashcode Of A String C
Add Reply New Topic New Poll
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Oct 7 2012 03:55pm
So I'm trying to return the hashcode value of a string using an algorithm:



My question is, how do I break up the string into individual chars so I can.

This is what I did:

Code
int sum = 0;
  for(int i = 0; i < strlen(String); i++)
  {
     sum += (int)String[i];
  }
  return sum;


Is this correct?

This post was edited by lopelurag on Oct 7 2012 03:55pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 7 2012 04:12pm
if you're referring to how you treat string like an array and iterate over it, then yes. if you're referring to taking the sum, then no. i dont know why you're asking this since you can easily print it out and confirm if it's the ascii value. iirc it's unicode, but whatever.

/edit why did you capitalize String? unless you have a good reason, variables should be lower case

This post was edited by carteblanche on Oct 7 2012 04:22pm
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Oct 7 2012 04:19pm
You don't need to cast it to int, it will get promoted anyway.
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Oct 8 2012 08:22am
Quote (carteblanche @ Oct 7 2012 06:12pm)
if you're referring to how you treat string like an array and iterate over it, then yes. if you're referring to taking the sum, then no. i dont know why you're asking this since you can easily print it out and confirm if it's the ascii value. iirc it's unicode, but whatever.

/edit why did you capitalize String? unless you have a good reason, variables should be lower case


Yeah it was a typo, and completely forgot strings are considered an array of characters lol...

Fixed it anyways
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Oct 8 2012 11:16am
Just a quick question


Say you wanted to either get user input from standard input, or from a file.

How would you determine which one they want to use?
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Oct 8 2012 11:40am
usually via command line flags
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll