Repeat Character Array

I'm having a little trouble writing program that on remove all repeat characters
and leave only the non repeated characters. This is want I have so far. Thank in advance.

#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" ;
}
I'm having a little trouble writing program that on remove all repeat characters
and leave only the non repeated characters.


Your code appears to be trying to count the number of repeated characters, which is different from what you describe the goal as. Perhaps you could provide the expected output for the following input:

aaa
aba
abba
abab
You are close. Besides unique, you also need to count the number of times each character in unique appears in the input string.

Hello world

H e l o   w r d
1 1 3 2 1 1 1 1

Now eliminate all those characters which have a count != 1.

He wrd


This will take a little ingenuity, but I think you can get there. Post back if you get stuck again.

Hope this helps.
you could use std::sort and std::unique to get rid of those repeat characters
and count how many repeat characters with a simple algorithm like


pseudo codes

1
2
3
int chars_nums[26];

++nums[s[i] + 127];


Last edited on
This is what I'm trying to accomplish below.

When inputting:
Only three more lessons to go after this one!

Output should only show:
Only thremosgafi!
You have that in the unique variable that is generated in repeatingCharCount if you change the input method in main to use getline rather than the extraction operator.

Two lines of code are changed in the following:

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
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] ;
    }
 
    std::cout << unique << '\n' ;	// ** change here **
 
    return length - unique.length() ;
}
 
int main()
{
    char buffer[1000] ; // ick.
 
    std::cout << "Enter a string\n" ;
    std::cin.getline(buffer, 1000) ;	// ** change here **
    // std::cin >> buffer ;
 
    std::cout << '"' << buffer << "\" has "
        << repeatingCharCount(buffer) << " repeats.\n" ;
} 


http://ideone.com/JI7SWi
Thank you cire it worked.
Topic archived. No new replies allowed.