Hi,
I wrote a simple program that counts the letters in the words from a sentence.
The program works, but what i was wondering is :
did i use the correct approach to pass the pointers to the function bereken. Also with a pointer to an array, do you have to de-reference this pointer (in the function bereken) ? I just used the name without de-referencing, this seems to work.
Thanks.
Code:
#include <iostream.h>
#include <string.h>
void bereken(char *zinp, int *letters);
void main()
{
char zin[80];
int letters[10];
int i;
//clrscr();
cout << "Geef een nieuwe regel: ";
cin.getline(zin,80);
cout << "lengte zin : " << strlen(zin) << endl;
// initialiseren van de array
for(i=0; i<10; i++)
letters[i]=0;
bereken(zin, letters);
for (i=0; i<10; i++)
{
cout << "Aantal woorden met " << i+1 << " letters: " << letters[i] << endl;
}
}
void bereken(char *zinp, int *letters)
{
int j, letters2;
letters2=0;
j=0;
do
{
if ((zinp[j] != ' ') && (zinp[j] != '.'))
letters2+=1;
else
{
letters[letters2-1]+=1;
letters2=0;
}
j++;
} while (zinp[j] != '.');
// laatste waarde ook nog in tabel zetten
letters[letters2-1]+=1;
}