Trouble with strings

Hello, im having a problem with a program. Im supposed to write a code where a user inputs a word (doesnt need to be a real word) and i need to make a function(preferably bool) where it says whether or not the word has the same amount of letters (for example poop , irerir, olosls , etc ) and words that could have the same amount of words if you remove one letter (im assuming with cin.get() ). However im not allowed to use strings. Could anyone help ?
And how does your instructor want you to input/store a "word" without using "strings"? What concepts have you learned in class so far?

Can you show us what you've tried?
Last edited on
I couldn't even comprehend what im supposed to write , but as far as i know only char arrays are allowed (no vectors)
Just focus on one part at a time.

Can you show us this:
write a code where a user inputs a word

Just focus on this. Do you know how to let the user enter a word? Write a program that let's a user enter a word, and then display that word back to them.

This site has a tutorial section, and there are other sites as well that are helpful and you can search for, like learncpp or geeksforgeeks.
https://cplusplus.com/doc/tutorial/
https://cplusplus.com/doc/tutorial/basic_io/
For the first part consider:

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
#include <iostream>
#include <cctype>
#include <iomanip>

bool issame(const char* str)
{
	char let[26] {};

	for (; *str; ++str)
		if (std::isalpha(*str))
			++let[std::tolower(*str) - 'a'];

	int same {};

	for (int l = 0; l < 26; ++l)
		if (let[l] != 0)
			if (same == 0)
				same = let[l];
			else
				if (same != let[l])
					return false;

	return true;
}

int main()
{
	const size_t MAX {20};
	char word[MAX] {};

	std::cout << "Enter word: ";
	std::cin.getline(word, MAX);

	std::cout << std::boolalpha << issame(word) << '\n';
}


Note that in your example given irerir doesn't work as it has only 1 e when there are 2 of the other letters - assuming I'm reading the req right.


Enter word: olosls
true

Enter word: irerir
false


Last edited on
For the second part, consider:

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
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <cctype>
#include <iomanip>
#include <vector>

char issame(const char* str)
{
	char let[26] {};
	size_t ln {};

	for (; *str; ++str, ++ln)
		if (std::isalpha(*str))
			++let[std::tolower(*str) - 'a'];

	std::vector<int> dist(ln + 1);

	for (size_t l = 0; l < 26; ++l)
		if (let[l])
			++dist[let[l]];

	size_t mxelm {};
	size_t mxnum {};

	for (size_t l = 1; l <= ln; ++l)
		if (dist[l] > mxnum)
			mxnum = dist[mxelm = l];

	if (dist[mxelm] * mxelm == ln)
		return 1;

	size_t fnd {};

	if (dist[1] == 1 && dist[mxelm] * mxelm + 1 == ln)
		fnd = 1;
	else
		if (mxelm < ln && dist[mxelm + 1] == 1 && dist[mxelm] * mxelm + dist[mxelm + 1] * (mxelm + 1) == ln)
			fnd = mxelm + 1;

	if (fnd != 0)
		for (size_t l = 0; l < 26; ++l)
			if (let[l] == fnd)
				return l + 'a';

	return 0;
}

int main()
{
	const size_t MAX {20};
	char word[MAX] {};

	std::cout << "Enter word: ";
	std::cin.getline(word, MAX);

	const char rem {issame(word)};

	if (rem < 2)
		std::cout << std::boolalpha << (bool)rem << '\n';
	else
		std::cout << "false - remove " << issame(word) << '\n';
}



Enter word: pool
false - remove o

Enter word: polly
false - remove l

Enter word: qaqssak
false - remove k

Enter word: pooll
false - remove p

Enter word: qweqweqw
false


Last edited on
Topic archived. No new replies allowed.