The program should read out exactly as the example and must use iostream in C++
ID: 3597889 • Letter: T
Question
The program should read out exactly as the example and must use iostream in C++
the song lyrics in the file
Write a program that computes the average word length (the average number of characters per word) for a file that contains some text. Characters associated with a word are defined to be composed of any alphabetical characters, the apostrophe character in the case of a contraction, and a single hyphen in the case of a hyphenated word. Words are separated by spaces or return characters. Whitespace characters, double dashes, punctuation, and other non-alphabetic characters (except those previously noted) should be ignored in the character count for a given word. A new paragraph (two return characters) should not increase the character or word count. Specifications: The text file is called "bob.txt", and can be found on the website for ECE 71. It contains the lyrics for Weird Al Yankovic's palindrome song, "Bob", in which every line is composed of a palindrome https://www.voutube.com/watch?v-JUODzi6R3 Copy and paste the file into a "bob.txt" file within your project directory, in a manner similar to HW 20 Use a function to read in each character one at a time from the file, following the rules noted above to determine whether the character should be counted toward a word or not. Only count characters that are part of words, and be sure to increase the word count only for a valid word. It will be helpful to keep track of the current character and previous character. The function should also work with the stream cin as an input stream, even though the function will not be called with cin as an argument in this program. Use the following function prototype: void wordLength (istream &bob;) Print 3 columns to the screen. As characters are read in from the file, the first column will print every character in the file. The second column will print the current valid character count. The third column will print the current number of words. Separate each column with the tab character. After the entire file has been printed, calculate and print the average number of characters per word using 2 values after the decimal point The only purpose of the main function is to open the file, exit the program if the file fails to open, call the wordLength () function, and close the file. As an example, if you execute the program with the following underlined inputs, the output will be Chars: Chars: 2 Words: 0 Words: 0Explanation / Answer
void raw_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
{ {
u_int length = h->len;
u_int caplen = h->caplen;
/* At this point we start extracting data from the packet, with the var idx keeping up with where we are in the packet */
// Print total packet size
printf(" Packet Length %d bytes", h->len);
// Decode Ethernet (Layer 2) Header Info
int idx; // counter variable for indexing through packet
char eth_destination[6]; // var to store the ethernet destination address
char eth_source[6]; // var to store the ethernet source address
short eth_type; // var to store the ethernet type value
char eth_payload[10]; // string array to store payload description
char eth_bcast[6] = "0xFF";
// Extract Ethernet Destination Address (1st 6 bytes)
for ( idx = 0; idx < 6; idx++)
{
eth_destination[idx] = p[idx];
}
// Extract Ethernet Source Address (2nd 6 bytes)
for ( idx = 6; idx < 12; idx++)
{
eth_source[idx-6] = p[idx];
}
// Combine two byte Ethernet Type/Length field into one value
eth_type = p[12] * 256 + p[13];
// Check the packet type and increment related counter
if ( eth_type >= 0x600) {
switch ( eth_type )
{
case 0x800: // Check to see if the type indicates that the packet is IP
ctrIP++;
strcpy(eth_payload, "IP");
break;
case 0x806: // Check to see if the type indicates that the packet is ARP
ctrARP++;
strcpy(eth_payload, "ARP");
break;
default:
break;
}
}
// Check to see if the destination was a broadcast packet and increment related counter
/*
if ((eth_destination[0] == 0xFF ) && (eth_destination[1] == 0xFF ) && (eth_destination[2] == 0xFF ) && (eth_destination[3] == 0xFF ) && (eth_destination[4] == 0xFF ) && (eth_destination[5] == 0xFF )) {
ctrBCAST++;
}
*/
// Print Ethernet (Layer 2) Header Info
printf("Layer Field Value ");
printf("ETHERNET Destination %02x:%02x:%02x:%02x:%02x:%02x ", eth_destination[0],eth_destination[1],eth_destination[2],eth_destination[3],eth_destination[4],eth_destination[5]);
printf(" Source %02x:%02x:%02x:%02x:%02x:%02x ", eth_source[0],eth_source[1],eth_source[2],eth_source[3],eth_source[4],eth_source[5]);
printf(" Type 0x%02x ", eth_type);
printf(" Payload %s ", eth_payload);
// All packets will have ethernet info (decoded and printed above).
// At this point we have to determine what kind of data is at the next layer up (layer 3) and decode/print the data accordingly.
// This is done based on the eth_type variable.
if ( eth_type >= 0x600) {
switch ( eth_type )
{
case 0x800: // IP Packet
// insert code to decode and print IP (should this be a separate sub-routine?
break;
case 0x806: // ARP Packet
// insert code to decode and print ARP
break;
default:
break;
}
}
exit(0);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.