Reading secret messages

Instructions:
A local company is developing a way to pass multiple messages to multiple places at the same time. It is one of those secret companies. You are to write a program that will extract only the text sent to a particular device and ignore the rest of the messages. Each message sent has an identifying two characters’ header (first being a $ followed by a single character) followed by a number indicating the size of the message that preceded $ tag symbol. (ie the actual message is in front of the identifier $tag. This is a new way to send message to fake out the folks who are monitoring your text trafficking.) You are to extract out only the set of messages (lines of text) that are addressed to you. Your identifying two characters is $Y so your program will only extract messages sent to $Y devices.

Example input:
oprfjiad093$X5abcdep0qj3 fqijfpiqjef$L21Your message to find.$Y21p9iqjv0$Ea0n wq9r r$K13Next Message.$Y1309q 4r9 qefkjad0$Tfu8;lkfdv98

Example output:
Your message to find.
Next Message.


Your test data for your program is found in the following file on blackboard
'MessagesAtEnd.txt'
You will read in the input text into an array of characters. Example char line[300]. You will use pointers to move through the array (ie no [ ]’s in your code.) and extract and print out your text.

Hello again. I am having trouble figuring out how to basically flag the system to read only the message meant for me. So far I don't have much, but we are only supposed to read the message if it is a $Y and then the next character after that tells the length of the message, then you read backwards. My idea was if as you are reading, the message has a $ and then a Y, read the number, then go backwards and read, but I'm having trouble figuring out what the syntax for that would be. I know my code is really wrong, but I was just wondering if someone could point me on how to approach this syntax-wise.


***WE HAVE NOT LEARNED VECTORS. HE DOES NOT WANT [] ANYWHERE EXCEPT THE INITIALIZATION OF THE ARRAY, BUT I WAS GOING TO GET IT WORKING FIRST THEN SWITCH THEM TO POINTERS****

Message
**********************************************************************
Enter message file: Enter real message file: $Zqva'HMQ[V$L01$M56On the twelfth day of Christmas my true love gave to me$Y55xN\t}h0{YS/L$X66The 1997 Chautauqua Program has been released. This is a faculty !-2_w2@~:M/u#$/$X71development program of the National Science Foundation. While much of ]gP0@uItU}1WkCS$L71the information is directed to science and mathematics, some of it is $L69twelve drummers drumming, eleven pipers piping, ten lords a-leaping,$Y68E9`$S60more general in nature and appropriate to all disciplines. $A01$G68nine ladies dancing, eight maids a-milking, seven swans a-swimming,$Y67DFIaZ\^zQ'NV$X73The short courses are usually three days long and are limited to around HtwGa&l+Lcgb$S6725 participants. The application requires a statement of interest $P37six geese a-laying, five gold rings$Y35;E~(s8Qr5YALv$X73explaining why you want to take the course and how you will use it when $G56four calling birds, three french hens, two turtle doves$Y55j$_eE]p$j4l$S68you return to your home institution. They are presented at various @R~Z,v>$M71locations around the country, including Atlanta, Memphis, and Dayton, $L32and a partridge in a pear tree.$Y31,6*`lWT$M06Ohio.s'$L01p}/dbbBXY$S16That's all folks?$Y17There an't no more cv03248(%$76.$Y1.ikjh7*&(^$&^ $y7The end
************************************************************************
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
//functions 
void readCharacters(char message [])
{
    int i;
    for(i=0;i<29;i++)   //kept it small to test first..I know all of this is wrong
    {
   
        if(message[i]=='$'&& message[i+1]=='Y'&& isdigit(message[i+2]))
        {
            infile>>message[i];
           // cout<<message[i]<<endl;
        }
         
        cout<<message[i]<<" ";
    }
   
}

int main()

{
    
    infile.open("C:\\data\\input\\TestMessage.txt");
    
    if(!infile)
    {
        cout<<"File did not open. Please try again.";
        return 0;
    }

    char message[600];  //array to hold the encrypted message
    char *ptr=nullptr;  //was going to get it working before adding the pointers. 
    ptr=message;
    int i;
   
    
    readCharacters(message);
Last edited on
> You will read in the input text into an array of characters.
I think that means to dump the full file into the array
then you'll process the array to extract your message

1
2
3
4
5
6
7
//pseudocode with index
def extract_message(array):
	for pos in range(len(array)-1):
		if array[pos] == '$' and array[pos+1] == 'Y':
			//the message is for us
			length = read_int(array, pos+2)
			return array[pos-length:pos]


> I'm having trouble figuring out what the syntax for that would be
1
2
3
4
char *ptr = message; //start at the begin
++ptr; //move forward one position
*ptr; //read the character
ptr -= length; //jump backwards 



by the way, in your example you have
input: r r$K13Next Message.$Y1309q 4r
output: Next Message.

I don't understand why it only jumps 13 positions instead of 1309
cause Idk..I just didnt know if I should be checking it before or after reading it into the array so I started small just to read in a few characters. I knew it was all wrong. Figured I had to have something written before anyone would help me.
Yep that first Chegg question was written by me.
Ok the second one I haven't seen yet and looks like it was answered. Thanks guys. I'll check that out and get back on here cause sometimes the experts dont read the questions and answer them how they should.
For anyone that needs help. I got it working. Just have to clean some things up and make functions, but here is a snippet:

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
 
    infile.get(message, 1300);
     ptr=message;
       while(*ptr!='\0')
       {//traverse until the end of the array
        if((*ptr=='$') && (*(++ptr)==key))
       {//check for $Y pattern in the string
           ptr=ptr+1;
           if(isdigit(*ptr))
           {//after $Y pattern check for numeric digit
                                  
              length=length*10+(*ptr-48);//finds the length of the message
              //cout<<"length: "<<length<<endl; //for test purposes 
               ptr++; 
               
               if(isdigit(*ptr))
               {
                   length=length*10+(*ptr-48);
                   //cout<<"length 2: "<<length<<endl;  //for testing 
                   
               
               }
           }
           ptr=(ptr-length)-3;
             for(int i=0;i<length;i++)
             {//print the message
               cout<<*ptr; //character by character
               ptr++;
           }
             
           
           cout<<endl;
                  length=0;//reset length to print another message
          
       }
        
       ptr++;
    
       }
Last edited on
Topic archived. No new replies allowed.