Question
In C language - Given vector.h and vector.c from the Vector ADT section of the book, modify the code so that the vector holds characters
In C language - Given vector.h and vector.c from the Vector ADT section of the book, modify the code so that the vector holds characters instead of integers. Define also the following new functions in vector.c :
- void vector_from_string(vector* v, char* string) - Create vector v then copy the characters from string to v.
- void vector_print(vector* v) - Print vector v as a string; do not end with a newline
The result will implement a string that is more flexible than the standard C char* strings. In particular insertion and deletion of characters are allowed anywhere in the strings.
The main program performs various actions to test the new vector. Complete main() by replacing each TODO comment with code that implements the described actions.
Note: Some operations in main() occur in pairs. After the program performs the first operation, the second operation is applied to the result of the first operation, rather than the state of the vector before performing the first operation.
Ex:
vector = friend
Input indices for removal: a = 1 b = 2
friend -- removing character at index 1 --> fiend
fiend -- removing character at index 2 --> find
Ex: If the input is:
ridge
y
0 3
0 e 1 r
1 n 2 e
the output is:
ridge
ridgey
idgy
ergy
energy
These programs are to be used (main.c,vector.h,vector.c):
#include #include \"vector.h\"
int main(void) { int a; int b; char char1; char char2; vector v; char string[50]; // Read a string scanf(\"%s \", string);
// TODO: Create a vector from the input string
main.c vector_print(&v); printf(\" \"); // Read a character char1 scanf(\"%c\", &char1); // TODDO: Add char1 to the end of the string vector_print(&v); printf(\" \");
// Read two indices a and b scanf(\"%d %d\", &a, &b); // TODO: Remove the character at index a from the vector, // then remove the character at index b of the modified vector vector_print(&v); printf(\" \");
// Read two pairs of index and character scanf(\"%d %c %d %c\", &a, &char1, &b, &char2); // TODO: Replace the character at index a with char1 and // the character at index b with char2 vector_print(&v); printf(\" \");
// Read two pairs of index and character scanf(\"%d %c %d %c\", &a, &char1, &b, &char2); // TODO: Insert character char1 before index a of the vector, then // insert character char2 before index b of the modified vector vector_print(&v); printf(\" \");
// TODO: Free the memory allocated to the vectort
return 0; }
vector.h
// TODO: Modify the code so that the vector holds characters instead of integers -- a flexible string
#ifndef VECTOR_H #define VECTOR_H
#include \"string.h\" // for conversion
// struct and typedef declaration for Vector ADT typedef struct vector_struct { int* elements; unsigned int size; } vector;
// interface for accessing Vector ADT
// Initialize vector void vector_create(vector* v, unsigned int vectorSize);
// Destroy vector void vector_destroy(vector* v);
// Resize the size of the vector void vector_resize(vector* v, unsigned int vectorSize);
// Return pointer to element at specified index int* vector_at(vector* v, unsigned int index);
// Insert new value at specified index void vector_insert(vector* v, unsigned int index, int value);
// Insert new value at end of vector void vector_push_back(vector* v, int value);
// Erase (remove) value at specified index void vector_erase(vector* v, unsigned int index);
// Return number of elements within vector int vector_size(vector* v);
// Convert an ordinary string to a vector void vector_from_string(vector*, char* string);
// Print the vector as if it were a string void vector_print(vector* v);
#endif
vector.c
// TODO: Modify the code so that the vector holds characters instead of integers -- a flexible string
#include #include #include \"vector.h\"
// Initialize vector with specified size void vector_create(vector* v, unsigned int vectorSize) { int i;
if (v == NULL) return;
v->elements = (int*)malloc(vectorSize * sizeof(int));; v->size = vectorSize; for (i = 0; i size; ++i) { v->elements[i] = 0; } }
// Destroy vector void vector_destroy(vector* v) { if (v == NULL) return;
free(v->elements); v->elements = NULL; v->size = 0; }
// Resize the size of the vector void vector_resize(vector* v, unsigned int vectorSize) { int oldSize; int i;
if (v == NULL) return;
oldSize = v->size; v->elements = (int*)realloc(v->elements, vectorSize * sizeof(int)); v->size = vectorSize; for (i = oldSize; i size; ++i) { v->elements[i] = 0; } }
// Return pointer to element at specified index int* vector_at(vector* v, unsigned int index) { if (v == NULL || index >= v->size) return NULL;
return &(v->elements[index]); }
// Insert new value at specified index void vector_insert(vector* v, unsigned int index, int value) { int i;
if (v == NULL || index > v->size) return;
vector_resize(v, v->size + 1); for (i = v->size - 1; i > index; --i) { v->elements[i] = v->elements[i-1]; } v->elements[index] = value; }
// Insert new value at end of vector void vector_push_back(vector* v, int value) { vector_insert(v, v->size, value); }
// Erase (remove) value at specified index void vector_erase(vector* v, unsigned int index) { int i;
if (v == NULL || index >= v->size) return;
for (i = index; i size - 1; ++i) { v->elements[i] = v->elements[i+1]; } vector_resize(v, v->size - 1); }
// Return number of elements within vector int vector_size(vector* v) { if (v == NULL) return -1;
return v->size; }
// Convert an ordinary string to a vector void vector_from_string(vector* v, char* string) { vector_create(v, strlen(string));
/* Type your code here. */ }
// Print the elements of the vector (no final newline) void vector_print(vector* v) { /* Type your code here. */ } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started