replace letters in a string

i would like to replace the letters in a entered string with the provided binary code given in the header file. such as if i input "hi how is it going" it outputs "100 1000100 1001 100 1000100 1111101 0111 100 1001101 0011 100 1001101 0100 100 0111100 1111100 1001100 1101101 0001"

Header.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <cctype>
#include <stdio.h>
#include <windows.h>

//A=100 0001
//B=100 0010
//C=100 0011
//D=100 0100
//E=100 0101
//F=100 0110
//G=100 0111
//H=100 1000
//I=100 1001
//J=100 1010
//K=100 1011
//L=100 1100
//M=100 1101
//N=100 1110
//O=100 1111
//P=101 0000
//Q=101 0001
//R=101 1010
//S=101 0011
//T=101 0100
//U=101 0101
//V=101 0110
//W=101 0111
//X=101 1000
//Y=101 1001
//Z=101 1010  


Source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "Header.h"

using namespace std;

int main()
{
	string input;

	cout << "Enter statement you would like to convert to binary: ";
	getline(cin >> ws, input);
	
}

Double Post?

http://www.cplusplus.com/forum/beginner/132441/

I answered you post in the above thread.
The output you show as generated for "hi how is it going" is wrong.

Here's one way to do such a thing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <string>
#include <cctype>
#include <iostream>
#include <unordered_map>

std::unordered_map<char, std::string> substitute_for =
{
    { 'A', "100 0001" }, { 'B', "100 0010" }, { 'C', "100 0011" }, { 'D', "100 0100" },
    { 'E', "100 0101" }, { 'F', "100 0110" }, { 'G', "100 0111" }, { 'H', "100 1000" },
    { 'I', "100 1001" }, { 'J', "100 1010" }, { 'K', "100 1011" }, { 'L', "100 1100" },
    { 'M', "100 1101" }, { 'N', "100 1110" }, { 'O', "100 1111" }, { 'P', "101 0000" },
    { 'Q', "101 0001" }, { 'R', "101 0010" }, { 'S', "101 0011" }, { 'T', "101 0100" },
    { 'U', "101 0101" }, { 'V', "101 0110" }, { 'W', "101 0111" }, { 'X', "101 1000" },
    { 'Y', "101 1001" }, { 'Z', "101 1010" }
};

std::string convert(const std::string& text)
{
    std::string result;

    for (auto ch : text)
    {
        if (std::isalpha(ch)) result += substitute_for[std::toupper(ch)];
        else result += ch;
    }

    return result;
}

int main()
{
    std::string text;

    std::cout << "Enter statement you would like to convert to binary: ";
    std::getline(std::cin, text);

    std::cout << convert(text) << '\n';
}


http://ideone.com/Pt1v6K
closed account (j3Rz8vqX)
A primitive alternative:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;
string conversion(string input)
{
    string table[26]={  "100 0001","100 0010","100 0011","100 0100","100 0101",
                        "100 0110","100 0111","100 1000","100 1001","100 1010",
                        "100 1011","100 1100","100 1101","100 1110","100 1111",
                        "101 0000","101 0001","101 1010","101 0011","101 0100",
                        "101 0101","101 0110","101 0111","101 1000","101 1001",
                        "101 1010"};

    //Convert characters to binary:
    string temp;                                //Create string variable
    for(int i=0;i<input.length();i++)           //Loop while i < than the number characters in string
    {
        if(input[i]>='a'&&input[i]<='z')
            input[i]=(input[i]-'a')+'A';        //If character is an alpha-lower, convert to alpha-upper.
        if(input[i]>='A'&&input[i]<='Z')
            temp+=table[input[i]-'A'];          //If character is valid, cat interpreted bin to temp string.
        else
            temp+=input[i];                     //If character not alpha, cat character to temp string.
    }
    return temp;                                //Return value of temp.
    //End conversion
}
int main()
{
    string input;

    cout << "Enter statement you would like to convert to binary: ";
    getline(cin >> ws, input);

    input = conversion(input);
    cout<<input<<endl;

    return 0;
}


As mentioned, your provided
"100 1000100 1001 100 1000100 1111101 0111 100 1001101 0011 100 1001101 0100 100 0111100 1111100 1001100 1101101 0001"
is wrong, the last character should be represented as: G=="100 0111".
Last edited on
Topic archived. No new replies allowed.