Letter Counter

Im trying to create a program that looks at each word and outputs their length.

Length 1: 2 occurrences
Length 2: 3 occurrences
etc.

My codes not working and need some help

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
46
47
48

#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");

}
void main (void)
If your compiler accepts this, throw it away and get a C++ compiler. It's wrong.
int main() is correct.

1
2
3
	}
		sent
	}


What is that sent doing there? I'm guessing it's meant to be sent++ ?
Last edited on
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.
sent++ will not work because there is no operator++ for arrays.


Oops. That's my mistake. I never use "array" variables, only pointers, so I didn't even notice that. Changing

char sent[] = "My name is geraint and I live in a house";

to

char* sent = "My name is geraint and I live in a house";

will fix that up, but maybe the OP prefers the former.
We all have our own "solutions". Here's mine:

make the words a string:

1
2
#include <string>
std::string jargon;

"jargon" is variable that you can call whatever you want.
example:

1
2
3
4
5
6
7
8
9
10
11
#include <string>

jargon = "apple ";
if (jargon.find(" ")!=string::npos)



{
	cout << jargon.find(" ");
}


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.




Thanks for all your help so far. Dalydir I dont understand your method sorry, Im still in early learning stage and unsure how to do your method sorry.

I have edited my code a little, and its running but its outputting all the occurences is 0.

Length 1: 0 occurences
Length 2: 0 occurences

Am I correct in thinking that

if sent!= \0 that it means end of string?

Here is my current code:

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
#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");

}

if (sent!= "\0")
Presumably this is meant to be the start of a loop. if is not a loop construct. This will happen only once. Try while.

sent;
That doesn't do anything.

Try something like this (with extra output to help you see what it's doing):

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
#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.
Last edited on
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.

Thanks again.
If you're just starting, definitely don't start with printf and char arrays. They are more difficult than the C++ string and cout.
Topic archived. No new replies allowed.