Repeating character in c++?

I'm writing a code which tells the user how many times a word they input can be written as. I know that the general "equation" one could call it is:
Let's say x = number of letters in the word, y = number of repeating letters;
x! - y! = answer
I managed to get the factorial part working, but I cannot seem to figure out how to see if a word has repeating letters, hopefully you guys can help!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string.h>
using namespace std;
int factorial(int n); //prototyping the factorial function
int main()
{
    while (1) {
    char word[5000]; //I just entered a random value here
    int wordLength;
    cout << "Enter a word: ";
    cin >> word;
    wordLength = strlen(word);
    cout << "The word " << "'" << word << "'" << " can be rearranged " << factorial(wordLength) << " times uniquely" << endl;
    return 0;
}
}
int factorial(int n){ //factorial function
if (n== 1){
    return 1;
   } else {
        return n*factorial(n-1);
    }}


Thanks!
Last edited on
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
#include <iostream>
#include <cstring>
#include <string>

unsigned repeatingCharCount(char const* s)
{
    std::string unique ;

    const unsigned length = std::strlen(s) ;

    for ( unsigned i = 0; i<length; ++i )
    {
        if ( std::string::npos == unique.find(s[i]) )
            unique += s[i] ;
    }

    return length - unique.length() ;
}

int main()
{
    char buffer[1000] ;  // ick.

    std::cout << "Enter a string\n" ;
    std::cin >> buffer ;

    std::cout << '"' << buffer << "\" has " 
        << repeatingCharCount(buffer) << " repeats.\n" ;
}


http://www.cplusplus.com/reference/string/string/find/

Using std::string here would be preferable to the character array.
Topic archived. No new replies allowed.