Untitled

 avatar
unknown
plain_text
13 days ago
2.3 kB
4
Indexable
#include <iostream> #include <sstream> #include <bitset> #include <vector> using namespace std;

// Function to determine IP class char getIPClass(int firstOctet) { if (firstOctet >= 1 && firstOctet <= 126) return 'A'; else if (firstOctet >= 128 && firstOctet <= 191) return 'B'; else if (firstOctet >= 192 && firstOctet <= 223) return 'C'; else if (firstOctet >= 224 && firstOctet <= 239) return 'D'; else return 'E'; }

// Q1 & Q2: Determine IP class void determineIPClass(const string& ip, bool allClasses = true) { int firstOctet; sscanf(ip.c_str(), "%d", &firstOctet); char ipClass = getIPClass(firstOctet);

if (allClasses || (ipClass == 'A' || ipClass == 'B' || ipClass == 'C'))
    cout << "IP Address " << ip << " belongs to Class " << ipClass << endl;
    else
        cout << "IP Address " << ip << " is not in Class A, B, or C." << endl;
        
        }

        // Q3: Convert dotted decimal to 32-bit binary void convertToBinaryIP(const string& ip) { int octets[4]; sscanf(ip.c_str(), "%d.%d.%d.%d", &octets[0], &octets[1], &octets[2], &octets[3]); bitset<32> binaryIP((octets[0] << 24) | (octets[1] << 16) | (octets[2] << 8) | octets[3]); cout << "Binary Representation: " << binaryIP << endl; }

        // Q5: Bit Stuffing and De-Stuffing string bitStuff(const string& input) { string stuffed = ""; int count = 0; for (char bit : input) { stuffed += bit; if (bit == '1') count++; else count = 0; if (count == 5) { stuffed += '0'; count = 0; } } return stuffed; }

        string bitDeStuff(const string& input) { string destuffed = ""; int count = 0; for (size_t i = 0; i < input.length(); i++) { destuffed += input[i]; if (input[i] == '1') count++; else count = 0; if (count == 5 && i + 1 < input.length() && input[i + 1] == '0') { i++; // Skip stuffed 0 count = 0; } } return destuffed; }

        int main() { string ip; cout << "Enter IP Address: "; cin >> ip; determineIPClass(ip, true); // Q1 determineIPClass(ip, false); // Q2 convertToBinaryIP(ip); // Q3
        
        // Q5 Bit Stuffing
        string inputBits;
        cout << "Enter bit stream for stuffing: ";
        cin >> inputBits;
        string stuffed = bitStuff(inputBits);
        cout << "Stuffed Bits: " << stuffed << endl;
        cout << "De-stuffed Bits: " << bitDeStuff(stuffed) << endl;
        
        return 0;
        
        }

        
Editor is loading...
Leave a Comment