Question

In: Computer Science

convert this program that is for string into character input data #include "stdafx.h" #include <string.h> #include...

convert this program that is for string into character input data

#include "stdafx.h"
#include <string.h>
#include <stdlib.h>

unsigned int putIntoHashTable(char *ptrInputData, unsigned int bufferLength); // function to add to hash table
unsigned int getFromHashTable(char *ptrOutputData, unsigned int bufferLength); // function to retrieve data from hash table

#define INPUT_BUFFER_SIZE 200 // local buffer used for adding data to the hash table (there is no reason in this assignment to change this value)

#define HASH_SIZE 100 // size of hash table to be used (for testing I suggest making this number much lower)

                                               // data structure used to keep track of hashed data
struct myHashStruct {
   char *ptrBuffer = NULL; // pointer to data stored in hash (you will need to malloc space for string to be stored)
   struct myHashStruct *ptrNextHashData = NULL; // pointer to next item in this hash bucket (or NULL if no more)
};

struct myHashStruct *myHash[HASH_SIZE]; // create an empty hash table structure (note this is basically an arrary of linked list heads)

int main()
{
   char inputBuffer[INPUT_BUFFER_SIZE];

   // initialize the hash table to empty one
   for (int i = 0; i < HASH_SIZE; i++)
   {
       if ((myHash[i] = (struct myHashStruct *)calloc(1, sizeof(struct myHashStruct))) == NULL)
       {
           printf("calloc failed!\n");
           return(-1);
       }
   }

   // add to hash table loop
   while (1)
   {
       printf("enter data to be added to hash table or exit when done\n");

       // get strings from the console and place in hash until nothing entered
       scanf_s("%s", inputBuffer, INPUT_BUFFER_SIZE);

       // stop adding data into hash table when "exit" is entered
       if (strcmp(inputBuffer, "exit") == 0)
           break;

       if (putIntoHashTable(inputBuffer, strlen(inputBuffer)) == HASH_SIZE)
           printf("Error putting into hash table\n");
   }

   // check if data is in hash table
   while (1)
   {
       unsigned int hashIndexNumber = 0;

       printf("Enter data to find, done when complete\n");

       // get strings from the console and check if in hash table
       scanf_s("%s", inputBuffer, INPUT_BUFFER_SIZE);

       // stop adding data into hash table when "done" is entered
       if (strcmp(inputBuffer, "done") == 0)
           break;

       if ((hashIndexNumber = getFromHashTable(inputBuffer, strlen(inputBuffer))) == HASH_SIZE)
           printf("%s not found in hash table\n", inputBuffer);
       else
           printf("%s found in hash table at %u\n", inputBuffer, hashIndexNumber);
   }

   return 0;
}

// FUNCTION : myHashFunction
// DESCRIPTION :
// Calculate a hash value to use in storing the data into the hash table
// PARAMETERS :
// ptrInputBuffer - a pointer to the buffer to be put into the hash table
//
// RETURNS :
// Hash value calculated ( HASH_SIZE on failure)

unsigned int myHashFunction(char *ptrInputBuffer)
{
   unsigned int calculatedHashValue = 0;

   // add code to create a hashed value here!!!
  
   for (int i = 0; i < HASH_SIZE; i++) // here string can be add based on hash value
   {
      
           calculatedHashValue += ptrInputBuffer[i]; // hash value will be added in buffer of size i
      
   }
printf("Hash Calculated to be :%d \n", calculatedHashValue); // hash value that calculated will be printed
   // make sure if hash value is bigger than the table size, the value wraps
   return calculatedHashValue % HASH_SIZE;
}

// FUNCTION : putIntoHashTable
// DESCRIPTION :
// Put the supplied data into a hash table
// PARAMETERS :
// ptrInputBuffer - a pointer to the buffer to be put into the hash table
// bufferLength - how many characters are in the buffer being put into the hash table
//
// RETURNS :
// Hash value used ( HASH_SIZE on failure)

unsigned int putIntoHashTable(char *ptrInputData, unsigned int bufferLength)
{
   unsigned int hashValue = myHashFunction(ptrInputData);

   // check if the value is in the hash

   // add code to put data into the hash table!!!

   return hashValue;
}

// FUNCTION : getFromHashTable
// DESCRIPTION :
// Read as much data as there is room for from the hash table
// PARAMETERS :
// ptrOutputBuffer - a pointer to the buffer to place the data read from hash table
// bufferLength - maxiumum number of characters that can be read
//
// RETURNS :
// Hash value used ( return HASH_SIZE value on failure)

unsigned int getFromHashTable(char *ptrOutputData, unsigned int bufferLength)
{
   unsigned int hashValue = myHashFunction(ptrOutputData);

   // check if the data is in the hash table here!!!

   return hashValue;
}

// end code * * *   


yes for single character

Solutions

Expert Solution

Below is the code for the above problem:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<string.h>

unsigned int putIntoHashTable(char InputData); // function to add to hash table
unsigned int getFromHashTable(char OutputData); // function to retrieve data from hash table

//#define INPUT_BUFFER_SIZE 200 // local buffer used for adding data to the hash table (there is no reason in this assignment to change this value)

#define HASH_SIZE 100 // size of hash table to be used (for testing I suggest making this number much lower)

                                               // data structure used to keep track of hashed data
struct myHashStruct {
   char Buffer ; // pointer to data stored in hash (you will need to malloc space for string to be stored)
   struct myHashStruct *ptrNextHashData; // pointer to next item in this hash bucket (or NULL if no more)
};

struct myHashStruct *myHash[HASH_SIZE]; // create an empty hash table structure (note this is basically an arrary of linked list heads)

int main()
{
   char inputBuffer;

   // initialize the hash table to empty one
   for (int i = 0; i < HASH_SIZE; i++)
   {
       if ((myHash[i] = (struct myHashStruct *)calloc(1, sizeof(struct myHashStruct))) == NULL)
       {
           printf("calloc failed!\n");
           return(-1);
       }
   }

   // add to hash table loop
while (1)
   {
       printf("enter data to be added to hash table or enter 1 when done\n");

       // get character from the console and place in hash until nothing entered
       scanf("%c", &inputBuffer);
   if(inputBuffer =='\n') //if new line , read next character to new line
   scanf("%c",&inputBuffer);
      // stop adding data into hash table when '1' is entered   
   if (inputBuffer == '1')
           break;
       if (putIntoHashTable(inputBuffer) == HASH_SIZE)
           printf("Error putting into hash table\n");
   }

   // check if data is in hash table
   while (1)
   {
       unsigned int hashIndexNumber = 0;

       printf("Enter data to find, enter 0 when complete\n");

       // get character from the console and check if in hash table
       scanf("%c",&inputBuffer);

   if(inputBuffer == '\n')   //if new line , read next character to new line
   scanf("%c",&inputBuffer);

       // stop adding data into hash table when '0' is entered
       if (inputBuffer == '0')
           break;
       if ((hashIndexNumber = getFromHashTable(inputBuffer)) == HASH_SIZE)
           printf("%c not found in hash table\n", inputBuffer);
       else
           printf("%c found in hash table at %u\n", inputBuffer, hashIndexNumber);
   }

   return 0;
}

// FUNCTION : myHashFunction
// DESCRIPTION :
// Calculate a hash value to use in storing the data into the hash table
// PARAMETERS :
// ptrInputBuffer - a pointer to the buffer to be put into the hash table
//
// RETURNS :
// Hash value calculated ( HASH_SIZE on failure)

unsigned int myHashFunction(char InputBuffer)
{
   unsigned int calculatedHashValue = 0;

   // add code to create a hashed value here!!!
  
           calculatedHashValue += InputBuffer; // hash value will be added in buffer of size i

printf("Hash Calculated to be :%d \n", calculatedHashValue); // hash value that calculated will be printed
   // make sure if hash value is bigger than the table size, the value wraps
   return calculatedHashValue % HASH_SIZE;
}

// FUNCTION : putIntoHashTable
// DESCRIPTION :
// Put the supplied data into a hash table
// PARAMETERS :
// ptrInputBuffer - a pointer to the buffer to be put into the hash table
// bufferLength - how many characters are in the buffer being put into the hash table
//
// RETURNS :
// Hash value used ( HASH_SIZE on failure)

unsigned int putIntoHashTable(char InputData)
{
   unsigned int hashValue = myHashFunction(InputData);

   // check if the value is in the hash

   // add code to put data into the hash table!!!

   return hashValue;
}

// FUNCTION : getFromHashTable
// DESCRIPTION :
// Read as much data as there is room for from the hash table
// PARAMETERS :
// ptrOutputBuffer - a pointer to the buffer to place the data read from hash table
// bufferLength - maxiumum number of characters that can be read
//
// RETURNS :
// Hash value used ( return HASH_SIZE value on failure)

unsigned int getFromHashTable(char ptrOutputData)
{
   unsigned int hashValue = myHashFunction(ptrOutputData);

   // check if the data is in the hash table here!!!

   return hashValue;
}

// end code * * *

OUTPUT:


Related Solutions

Write a program that accepts a string and character as input, then counts and displays the...
Write a program that accepts a string and character as input, then counts and displays the number of times that character appears (in upper- or lowercase) in the string. Use C++ Enter a string: mallet Enter a character: a "A" appears 1 time(s) Enter a string: Racecar Enter a character: R "R" appears 2 time(s)
Write a program whose input is a string which contains a character and a phrase, and...
Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. Ex: If the input is: n Nobody the output is: 0...
Write a basic C++ program with function, whose input is a character and a string, and...
Write a basic C++ program with function, whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex: If the input is: n...
convert this string type   program into interger data type #include "stdafx.h" #include #include unsigned int putIntoHashTable(char...
convert this string type   program into interger data type #include "stdafx.h" #include #include unsigned int putIntoHashTable(char *ptrInputData, unsigned int bufferLength); // function to add to hash table unsigned int getFromHashTable(char *ptrOutputData, unsigned int bufferLength); // function to retrieve data from hash table #define INPUT_BUFFER_SIZE 200 // local buffer used for adding data to the hash table (there is no reason in this assignment to change this value) #define HASH_SIZE 100 // size of hash table to be used (for testing...
In objective-C Task: Write a program whose input is a character and a string, and whose...
In objective-C Task: Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. The output should include the input character and use the plural form, n's, if the number of times the characters appears is not exactly 1.You may assume that the string does not contain spaces and will always contain less than 50 characters. Ex: If the input is: n Monday the output is:...
To begin, write a program to loop through a string character by character. If the character...
To begin, write a program to loop through a string character by character. If the character is a letter, print a question mark, otherwise print the character. Use the code below for the message string. This will be the first string that you will decode. Use the String class method .charAt(index) and the Character class method .isLetter(char). (You can cut and paste this line into your program.) String msg = "FIG PKWC OIE GJJCDVKLC MCVDFJEHIY BIDRHYO.\n"; String decryptKey = "QWERTYUIOPASDFGHJKLZXCVBNM";...
Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase.
# PYTHONWrite a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase.Ex: If the input is:n Mondaythe output is:1Ex: If the input is:z Today is Mondaythe output is:0Ex: If the input is:n It's a sunny daythe output is:2Case matters.Ex: If the input is:n Nobodythe output is:0n is different than N.
Write a C program that will read a character string and then encrypt the string based...
Write a C program that will read a character string and then encrypt the string based on one of the 3 different encryption methods. The type of encryption is to be selected by the user. Encryption method 1: Swapping by position. Characters in the array are swapped with the opposite characters based on their position in the string. Example: Input string – apple. Encrypted string – elppa Method: The first character ‘a’ and the last character ‘e’ – swap their...
The input will be a String with multiple not-always-lowercase directions separated by underscores. The last character...
The input will be a String with multiple not-always-lowercase directions separated by underscores. The last character of the input is always an underscore. You must output the final (x, y) coordinates of a robot, starting at (0, 0), after it has followed all of the directions. Print the result in the format “Result: (x, y)” USE THE SKELETON PROGRAM PROVIDED. Example #1: INPUT: up_down_left_down_down_ Result: (-1, -2) up --move→ y + 1 = 1 down --move→ y - 1 =...
How To Print Nice Padding Character in a String in C++? 1. User input the width...
How To Print Nice Padding Character in a String in C++? 1. User input the width = 14 2. User input the padding character = + Example output: ++Hello World++ Thanks in advance.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT