online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
#include <stdio.h> #include <string.h> /* Setup some constants related to ASCII encoding. */ const int zeroIndexAscii = 48; // First and last numeral indices. const int nineIndexAscii = 57; const int upperAIndexAscii = 65; // First and last upper-case letter indices. const int upperZIndexAscii = 90; const int lowerAIndexAscii = 97; // First and last lower-case letter indices. const int lowerZIndexAscii = 122; const int spaceAscii = 32; // Space character index. const int lowerToUpperAscii = 32; // Subtract from lowercase letter to convert to uppercase. const int alphaToMorse = 55; // Subtract from alpha character to get Morse index. const int numericToMorse = 48; // Subtract from numeric character to get Morse index. const int spaceToMorse = 36; // The Morse index of a space character. /* Setup some constant arrays related to Morse and binary code. */ char* binaryCode[3] = {"1110", "10", "00"}; // Dash, Dot, Sep binary encoding. char binaryMorse[3] = {'-', '.', ' '}; // Morse symbols to encode to binary. char* morseCode[37] = // Morse symbols corresponding to letters and numerals. { "-----",// 0, index 0 ".----",// 1 "..---",// 2 "...--",// 3 "....-",// 4 ".....",// 5 "-....",// 6 "--...",// 7 "---..",// 8 "----.",// 9 ".-", // A, index 10 "-...", // B "-.-.", // C "-..", // D ".", // E "..-.", // F "--.", // G "....", // H "..", // I ".---", // J "-.-", // K ".-..", // L "--", // M "-.", // N "---", // O ".--.", // P "--.-", // Q ".-.", // R "...", // S "-", // T "..-", // U "...-", // V ".--", // W "-..-", // X "-.--", // Y "--..", // Z " " // Space, index 36 }; /* Print the binary code for a given morse character. */ void printBinaryCode(int morseIndex, int* charCount, int* bitCount) { // Temp vars to make calculations easier. int tempChar; int binaryIndex; // If the index is out of range, ignore this character. if (morseIndex < 0 || morseIndex > 36) return; // Increment the count of processed characters. *charCount += 1; // Get the length of this morse string. int morseLength = strlen(morseCode[morseIndex]); // Go through each Morse character and print the appropriate binary code. for (int i = 0; i < morseLength; i++) { // Get the character at this position so we can manipulate it more easily. tempChar = morseCode[morseIndex][i]; // Set the binary index appropriately. binaryIndex = -1; for (int j = 0; j < 3; j++) { if (tempChar == binaryMorse[j]) { binaryIndex = j; break; } } // Print the binary code if the index is valid. if (binaryIndex >= 0) { printf("%s", binaryCode[binaryIndex]); // Increment the bit count, adding a newline about every 100 bits. //int tempCount = *bitCount; *bitCount += strlen(binaryCode[binaryIndex]); //if (*bitCount % 100 < tempCount % 100) // printf("\n"); } } } /* Program entry point, gets data in ASCII and converts alphanumeric characters to binary-coded Morse code. */ int main() { // Temp vars to make calculations easier char tempChar; int morseIndex; // Vars for getting the average bit length of the characters. int charCount = 0; int bitCount = 0; float bitsPerChar; // Setup an input string to get data from the user. const int inputStringSize = 5001; // 5000 chars plus a null. char inputString[inputStringSize]; int inputCharCount; // Get the data from the user. printf("Enter a text string (up to %i characters):\n", inputStringSize - 1); fgets(inputString, inputStringSize, stdin); printf("\nBinary Code is:\n"); inputCharCount = strlen(inputString); // Go through every character in the string to convert to binary-coded Morse code. for (int i = 0; i < inputCharCount; i++) { // Get the character at this position so we can manipulate it more easily. tempChar = inputString[i]; // If this is a lower-case character, convert to upper case. if (tempChar >= lowerAIndexAscii && tempChar <= lowerZIndexAscii) tempChar -= lowerToUpperAscii; // Set the Morse index appropriately. if (tempChar >= upperAIndexAscii && tempChar <= upperZIndexAscii) morseIndex = tempChar - alphaToMorse; else if (tempChar >= zeroIndexAscii && tempChar <= nineIndexAscii) //morseIndex = -1; // Ignore numeric characters to compare to 5-bit "ASCII". morseIndex = tempChar - numericToMorse; // Process numeric characters normally. else if (tempChar == spaceAscii) morseIndex = spaceToMorse; else morseIndex = -1; // Prevent invalid indexes for non-supported characters. // Call the helper function to print this character in binary-coded Morse code. printBinaryCode(morseIndex, &charCount, &bitCount); } // Print the bit and character counts. printf("\nInput consisted of %i characters, of which %i were processed and converted to %i bits.\n", inputCharCount, charCount, bitCount); // Print the average bits per character, but avoid division by zero. if (charCount > 0) printf("On average, this encoding used %.2f bits per character.\n", (bitCount / (float)charCount)); // Flush the buffer and quit the program. fflush(stdout); return 0; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue