#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
void main (void)
{
int size [25] = {0};
int current =0;
char end = 0;
do
{
char sent[] = "My name is geraint and I live in a house";
while (sent != '\0')
{
if ((sent == ' ') || (sent == '.') || (sent == ','))
{
size[current]++;
current = 0;
}
else
{
current++;
}
sent
}
for (int i=0; i<=24; i++)
{
printf ("length %2i: %i occurences\n" , i,size[i]);
}
printf (" Would you like to start again? \n");
scanf ("%c", &end);
}
while (end == 'y');
printf("\n");
system ("pause");
}
sent++ will not work because there is no operator++ for arrays.
You probably need one more variable so you can keep track of both the current letter in the string and the length of the current word. Also be careful so you don't miss to count the last word.
Tried it too. results in "5" which is the number of spaces in the string before " " (the space).
jargon.find(" "!=string::npos)
returns the position immediately preceding what you have in the quotations
If you don't know the full dynamics of that, please let me know and I'll try to work it out to show how you could do it word for word with your example sentence.
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
int main ()
{
int size [25] = {0};
int current =0;
char end = 0;
char *sent = "My name is geraint and I live in a house.\n\n";
printf("%s", sent);
if (sent!= "\0")
{
if ((sent == " ") || (sent == ".") || (sent == ","))
{
size[current]++;
current = 0;
}
else
{
current++;
}
sent;
}
for (int i=0; i<=24; i++)
{
printf ("length %2i: %i occurences\n" , i,size[i]);
}
printf("\n");
system ("pause");
}
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
int main ()
{
int size [25] = {0};
int current =0;
char end = 0;
char *sent = "My name is geraint and I live in a house.";
printf("%s", sent);
while (*sent != 0)
{
printf("In loop, *sent is %c \n", *sent );
if ((*sent == ' ') || (*sent == '.') || (*sent == ','))
{
size[current]++;
current = 0;
}
else
{
current++;
}
sent++;
}
for (int i=0; i<=24; i++)
{
printf ("length %2i: %i occurences\n" , i,size[i]);
}
printf("\n");
system ("pause");
}
I recommend you stop using char arrays and use proper C++ strings. Similarly, stop using printf and use cout instead.
THANK YOU! Works great. I am currently learning cout and c++, so hopefully can drop all the old char and printf stuff soon, but not very confident yet.