Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7630991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:14:20+00:00 2026-05-31T06:14:20+00:00

How can I set each character in a string to an integer? This is

  • 0

How can I set each character in a string to an integer? This is just the first thing I have to do in order to write a hash function. I have to set each character in a string to an integer so that I can sum their values. Please help! It it something like this??

    int hashCode(string s)
{
   int Sum = 0;
   for(int i=0; i<strlen(s); i++)
   {
      Sum += (int)s[i];
   }
   return Sum;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:14:22+00:00Added an answer on May 31, 2026 at 6:14 am

    Yes — in C and C++, char is just a small integer type (typically with a range from -128 to +127). When you do math on it, it’ll normally be converted to int automatically, so you don’t even need your cast.

    As an aside, you really don’t want to use strlen(s) inside the stopping condition for your for-loop. At least with most compilers, this will force it to re-evaluated strlen(s) every iteration, so your linear algorithm just became quadratic instead.

    size_t len = strlen(s);
    
    for (int i=0; i<len; i++)
        Sum += s[i];
    

    Or, if s is actually a std::string, as the parameter type suggests:

    for (int i=0; i<s.size(); i++)
        Sum += s[i];
    

    As yet one more possibility:

    Sum = std::accumulate(s.begin(), s.end(), 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: How to replace all character with Regex I
My Rails application makes use of Time.zone so that each user can set their
I can set data in JTable constructor, and then user can change this data
I have a JSON character string that I put into a data frame. I
I have a datatype that's more or less a character array. Each space in
I have this class used in MVC3... The Validation Attributes for each property are
How can we find the most efficient hash function(least possible chances of collision) for
I have a set of files I am trying to import into MySQL. Each
I have a class that looks like this: public class Person { public string
I have a static function defined in a class called Character as: + (UIImage)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.