How a character string and number string be added?

Input :absde// character string 12345// number string Output : bdvhj Help me to understand this

8/18/2019 3:21:53 PM

Noor ❤

12 Answers

New Answer


Noor ❤ cs = "absde" + ns = "12345" cs[0] = 'a' ns[0] = '1' (note char 1 not number 1) ns[0] to number 1 => ns[0] - '0' or (48, ascii value of char '0') => 1 (number 1) 'a' + 1 = 97(ascii value of char 'a') +1 = 'b' do this for each element of the both strings.


I think the easiest way to do this is to convert the character to its asci representarion. For example: int i = 'a' will be 97 (google 'ascii table' to find the numbering). Than you can add the integer value to the ascii value und convert it back to a character.




Tom Hammerbacher I know this How can I add a number string and character string,?? Bcoz number string is a string not integer??


This will do what you want;- #include <iostream> #include <string> using namespace std; int main() { string newstring = ""; string cs = "absde"; string ns = "12345"; for(unsigned int i = 0; i < cs.length() && i < ns.length(); i++){ newstring += cs[i] + stoi(ns.substr(i,1), nullptr); } cout << newstring; return 0; }


That number string can take as a character string.than we can directly concat those two strings(that will convert into asci values and add).


Kewal Sharma why is Python? Question was about C or C++


UraL That will be easier https://code.sololearn.com/cLlUB7keVprI/?ref=app


Seems like you add every character. a+1=b, b+2=d, ... So you count further in the alphabet :)


It's better to convert the character string into number string and then add both of them...!


@You did this in decrypt prog? Not so?