0

Reduce on char is not working

Why Reduce is not working for char type? https://www.sololearn.com/compiler-playground/crTD3J1hlGC0

25th Oct 2022, 6:48 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
4 Respuestas
+ 2
It appears to be working fine for me. It just works differently to other types used in the code; it works by accumulating each characters' ASCII value instead of concatenating the characters such as to form a string.
25th Oct 2022, 8:37 AM
Ipang
+ 1
That is also not correct as per my understanding... I am getting output as 's' for vector char array of K,E,T,A and N I was expecting 0 as output for below line but that's not 0 cout << int('K') + int('E') + int('T') + int('A') + int('N') - int('s') << endl; is this analysis correct or Am i missing something else?
25th Oct 2022, 11:21 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Yes Ketan, that was correct. Accumulate ASCII value for each character 'K' , 'E', 'T', 'A', and 'N' then the result is assumed as letter 's' because the result overflow `char` type data range.
25th Oct 2022, 11:25 AM
Ipang
+ 1
Thanks Ipang It works the way you are telling vector<char> v = {'A',' '}; Space being 32 and A being 65 does give a = 65 +32 Thanks again for the insight
25th Oct 2022, 2:12 PM
Ketan Lalcheta
Ketan Lalcheta - avatar