comparing strings

Write your question here.
How can I compare these two strings and count when the strings are the same? It prints 10 all the time
[]
Put the code you need help with here.
[
#include <iostream>
#include <string>
#include <cmath>
//#include <cstdlib>
using namespace std;

void readtosentinel (string a[],int capacity,
int &size, string sentinel);
int occur (string myArray[], int mass, string array[], int size );
/* Reads doubles into the array a. Ends if the sentinel
value is read, or if the capacity of the array is reached.
Size is set to the number of elements read.
*/


int main () {
int MAX_SIZE=1000;
string text[MAX_SIZE];
int count = 0;
int NUMTERMS = 10;
string sentinel = "XXX";


string terms[NUMTERMS] = {"array", "compute", "declare", "define", "for", "function", "if", "loop", "variable", "while" };
cout << "Please enter up to " << MAX_SIZE;
cout<< " words, terminated with " << sentinel << ":";
readtosentinel (text, MAX_SIZE, count, sentinel);

int counter[NUMTERMS];
for (int i = 0; i < NUMTERMS; i++){
cout << terms[i];
counter[i] = occur(text, count, terms, NUMTERMS);
// counter[i] = value;
cout<<counter[i]<< endl;
}



return 0;
}


void readtosentinel (string a[],int capacity, int &size, string sentinel){

string x;
cin >> x;
while (size < capacity && x != sentinel)
{
a[size] = x;
size = size+1;
cin >> x;

}


}

int occur (string myArray[], int mass, string array[], int size){


int count = 0;
for (int j=0; j< size; j++)
{



for (int i = 0; i<mass; i++)
{


if (array[j] == myArray[i])
count++;
}
return count;
count = 0;
}

}




]

below is the text that will be read in:


1E3 is concerned with loop array function variable and so on
the first exam question will test loop
the next two will test function the last one array
array is a set of variables of same type
use for loop to process array
array function takes in array and size
function can return a value or function can print or update array element
variable stores value of size of array
loop allows you to do same thing many times
while loop for going until you notice you reach the end
for loop for doing something a predictable number of times
loop control variable often i is used to control the for loop
if statement allows branching one for true and one for false
boolean conditions are used in if statement while loop
bool variable often called a flag
up till now we have used array to deal with problems of known size
Often we want to use array but we dont know in advance how many students employees words there will be
XXX
Last edited on
What should the occur() do?

The way you do use it:
1
2
3
4
5
6
int counter[NUMTERMS];
for ( int i = 0; i < NUMTERMS; i++ ){
  cout << terms[i];
  counter[i] = occur( /*params*/ );
  cout<<counter[i]<< endl;
}

Hints that on a call to occur() you would count how many times single word terms[i] appears in text.

The code of the function does something different.


PS. as long as you don't need the counters later, you could simplify the usage loop into:
1
2
3
for ( int i = 0; i < NUMTERMS; i++ ){
  cout << terms[i] << ' ' << occur( /*params*/ ) << '\n';
}
Last edited on
Thanks for the reply,
the occur() function counts how many times the hard-coded words from terms array appears in text array and returns it. I understand the simplification code, however the entire codes from main down does not count the actual number of the individual strings.

this is the terminal view :
array10
compute10
declare10
define10
for10
function10
if10
loop10
variable10
while10

but in reality, each of those words do not appear 10 times in the text. Only "array" appears 10 times
times the hard-coded words from terms array appears in text and returns it
it. One number. Sum of all individual occurrences.

Hardcoding is restrictive.


Consider a function that counts how many times one word occurs in array text. Just one word.
(The C++ Standard Library already has such function, but it is better for learning if you can come up with your version.)

What does such function need as arguments?
* The array
* Size of array
* The word
I have the array, the size of the array and the word in the occur function.
In this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int occur( string myArray[], int mass, string array[], int size )
{
  int count = 0;
  for (int j=0; j< size; j++)
  {
    for (int i = 0; i<mass; i++)
    {
      if (array[j] == myArray[i])
        count++;
    }
    return count;
    count = 0;
  }
}

You don't have the word. You have 'array' of 'size' words.

Btw, code tags are nice. Really nice. See http://www.cplusplus.com/articles/jEywvCM9/

Now that we look at the function we see the return statement on line 11. That renders some lines inaccessible.

Here is a bit shorter version that does exactly the same as yours:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int occur( string myArray[], int mass, string array[], int size )
{
  int count = 0;
  if ( 0 < size )
  {
    for (int i = 0; i<mass; i++)
    {
      if (array[0] == myArray[i])
        count++;
    }
    return count;
  }
  // what do we return here? Compiler should warn about this.
}

That should make it clear, why you do get count for "array".
Thank you for your replies. I have figured out the source code, apparently c++ cannot compare two different arrays simultaneously. One must check the values individually.

Below is the source code:


#include <iostream>
#include <string>
#include <cmath>
//#include <cstdlib>
using namespace std;

void readtosentinel (string a[],int capacity,
int &size, string sentinel);
int occur (string word, string array[], int mass );
/* Reads doubles into the array a. Ends if the sentinel
value is read, or if the capacity of the array is reached.
Size is set to the number of elements read.
*/
const int MAX_SIZE = 1000;
const int NUMTERMS = 10;


int main () {
string text[MAX_SIZE];
int count = 0;
string sentinel = "XXX";


string terms[NUMTERMS] = {"array", "compute", "declare", "define", "for", "function", "if", "loop", "variable", "while" };
cout << "Please enter up to " << MAX_SIZE;
cout<< " words, terminated with " << sentinel << ":";
readtosentinel (text, MAX_SIZE, count, sentinel);

int counter[NUMTERMS];

int value;

for (int i = 0; i < NUMTERMS; i++)
{

value = occur(terms[i], text, MAX_SIZE);
counter[i] = value;

}

for (int i = 0; i < NUMTERMS; i++) {

cout << terms[i] << " " << counter[i] << endl;
}
return 0;
}


void readtosentinel (string a[],int capacity, int &size, string sentinel){

string x;
cin >> x;
while (size < capacity && x != sentinel)
{
a[size] = x;
size = size+1;
cin >> x;

}


}

int occur (string word, string array[], int mass){


int count = 0;
for (int j=0; j< mass; j++)
{
if (array[j] == word)
count++;

}
return count;
}



* run the function and into the terminal, paste that text which I posted earlier in the forum.



Topic archived. No new replies allowed.