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.
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];
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 );
}