Strings: Given a string convert into its equivalent ASCII form

Problem Statement:

Given a string convert into its equivalent ASCII format.

Example

Input : hello, world!
Output : 104101108108111443211911111410810033

Solution

The solution is very simple.

Iterate over the string and take each character and subtract NULL character.

Print the result.

Solution in C++

#include <vector>    
#include <algorithm>  
//visit www.ProDeveloperTutorial.com for 450+ solved questions  
#include <iostream>    
#include <string>
#include <stack>

using namespace std;


void convert_into_ASCII(string str) 
{ 
    int l = str.length(); 
    int convert; 
    for (int i = 0; i < l; i++) 
    { 
        convert = str[i] - NULL; 
        cout << convert; 
    } 
} 
  
int main() 
{ 
    string str = "A"; 
    cout << "ASCII Sentence:" << std::endl; 
    convert_into_ASCII(str); 
    return 0; 
} 

Output:

65

 

 

 

 

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *