How to access the last character in an element of string array

I am provided with a coded message, a definition list, and asked to write a program to translate the message.

And the definition list looks like this:

A = F
B = D
C = I
...and so on until the end of the alphabet

--where for every A in the coded message, it should be an F. I will store this entire list of definitions into a string named ourCode[26] so I can iterate through the message and replace letters. But how would you access the final character in an element of the string array?

For example I have "CA" and I want to replace C with I. But as I understand it, the current string array ourCode have | "A = F" | "B = D" | "C = I" | so how would I take the element "C = I", extract the I, and put that where the C in the original code line is?
provide some code for context. [code] code in here ['\'code]
Sorry about that. Here's the code. It's pseudocode though because I don't know what to put for the for-loop that'll do the actual decoding.

Also, the input file they'll use to give us the code and the coded message will look like this: https://78.media.tumblr.com/4afdb31f3a23dd12b62154538086bc9d/tumblr_inline_p5t71hCVJa1utxlwl_540.png

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {

int size = 0;

string ourCode[26];

string ourFile;

string codedMessage, decodedMessage;

cout << "Please enter the name of your desired file: " << endl;
cin >> ourFile;

ifstream inFile;

inFile.open(ourFile);
line = "";
while (getline(inFile, line)) {
if (inFile.is_open()) { //confirm that the file is open
for (int i = 0; i < 26; i++) {
ourCode[i] = line; //take the first 26 lines as our decoder information
}
cin >> size; //how many messages there will be; stored for later printing
cin >> codedMessage;
}
else {
cerr << "File can't be opened." << endl;
delete[] Series;
exit(1);}
}

/*for now we assume there is only 1 coded message to resolve*/

int j = 0;
for (int i = 0; i < "\0"; i++) { //we will be going through our codedMessage array
if (codedMessage[i] == ourCode[j]) {
        //perform the function to swap letter in ourCode with correct one provided by
       //array ourCode
}
j++;
}

decodedMessage = codedMessage;

ofstream outFile;
outFile.open("result.txt");

while (outFile.is_open) {
outFile << "The number of messages are " << size << endl;
outFile << "The decoded message is " << decodedMessage << endl;
outFile.close();
}
Last edited on
Topic archived. No new replies allowed.