-1

Very new to "backend" and trying out this reddit challenge that I saw at r/dailyprogrammer

Challenge: Assign every lowercase letter a value, from 1 for a to 26 for z. Given a string of lowercase letters, find the sum of the values of the letters in the string.

I specifically want to do it like this but is it possible? How can a user input a word that would then add the int I've listed here so that the total sum of the word would show.

int main()
{
    int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z;
        a=1;
        b=2;
        c=3;
        d=4;
        e=5;
        f=6;
        g=7;
        h=8;
        i=9;
        j=10;
        k=11;
        l=12;
        m=13;
        n=14;
        o=15;
        p=16;
        q=17;
        r=18;
        s=19;
        t=20;
        u=21;
        v=22;
        w=23;
        x=24;
        y=25;
        z=26;
ramesays
  • 1
  • 2
  • Seems to be that the task is to assign some data with labels which can be searched later for the given search string. That is totally different from what you are doing. Even if the task is not understood, you should start with a beginner book for c++. – Klaus Dec 10 '21 at 09:07
  • Check it out: [https://en.cppreference.com/w/cpp/io/cin] – yemo Dec 10 '21 at 09:07
  • You are confusing variable names with characters. The character `'a'` has no relationship to a varaible named `a`. What you need to do is: 1) Learn how to read a line of user input 2.) Write function that takes a single `char` and returns the respecitve number 1-26 and 3.) iterate over the user input, call the function from (2) for each character and add up the results. – Lukas-T Dec 10 '21 at 09:27
  • I think you have rather fundamental misunderstandings about how C++ works. The variable named "a" is has type "int" with value "1". But you need to look for a **character** value (`char`), and those are entirely unrelated to variable names. – MSalters Dec 10 '21 at 09:28
  • alright it seems my understanding is far from what I expected, thank you for that. And does this mean that what I'm trying to do here is wrong from the start? – ramesays Dec 10 '21 at 10:29

4 Answers4

3

This is pretty much one function call in C++

std::string example = "example";
std::cout << std::accumulate(example.begin(), example.end(), 0, 
  [](int sum, char c) { return sum + (c - 'a' + 1); }
  );

std::accumulate adds things in a collection. Usually, it just uses '+' but in this case I use a custom adder [](int sum, char c) { return sum + (c - 'a' + 1); }. This adds not the ASCII value of c, but (c - 'a' + 1). If c=='a', then c-'a'==0 and (c - 'a' + 1)==1

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Hi! First of all thanks for the help, I have never heard of the std::accumulate and other related functions, I've been using w3schools website as my main reference do you have any recommendations for a beginner like me? – ramesays Dec 10 '21 at 10:32
  • @ramesays: You really should get a good book. While cppreference.com is a good _reference_ site, there's unfortunately not yet a good learning site. – MSalters Dec 10 '21 at 11:21
2

ok , so , this is my code and it's working ... explanation -- > we know storing a charachter in a int variable will store the ASCII value , the ASCII value of a is 97 , b is 98 so on .... subtracting 96 from each letter's ascii value will give the number you want --> if you are still confused about the ascii table then go look it up at the google you'll understand

#include <iostream>
#include <string>
using namespace std;
int main(){
    string str ;
    cin >> str;
    int value = 0;
    for(int i=0 ; i<str.length() ; i++){
        int v = str[i] - 96;
        value += v;
    }
    cout << value;
    return 0;
}
  • 1
    That "96" is hard to understand. You can just write `str[i] - 'a'` instead. That character constant has the right value. – MSalters Dec 10 '21 at 09:24
  • @MSalters yeah , but then the numbering will start from 0 not from 1 , the question he asked has numbering from 1 – Namrata Mundhra Dec 10 '21 at 09:29
  • 4
    `str[i] - 'a' + 1`, still not that hard. – MSalters Dec 10 '21 at 09:30
  • @MSalters yeah , you can do that also , but i don't think he will be able to understand this ... – Namrata Mundhra Dec 10 '21 at 09:36
  • 4
    @NamrataMundhra IMO `str[i] - 96` is even more confusing. `str[i] - 'a' + 1` avoids magic number. – justANewb stands with Ukraine Dec 10 '21 at 09:36
  • @justANewbie yeah , but `a + 1` IS 96 right ?? I am saying he might get confused on how are we subtracting a charachter ! this problem can only be solved if you have the knowledge of the ASCII table hence it's better if we directly subtract 96 rather than subtracting a `char` and then adding 1 to it – Namrata Mundhra Dec 10 '21 at 09:46
  • If some part of the code might be confusing, this is exactly when a comment is due. Also, another variant is to write a helper function which is self-documenting, like `int alphabetic_value_from_ascii(char input)` (am not entirely happy about that name, but you get the idea). – Aziuth Dec 10 '21 at 09:52
  • @NamrataMundhra That's what a comment is for. While it is true that `'a'+1` is 96, I won't know what `str[i] - 96` will do (why is 96 used here? Why not 95? 97?), but I know `str[i] - 'a' + 1` will do. The concept of ASCII is not that complicated, I have answer a similar question, see [here](https://stackoverflow.com/questions/69821744/assigning-a-char-value-to-an-int/69821964#69821964) – justANewb stands with Ukraine Dec 10 '21 at 10:07
  • @justANewbie fine , i give up , you're the boss – Namrata Mundhra Dec 10 '21 at 13:12
  • @NamrataMundhra No, I'm not doing it because I want to win, I do it because it's considered best practice, and it will help the OP and you in the long run. – justANewb stands with Ukraine Dec 10 '21 at 13:14
1

make use of the std::map<char,int>

int main(void)
{
   std::map<std::string,int> list;
   char letter = 'a';
   int val = 1;
   while(val <= 26)
   {
      list.insert(std::make_pair(letter,val));
      letter++;
      val++;
   }
}
0

You're confusing the concept of strings and characters with a variable's name.

// a is the variable name, this is information for the programmer
int a = 1;

// 'a' here is the actual letter a, things can actually be done with this in the program
char theLetterA = 'a';

Combine this concept with std::cin so that you can read data from the user and you are on your way to figuring this out.

See:

Corvus
  • 53
  • 9