Dec 17, 2020 at 11:50pm Dec 17, 2020 at 11:50pm UTC
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 ?
Dec 18, 2020 at 12:00am Dec 18, 2020 at 12:00am UTC
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 Dec 18, 2020 at 12:01am Dec 18, 2020 at 12:01am UTC
Dec 18, 2020 at 12:07am Dec 18, 2020 at 12:07am UTC
I couldn't even comprehend what im supposed to write , but as far as i know only char arrays are allowed (no vectors)
Dec 18, 2020 at 10:17am Dec 18, 2020 at 10:17am UTC
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 Dec 18, 2020 at 10:17am Dec 18, 2020 at 10:17am UTC
Dec 19, 2020 at 11:16am Dec 19, 2020 at 11:16am UTC
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 Dec 19, 2020 at 11:18am Dec 19, 2020 at 11:18am UTC