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 ?
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.
#include <iostream>
#include <cctype>
#include <iomanip>
#include <vector>
char issame(constchar* 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;
elseif (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);
constchar 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