Get a Word from a Pointer in C++

In this tutorial, we will understand how to get a word from a pointer in C++. Given a string or a character, it is possible to return the whole word.
Word is a string which is an array of characters. For example, Sam is our string. Sam is an array of characters with ‘S’, ‘a’, ‘m’ and ‘\0’. Every string ends with a null character. A pointer can be used to initialize to a string as well as return a string. We can assume we have a file in our system which needs to be checked for words. Using pointers, this can be achieved.

Let us look at a sample code to get an idea. Explanation of the code follows the code.

Code to get string from pointer

word.cpp

#include <bits/stdc++.h>
using namespace std;
class Word
{
private:
    char *ptr;
    int len;

public:
    Word(const char *word)
    {
        ptr = new char[strlen(word)+1];
        memcpy(ptr,word,strlen(word)+1); 
        len = strlen(word); 
    } 
    ~Word() 
    { 
        ptr=NULL;
        len=0; 
    } 
char* getWord()
{ 
  return ptr; 
} 
};

main.cpp

#include<bits/stdc++.h>
#include<cstring>
#include<istream>
#include<ostream>
#include<fstream>
using namespace std;
const int FILE_PATH_SZ = 512;
Word** wordArray;
int arrSz;
int getSize(ifstream& infile, string filename);
void makearr(ifstream& infile);

int main()
{
ifstream infile;
ofstream outfile;
string filename;
filename = "example.txt";
arrSz = getSize(infile, filename);
cout << "The array size is " << arrSz << endl;
makearr(infile);
return 0;
}

int getSize(ifstream& infile, string filename)
{
string oneItem;
int count = 0;
infile >> oneItem;
while (!infile.eof())
{
infile >> oneItem;
count++;
}
infile.close();
infile.open(filename.c_str());
return count;
}

void makearr(ifstream& infile)
{
string newWord;
wordArray = new Word*[arrSz];
for(int j = 0; j < arrSz; j++)
{
infile >> newWord;
wordArray[j] = new Word(newWord.c_str());
cout << wordArray[j]->GetWord() << ", "; 
}
}

example.txt

I am never at home on Sundays.
When money was tight, he'd get his lunch money from the local wishing well.
Beside the white chickens.
Dan ate the clouds like cotton candy.

The words we have to get can be assumed as Sundays, money, chickens, white, etc. The output would be of the form:

Output:
The array size is 5
Sundays, money, chickens, white

That was a lengthy code!! Not many of you would get it in the first attempt or without any explanation. Here is an explanation for the same.

Code Discussion

  • We have created a class named Word under word.cpp. This has all the required variables for finding the word.
  • Inside the constructor, pointer ptr is assigned to a new character so that the entire word can be returned instead of the first character of the string.
  • memcpy() is used to copy a block of memory from a location to another. A for loop can be used for the same but to make the already big code little compact, we have used memcpy().
  • Coming to the main function, we have to check an example file, “example.txt” for the required words. Thus, we have included ifstream, ostream and fstream headers.
  • Two functions have been defined within main().
  • getSize() returns the size of the array of the words we require. This count is useful in determining the size of the resultant array of words.
  • makearr() makes an array of the required words and returns that array as an output.
  • The array size if 5 with our inputs as the word “money” appears twice in “example.txt”.
  • makearr() returns the result as a pointer, i.e. the addresses of these words have been passes, and using this pointer, by dereferencing, we get the required words.

Leave a Reply

Your email address will not be published. Required fields are marked *