In: Computer Science
Hi i need a c++ program that can convert charactor into numbers
pseodocode
user input their name
for example john
every single charactor should have a value so it return correct result
eg:
j=2, o=1, h=7,n=2
and display these numbers
if you may need any question to ask me, i will be very happy to answer them.
Thanks!
example project close but not accurate
#include
#include
#include
#include
#include
#include
#include
#include
#define INPUT_SIZE 8
using namespace std;
// Function to reverse a string
void reverseStr(string& str)
{
int n = str.length();
// Swap character starting from two
// corners
for (int i = 0; i < n / 2; i++)
swap(str[i], str[n - i - 1]);
}
int main() {
const std::string alpha =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string text;
std::cin >> text;
// convert all lower case characters to upper
case
for (char& c : text) c = std::toupper(c);
// sum up the values of alpha characters ('A' == 1,
'B' == 2 etc.)
int sum = 0;
for (char& c : text) // for each character c in
text
{
// locate the character in
alpha
//
http://www.cplusplus.com/reference/string/string/find/
const auto pos = alpha.find(c);
if (pos != std::string::npos) //
if found (if the character is an alpha character)
// note:
non-alpha characters are ignored
{
const int value
= pos + 1; // +1 because position of 'A' is 0, value of 'A' is
1
sum +=
value;
cout <<
sum;
}
}
more info is here:
https://www.chegg.com/homework-help/questions-and-answers/hi-need-c-program-convert-charactor-numbers-pseodocode-user-input-name-example-john-every--q40281532?trackid=qn_1uBwb
I JUST WANT TO REPLACE THE FINAL VALUE GIVEN FOR EACH CHAR WITH ANOTHER NUMBER FOR EXAMPLE IF THEY ENTER JOHN
IT SHOULD PRINT VALUE OF JOHN WHICH I SPECIFY FOR THAT NOT THE RANDOM VALUE OR POSITION VALUE AS RESULT OF THIS PROGRAM THANK YOU IN ADVANCE
Please do let me know if anything is required.
Below code to print the number representation of each character in the string and sum of all characters represented as numbers in the string.
Code :
#include<iostream>
#include<string>
#include<exception>
#include <cstdlib>
#include<stdio.h>
#include<map>
#include <cctype>
#define INPUT_SIZE 8
using namespace std;
int main() {
const std::string alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string text;
//Taking the string from the user
cout<<"Enter the string : ";
std::cin >> text;
// convert all lower case characters to upper case
for (char& c : text) c = std::toupper(c);
// sum up the values of alpha characters ('A' == 1, 'B' == 2
etc.)
int sum = 0;
cout<<"\nThe number representation of each character in the
string is\n****** ";
for (char& c : text) // for each character c in text
{
// locate the character in alpha
// http://www.cplusplus.com/reference/string/string/find/
const auto pos = alpha.find(c);
if (pos != std::string::npos) // if found (if the character is
an alpha character)
// note: non-alpha characters are ignored
{
const int value = pos + 1; // +1 because position of 'A' is 0,
value of 'A' is 1
sum += value; //summing the number of values of each chanracter in
to the variable sum
//printing the number value of each character in the string
cout << value;
}
}
//printing the total of all characters represented as numbers in
the string
cout<<" ******\n\nThe Total sum of all characters represented
as numbers in the string is \n****** "<<sum<<"
******\n";
}
Test 1 :
Test 2 :