Can i write a pragram such that for any name given input by keyboard output will be in such that it indiacate the no of alphabet | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can i write a pragram such that for any name given input by keyboard output will be in such that it indiacate the no of alphabet

for ex-if input is 'pranav' then it's out put should be '15,17,1,13,1,21'

5th Sep 2017, 2:01 AM
PRANAV VATSA
PRANAV VATSA - avatar
13 Answers
+ 3
yes you can. if i was at home i could have create it for you so i just tell you how would i do it. 1. create 2 arrays and a variable. char alphabet = 'a','b', etc... int * alphabet_numbers int counter = 1 2. then you get user input. 3. splits string to chars [ which you dont need because string is bunch of chars ] 4. create dynamic alphabet_variable with the size of your input ( i think input.size() ). 5. use a - z loop. 6. check in the loop if char equal to one of a - z in the loop, if not counter+1. if yes store counter into alphabet_numbers and set counter for 1. 7. output numbers :) EDIT: youll need to use another for loop for the array index. ( i forgot that sry )
5th Sep 2017, 4:23 AM
Cain Eviatar
Cain Eviatar - avatar
+ 2
i have made it for you. ( using Jared Bird Method ) https://code.sololearn.com/cIc6obw8nbXq/?ref=app
5th Sep 2017, 5:21 PM
Cain Eviatar
Cain Eviatar - avatar
+ 2
@Cain, Good work your code works well. 😀 One more related tip: You used the correct decimals for the ASCII codes for the letters​. You can also use the hex values, ie 65 == 0x41. More importantly​ you can use the character. So instead of: output = output - 65; you can use: output = output - 'A'; The code will work the same. The reason I suggest this is I think it makes the code more readable. 😃
6th Sep 2017, 9:55 AM
Jared Bird
Jared Bird - avatar
+ 1
There is a quicker way: For each character subtract the value of 'a' then add 1 //you want a to became 1, not 0 Then output. // you may have to use a cast, unless data is in a char array or string you probably want think about what happens if character is not a letter or is upper case.
5th Sep 2017, 10:59 AM
Jared Bird
Jared Bird - avatar
+ 1
i didnt think about that 😥
5th Sep 2017, 11:08 AM
Cain Eviatar
Cain Eviatar - avatar
+ 1
That's why we share ideas 😀 The number of times I have finished or got halfway through writing code for something, then realised that there was a better way. 😩 Too many!
5th Sep 2017, 11:16 AM
Jared Bird
Jared Bird - avatar
+ 1
hahahha i never tried to convert char to int so i didnt think about that :( but i have used a - z loop so...
5th Sep 2017, 11:17 AM
Cain Eviatar
Cain Eviatar - avatar
+ 1
ohhhh my bad. its converting from character to ascii. ill fix that.
5th Sep 2017, 6:11 PM
Cain Eviatar
Cain Eviatar - avatar
+ 1
fixed.
5th Sep 2017, 6:25 PM
Cain Eviatar
Cain Eviatar - avatar
+ 1
thanks for your tips.
6th Sep 2017, 10:23 AM
Cain Eviatar
Cain Eviatar - avatar
0
thankou so much cain eviatar and jared bird
5th Sep 2017, 6:08 PM
PRANAV VATSA
PRANAV VATSA - avatar
0
but it's giving output wrong as more than 26 which is not posible according to my question
5th Sep 2017, 6:11 PM
PRANAV VATSA
PRANAV VATSA - avatar
0
sure
5th Sep 2017, 6:12 PM
PRANAV VATSA
PRANAV VATSA - avatar