Sign in
Log inSign up

Can anyone help me with the following C++ code?

Sayture Storver's photo
Sayture Storver
·Apr 17, 2017

I need to make a program which translates words from character to numbers, say like a=1, b=2, c=3, etc...

It is working fine but when I type something like "zain", it will output 1 9 14 26 which is the expected output for "ainz"

The expected, correct output should be 26 1 9 14 (without spaces).

Can anyone please correct the following code to get the expected output?

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
 class c1{
 public:
 int i;
    char arr1[10];
    int  f2(char b1 , int i2){


for(i=0;i<11;i++){
        if(arr1[i]==b1){
        cout<<i2;
}
        }
    return 0;
    }
    void nl(){
        cin>>arr1;
f2('a',1);
f2('b',2);
f2('c',3);
f2('d',4);
f2('e',5);
f2('f',6);
f2('g',7);
f2('h',8);
f2('i',9);
f2('j',10);
f2('k',11);
f2('l',12);
f2('m',13);
f2('n',14);
f2('o',15);
f2('p',16);
f2('q',17);
f2('r',18);
f2('s',19);
f2('t',20);
f2('u',21);
f2('v',22);
f2('w',23);
f2('x',24);
f2('y',25);
f2('z',26);
    }
 };
int main (){

cout<<"hello"<<endl;
c1 c1;
c1.nl();


    return 0;
}
}
Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct