By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,983 Members | 2,082 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,983 IT Pros & Developers. It's quick & easy.

Counting the number of times a word occurs in a 2-D array of strings in C++

P: 4
I want to write a function in C++ to accept a 2D array of STRINGS and a word then count the number of times the word occurs in a string.
Tried different codes but end up with too many errors! Am I supposed to store the 2D array in a 1D array word by word (cant figure out how) or check for the word line by line?
Feb 7 '16 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 5K+
P: 9,197
So first you have to write a dictionary where you take a word parsed off the string and look it up in the dictionary. If it's not there, add it. If it is there increment a counter that's part of the dictionary entry.

Next write a process to pick a word from a string. Every time you call this process is picks off the next word. Finally, when all the words a parsed the process signals "no more words".

Get your code to work with one string. Just display the words parsed from the string.

Then add leading and trailing spaces and verify the code still works.

The use a null string and verify the code still works.

Not until here would I attempt to use the dictionary.

Finally, there are no 2D arrays in C or C++. There are only one dimensional arrays. Before you say anything, read this:

Post again and let me know how you are doing.
Feb 7 '16 #2

P: 4
sorry i didnt mention it before but the user enters the word (so i dont need a dictionary)
getting the code in 1 string is where im going wrong this is what im ending up doing all the time...

gets(a) //a is an array
for(int i=0;i<10;++i)
for(int j=0;j<10;++j)
b[i][j]=a[i]; /*trying to store it in many lines but this might be where im going wrong*/
cout<<b[i][j]<<endl; //to display words in many lines
gets(c) //word to be found
if(strcmp(c, b[i][j])==0)


not entirely sure if this is right, new to c++
Feb 8 '16 #3

Expert Mod 5K+
P: 9,197
I suggest you stop coding and instead design the program first. Then code it.

Maybe something like:

1) write a loop that parses a string into words

2) modify the loop to display each word as it is parsed

3) replace the display of the word with a function call using the word as an argument. Do the display inside the function.

4) replace the display of the word inside the function with a call to second function which will now do the display.

5) in the second function store the word in a table which has the word and a count. Each time the word is stored, the count increments.

6) write a third function to display this table showing the word and the count.

You have to break up the logic into functional blocks or else you end up with a huge mess of code that never works correctly. In a step approach you verify the program works at the end of each step. This way if the program fails to work the problem will be in the code added last.

Only when all this works do you add code for the user to input data.

Keep me posted.
Feb 9 '16 #4

Post your reply

Sign in to post your reply or Sign up for a free account.