to count no. of a particular word in aline


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
using namespace std;

// Write a function to display a count of the number 
// of times the word "duck" appears in an array of strings.

//Prototype
int getDuckCount (string word[], int arraySize);

int main ( )
{
// Declaration
int count;
string line[100];
int size = 100;

// Input
cout << "Enter some lines to find a count of how many times"
<< " the word duck appears. Hit enter on a blank line to terminate." << endl;
for (int index = 0; ;index++)
{
getline (cin, line[index]);
if (line[index] == "") break;
count = getDuckCount (line[], size);
}

//Output
cout << "The word duck appears " << count << " times. Quack!" << endl;
return 0;
}

int getDuckCount (string word[], int arraySize)
{
int duckCount = 0;
char * duck;

for (int index = 0; ;index++)
{
duck = strstr (word[index], "duck");
if (word[index] == "") break;
if (word[index] == duck) duckCount++;
}
return duckCount;
}

i wrote this code.......it is not working... pls help
At least you should place statement

count = getDuckCount (line[], size);

outside the for loop.

Also function strstr does not accept as its first argument an object of the type string.
Last edited on
thank you...

and any ideas of how to fix it??
Last edited on

any other means to achieve this objective???


please help
Last edited on
Hi macs,

Does it compile? If not post the compiler output.

If so, have you used the debugger in your IDE?

If you don't want to, or are unable to use the debugger, then put lots of couts everywhere to track the values of your variables. This is the same as "watching" in the deugger.

Find out what the getline function does and what value it returns.

Are size and arraySize actually used for anything?

Be really careful with for loops with no end condition - might be better with a while loop.

Also there are advantages in what names you use for variables, it might not seem to be a problem now, but it can save you when you get to longer more complex programs. Could line[] be better as LineArray[], and would word[] be better as WordsArray[] or WordsList. line[] and word[] imply that there is only one of them, and could be confusing. They are not technically lists, but you need a name that better describes what the variable holds. Perhaps index could be LineCounter.

IS this the correct use of the duck variable?
 
if (word[index] == duck) duckCount++;


Finally don't use:

 
using namespace std;


IF you are using only cout, cin and string, then do this instead:

1
2
3
4
using std::cout;
using std::cin;
using std::string;


Let us know how you get on.

Cheers TheIdeasMan
You could use std::string::find http://www.cplusplus.com/reference/string/string/find/ or std::search http://www.cplusplus.com/reference/algorithm/search/ instead of strstr.

If you really want to use strstr you have to convert from std::string to a c string. To do that you can use std::string::c_str http://www.cplusplus.com/reference/string/string/c_str/
Last edited on
strstr will not give you a correct result because it can return a pointer to a subword of another word but you need count words not subwords.

I could write your assignment in one statement with standard algorithms but I am affraid you do not know them.
Last edited on
thank u guys...
"strstr will not give you a correct result because it can return a pointer a subword of another word but you need count words not subwords.

I could write your assignment in one statement with standard algorithms but I am affraid you do not know them."

please write ur code I'll try to learn them.... waiting for reply
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{       char line[100], wd[30][30],ch[40];
      int l,i,j=0,w=0,k=0;
clrscr();
 printf("ENTER  A LINE \n");
 gets(line);
  l= strlen(line);
for(i=0;i<l;i++)
{
if(line[i] != ' ')
{	   wd[j][k]=line[i];
 k++;
}
else
{	   wd[j][k]='\0';
	   j++;
 k=0;
}
     }
printf("enter word to be counted\n");
   scanf("%s" ,ch);
  for (i=0;i<j;i++)
  {
printf("%s\n",wd[i]);
	if (strcmp(wd[i],ch)==0) w++;
 }
printf("The total number words %d\n",w);
  getch();}

now I have figured it out in another way....any other shorter methods are welcome...
As I can guess it is not your code becuase the first code you demonstrated was written with C++ and this one is written with C and even the main declared incorrectly.
Topic archived. No new replies allowed.