An array of chars

Pages: 123
closed account (23q2T05o)
Hi, so I have a task that I have to read from the console and array of chars with maximal length 1 MB, the array of chars can be on several lines. So idea is that the array of chars is an array of many many words and it looks like this:
Once upon a time there were three happy little pigs.# (it has to end with #)
I have to count the words in the array, count how many different words I have in the array, if I have a word that is repeated I have to say how many times I had this word in the text and then in the end I have to sort the words in alphabetical way and print them.
So Idk what does 1MB convert to, Idk if I have to use char array[MAX_SIZE] or char* array (MAX_SIZE=1MB) and IDK about the other things too. Thanks in advance
Also I can not use string.h!


So I started like this. idk what the MAX_SIZE=?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main() {
	char array [MAX_SIZE];

	cout << "Please enter the text and in the end place a #: " << endl;
	cin.getline(array, MAX_SIZE);

	cout << "The text that you entered like: " << endl;
	cout << array << endl;

	int counter = 0;
	int size = 0;
	size = strlen(array) + 1;

	for (size_t i = 0; i < size; i++)
	{
		if (array[i] == ' ' || array[i]=='#') {
			counter++;
		}
	}

	cout << "The number of words is: " << counter;

	return 0;
}
Last edited on
std::map<string,int>
closed account (23q2T05o)
can't use it
can you use a pointer or do you have to use an array, and if so, does your platform support an array this large on the stack? Do you also need to code it while riding a unicycle and juggling loaded uzis? Anything else you are not telling us?

1mb is 2 to the 20th power. Its a wee bit more than 1 million.
Last edited on
closed account (23q2T05o)
it doesnt way if we need a pointer or not, the user inputs a text and we have to do the following stuff.
can't use it

Why on earth not?

What is your alternative way of storing words and their frequencies?
closed account (23q2T05o)
soo this is a task for uni and we havent studied it so i cant use it
can not use string.h
That means you can't use strlen...

Have you learned trees in your uni class? Or do you know that you're expected to use an array?

the array of chars can be on several lines.
You should use getline(array, MAX_SIZE, '#') then.

But even so, that array is going "unparsed" for your purposes. You need to then parse that array for words, and you'll need a tree or array of {word, frequency} pairs (I guess {char*, int}) to keep track of the words and their frequencies.
Last edited on
closed account (23q2T05o)
we can use strlen strcpy and all of them
closed account (23q2T05o)
we cant use the string class maybe that is what i meant sorry
closed account (23q2T05o)
okay so I know how to count the words that we have but now idk how to sort the words in alphabetical way and how to know if we have a word twice or more than twice
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void read_word_from_the_array(char* array) {
	int counter = 0;
	int size = 0;
	size = strlen(array) + 1;

	char temp[64];
	for (size_t i = 0; i < size; i++)
	{
		for (size_t j = 0; j < size; j++)
		{
			if (strcmp(array[j - 1], array[j]) > 0)
			{
				strcpy(temp, array[j - 1]);
				strcpy(array[j - 1], array[j]);
				strcpy(array[j], temp);
			}
		}
	}
}


that is what i wrote for the sort but there are some errors
char temp is 65 because one word can not be longer than 64 chars
Last edited on
Did your lecturer say that you can't use the string class, or is that your interpretation of it?

Maybe you should list what you CAN use. Especially to store an arbitrary number of arbitrary-length words.
Solve an easier sub-problem first.

Given a char array like:
char arr[] = "These\nare some words.\n Blah happy blah. More words. Happy little pigs.
print each word on its own line, all lowercase, without punctuation.
Last edited on
closed account (23q2T05o)
Our lecturer said we cant use the string class.
That's fine. You can get away with using char arrays (and dynamic char arrays).
The harder part is going to be counting the word frequencies. Unless you use some sort of tree or hash table, it's going to be very inefficient.

Have you talked about trees or other data structures in your uni?

First, just focus on the smaller problem of printing each word from the arr on its own.
Last edited on
closed account (23q2T05o)
Okayy but I have some errors in the code and I am not sure if the things I have wrote are correct and if they will do the work that they gotta do. So can you give me a piece of advice how to continiue with sorting the words in alphabetical way and say if some of the words are repeated and how many times?
Show us your attempt at parsing the array and extracting each separate word.

<OP edited previous posts before this one, which previously did not have code in them>
Last edited on
closed account (23q2T05o)
So this is what I have written:

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
49
50
#include <iostream>
using namespace std;

const int MAX_SIZE = 100;
const int MAX_SIZE_WORD = 65;
void read_word_from_the_array(char* array);
void read_word_from_the_array(char* array) {
	int counter = 0;
	int size = 0;
	size = strlen(array) + 1;

	char temp[MAX_SIZE_WORD];
	for (size_t i = 0; i < size; i++)
	{
		for (size_t j = 0; j < size; j++)
		{
			if (strcmp(array[j - 1], array[j]) > 0)
			{
				strcpy(temp, array[j - 1]);
				strcpy(array[j - 1], array[j]);
				strcpy(array[j], temp);
			}
		}
	}
}

int main() {
	char array [MAX_SIZE];

	cout << "Please enter the text and in the end place a #: " << endl;
	cin.getline(array, MAX_SIZE);

	cout << "The text that you entered like: " << endl;
	cout << array << endl;

	int counter = 0;
	int size = 0;
	size = strlen(array) + 1;

	for (size_t i = 0; i < size; i++)
	{
		if (array[i] == ' ' || array[i]=='#') {
			counter++;
		}
	}

	cout << "The number of words is: " << counter;

	return 0;
}
this is the first step ... read the data.

1
2
3
4
5
6
7
8
9
10
const unsigned int MB = 1048577;
int main()
{
 char big[MB];
 cin.getline(big,MB,'#');
 int fullsize = cin.gcount();
 for(int i = 0; i < fullsize; i++)
	 cout << big[i];
	
}


from here you can slice it up by whitespace, end of lines and spaces. you can replace the whitespace with '\0' and take a new array of pointers to the start of each word and that gives you a list word-wise. Now you need to re-create strcmp so you can sort the data, and then if you can't use sort() you need to write a sort routine using your compare. Once it is sorted, THEN you can check for duplicates by the simple 'is this one same as next one when sorted' dumb counter loop.
so counting the words and the duplictes are the easy part, splitting it up will be new to you but is not too hard, and sorting it if you have to write your own the hardest piece. You don't have to use pointers to split it, you can use array index if you prefer, its the same end result.

If any of that does not make sense I can go into more details later. But I won't write it all. Get some code down, try to do these things, post the code and ask a question if you get stuck. Doing it my way, you only need 1 string.h routine: strcmp () which you can re-create easily and even improve it to not care about case of the letters.

--?? requirement?? I added +1 to MB to account for the # symbol / end of string .. just in case. I can't see someone typing a MB to the console but your prof may redirect a large text file into your program to test it.

the for loop just echos what was typed so you can play with it a bit.
Last edited on
closed account (23q2T05o)
so you said array of pointers, so I have to use char**?
Pages: 123