Click here to Skip to main content
15,916,288 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm trying to create one program, but no way to make it. Two lines in the first line of code are strings and three numbers in the second line of code are entered into the program. Like this:
word + other_word = other_other_word
1254 + 5471 = 81755
If I can replace each number with each letter so that there are no letters corresponding to two numbers, the program should print yes, and if that is impossible they should print no.
Can you help me do this in c++?

What I have tried:

I tried to make this program but I can't.
Posted
Updated 12-Jan-20 6:40am
v3
Comments
Patrice T 12-Jan-20 8:58am    
Add the letters result to your example.
and show your work and explain the problem.

Yes, we can help, but we are not going to do it all for you. You first need to show what you have done so far and explain exactly where you are stuck. As it stands there is not enough information in your question to know what criteria you use to replace a number with a letter.
 
Share this answer
 
Comments
[no name] 12-Jan-20 10:18am    
I use vector vector<int, char="">. But I do not know how to compare vectors. For example how to find is there numbers that corresponds to two different letters, or is there letter that corresponds to two different numbers.
Richard MacCutchan 12-Jan-20 10:26am    
Sorry, I am not sure what you mean. If you know which character represents which digit, then a simple array will suffice. For example if the digits 1 to 9 are represented by ABCDEFGHI, then something like the following will do it:

char digtoltr[] = "0ABCDEFGHI";
char number = getc(); // get the next digit as a character
int index = number - '0'; // make it an integer value
char letter = digtoltr[index]; // get the letter that corresponds to this number
[no name] 12-Jan-20 10:57am    
I managed to do this but I do not know how to continue, please help:
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

int main()
{
string first, second, third, some_string;
string first_number, second_number, third_number;
getline(cin, first, ' ');
getline(cin, some_string, ' ');
getline(cin, second, ' ');
getline(cin, some_string, ' ');
getline(cin, third);

getline(cin, first_number, ' ');
getline(cin, some_string, ' ');
getline(cin, second_number, ' ');
getline(cin, some_string, ' ');
getline(cin, third_number);

int number1 =atoi(first_number.c_str());
int number2 = atoi(second_number.c_str());
int number3 = atoi(third_number.c_str());
return 0;
}
[no name] 12-Jan-20 10:30am    
Thanks, but I do not know how to correspond letter to number. If I have string STOP, and int 1234; how should I correspond letter S to number 1, and all others?
[no name] 12-Jan-20 10:32am    
I managed to do this, but I do not know how to continue, where to start:
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

int main()
{
string first, second, third, some_string;
string first_number, second_number, third_number;
getline(cin, first, ' ');
getline(cin, some_string, ' ');
getline(cin, second, ' ');
getline(cin, some_string, ' ');
getline(cin, third);

getline(cin, first_number, ' ');
getline(cin, some_string, ' ');
getline(cin, second_number, ' ');
getline(cin, some_string, ' ');
getline(cin, third_number);

int number1 =atoi(first_number.c_str());
int number2 = atoi(second_number.c_str());
int number3 = atoi(third_number.c_str());
return 0;
}
This code will build you a translate table to match the letters and numbers. You can then use the number (converted by: int i = number - '0' - 1) to get the correct letter for future input strings.
C++
std::string letters = "bludsnake";
std::string numbers = "956324718";
std::string table = "";
table.append(letters.length(), ' ');

for (int i = 0; i < letters.length(); ++i)
{
    char c = letters[i];
    int index = numbers[i] - '0';
    table[index - 1] = c;
}
 
Share this answer
 
Comments
[no name] 12-Jan-20 12:51pm    
OK. But I already have the numbers offered below the letter to check that the same number matches the same letter. How can I make that function?
Richard MacCutchan 12-Jan-20 12:56pm    
You really need to start thinking harder about how to do this. I have given you enough ideas to get started. Now it is up to you.
[no name] 12-Jan-20 12:58pm    
OK. Thank you so much, but I do not understand what your program does?
Richard MacCutchan 12-Jan-20 13:03pm    
It creates a lookup table from the two inputs. You can then find the letter that corresponds to each digit by the following:

char number = // the next number in a set
int index = number - '0';
char matchingLetter = table[index - 1]; // so 9 would return b, 5 would return l etc.
[no name] 12-Jan-20 13:10pm    
But what if a number is a bigger than length of string?
For example 8751 for string book, prints:



k

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900