How to insert strings into an array

does anybody knows how to insert strings into an array of type char and also delete strings from that char array.

any help would be apreciated
There are more than one ways to do so. Show us what exactly it is that you have and what you have tried.
we are asked to count number of characters and words which i have done and insert and delete strings in this char array ........... below is what i have done, im confused only with that insert and delete part.


#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>

using namespace std;

void ReadtoMemory(ifstream& InFile, char Memory[])
{
string line;
int MemIndex = 0;

if (InFile.is_open())
{
while ( InFile.good() )
{
getline (InFile,line);
for(int i = 0; i < line.length(); i++)
{
Memory[MemIndex] = line[i] ;

MemIndex++;
}
}
}
}

void OutputMemoryToFile(char Memory [], ofstream& Out)
{
string line;
int MemIndex = 0;

if (Out.is_open())
{
int i = 0;
while ( Memory[i] != '\0')
{
Out<<Memory[i];
i++;
}
}
}

void DocumentStatisticsPrint(char Memory[])
{
int NumberWords = 0;
int i = 0; //used to count number of characters (includes spaces)
while ( Memory[i] != '\0')
{
if(Memory[i] == ' ')
NumberWords ++;
cout<<Memory[i];
i++;
}
cout<<endl;
cout<<NumberWords<<" is number of words "<<endl;
cout<<"Number of Characters (Including Spaces)"<<i<<endl;
cout<<"Number of Characters (Excluding Spaces)"<<i - NumberWords <<endl;
}

void FindReplace(char Memory[], string FindWord, string ReplaceWord)
{
int i = 0;
int SizeWord = FindWord.length();
int SizeReplace = ReplaceWord.length();
while ( Memory[i] != '\0')
{
for(int i=0; i<Memory[i]!='\0'; i++) {
for(int j=0; j<SizeWord; j++){
if( Memory[j] == FindWord[j] ){
FindWord[j] = ReplaceWord[j];
}
}
}

}

i++;
}


int main ()
{

const int MAXNUMCHARS = 100000;
char Memory[MAXNUMCHARS];

// string Memory;
ifstream InFile ("InputData.txt");
ofstream Out("OutputData.txt");

ReadtoMemory(InFile, Memory) ;

DocumentStatisticsPrint(Memory);

string FindWord = "the";
string ReplaceWord = "can";

FindReplace(Memory, FindWord, ReplaceWord);

OutputMemoryToFile(Memory, Out);

InFile.close();

Out.close();


cout<<endl<<endl;
system ("pause");
return 0;

}
Instead of a char array, if you declare your Memory as a std::string type, then you can easily use std::string::insert and std::string::erase that will do the jobs for you.

I'm not sure if it works for a char array as well.
As I have understood correctly your question then to delete a string from a character array you should find this string in the character array with using standard C function strstr and then shift left all characters after the string.
If you want to remove the whole string then simply set the first element of the array to value '\0'.

To insert a string in a character array you should shift right all characters strating from the position where you wanted insert a string and when simply copy the string starting from this position.

An example gow to delete a string

1
2
3
4
5
6
7
8
9
10
char sourse[] = "Hello Robert Lafore";
char target[] = "Robert";

char *p = std::strstr( sourse, target );

if ( p != NULL )
{
   size_t n = std::strlen( target );
   std::strcpy( p, p + n );
}
Last edited on
Topic archived. No new replies allowed.