Question

In: Computer Science

I have a char memory[1024]. I want to write the string "mike" into the memory starting...

I have a char memory[1024]. I want to write the string "mike" into the memory starting from an index. I also need a function to move "mike" to another index in the same memory swapping m and k in mike. Basically, i need a function to write to memory and a function to move the string to an index. Thank you

In c++ please.

Solutions

Expert Solution

If this answer doesn't meet your requirements, then please comment and let me know, so that I can help you.

Please look at my code and in case of indentation issues check the screenshots.

---------main.cpp---------------

#include <iostream>
#include <string.h>
using namespace std;


//this function intializes the memory with _, you can choose to intialize it with any letter you want.
void initialize_memory(char memory[], int size)
{
   for(int i = 0; i < size; i++){
       memory[i] = '_';
   }
}


void writeString(char memory[], int size, int index, char *str)
{
   for(int i = 0; i < strlen(str); i++)   //writes the string at the specified index
   {
       if(index < size){
           memory[index] = str[i];   //copies each character one by one
           index++;
       }
   }
}

void moveString(char memory[], int size, int oldindex, int newindex)
{
   int i = oldindex;  
   char temp[size];       //temporary array to read the string and store its copy
   int j = 0;
   while(memory[i] != '\0'){
       temp[j] = memory[i];   //copy each character of the old string from the memory
       i++;
       j++;
   }
   temp[j] = '\0';           //terminate the string with null character, to denote the end of string

   initialize_memory(memory, size);   //reinitialize memory
   writeString(memory, size, newindex, temp);   //write the string at newindex, the string is in temp array
}

int main()
{
   int size = 1024;
   char memory[size];    //declare a character array memory of size 1024

   initialize_memory(memory, size);   //initialize the memory with _ at every index
  
   int index;
   cout << "At which index do you want to write the string: ";       //ask the user where they want to write the string in memory
   cin >> index;

   writeString(memory, size, index, (char *)"mike");   //write the string at the memory specified by the user
   cout << "Memory:\n" << memory << endl;               //print the memory array

   int oldindex = index;   //store the location of the index, where we wrote the string
   int newindex;          
   cout << "\nAt which index do you want to move the string: "; //ask the user another index, where they want to move the string
   cin >> newindex;                          

   moveString(memory, size, oldindex, newindex);   //move the string the location specified by the user
   cout << "Memory:\n" << memory << endl;               //print the memory array
   return 0;
}


----------Screenshots--------------


----------Output--------------


------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs down.
Please Do comment if you need any clarification.
I will surely help you.

Thankyou


Related Solutions

Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
-Write in C++ -Use Char library functions Write a function that accepts a string representing password...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties: 1. At least 8 characters long 2. Has at least one upper case letter 3. Has at least one lower case letter 4. Has at least one digit 5. Has at least on special character
write a java program to Translate or Encrypt the given string : (input char is all...
write a java program to Translate or Encrypt the given string : (input char is all in capital letters) { 15 } *) Each character replaced by new character based on its position value in english alphabet. As A is position is 1, and Z is position 26. *) New characters will be formed after skipping the N (position value MOD 10) char forward. A->A+1= B , B->B+2=D ,C->C+3=F, .... Y->Y+(25%10)->Y+5=D A B C D E F G H I...
6- Write a C++ program that determines the amount of memory used by char, short int,...
6- Write a C++ program that determines the amount of memory used by char, short int, unsigned short int, float, and double types and display the information on the screen. You program should also display the typical range for each data type.
Hi! I wrote two function to get count cluster of char in a string , charFreq(),...
Hi! I wrote two function to get count cluster of char in a string , charFreq(), and an other one which iterate through a vector line by line looking for last element on each line, last(). The problem is, after appending all chars in a string and try to count clusters of values in that string, I get seg fault. I feel like my logic behind is ok, but I am not sure what I did wrong. Can someone help...
c++ I want to flip the string when it's string type. For example, if it is...
c++ I want to flip the string when it's string type. For example, if it is 'apple', I would like to print 'elppa'. how can i do?
Write an essay on why I want to be an educator. What I believe I have...
Write an essay on why I want to be an educator. What I believe I have to offer the students of our future?
Redo Program 1 so that you have a template class, that works with a char, string,...
Redo Program 1 so that you have a template class, that works with a char, string, int, or double data type. Program 1 is below: #include <iostream> #include <string> #define CAPACITY 12 using namespace std; class queue { private: string strArray[CAPACITY]; int frontIndex, endIndex; int queueSize; public: queue() { frontIndex = endIndex = -1; queueSize = 0; } void enqueue(string); string dequeue(); int size() { return queueSize; } int front() { return frontIndex; } int end() { return endIndex; }...
For university, I have to write a Research paper and i want to examine whether there...
For university, I have to write a Research paper and i want to examine whether there is a relationship (correlation)between hours worked in a Country per week and the Happiness Index points as the dependent variable. However, it has to be a mutiple linear regression model but I struggle with finding more independent variables. Can somebody help me here?
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates...
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates to string s with all occurrences of c removed. The string c is guaranteed to be a length-1 string; in other words a single character string. For example (remove-char "abc" "b") should evaluate to "ac". Here is pseudocode that you could implement.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT