anybody know how to remove the repeated characters///
The output is like this..

string:aaaaanffddddgjkh

result:afdgkh

P.S I am using c++ on this type of program

Are you just removing repeated characters, or truly removing duplicates? For example, what would the output be for "aaabaaa"? If it's the former, you can use std::unique quite easily to do the job:

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s("aaaaanffddddgjkh");
    
    s.erase(unique(s.begin(), s.end()), s.end());
    
    cout << "'" << s << "'\n";
}

may I know what is another code without using std::unique? or .erase()??

it is just removing repeated characters

may I know what is another code without using std::unique? or .erase()??

Why do you ask?

>>Why do you ask?

Because it is a homework problem in which the prof said not to use STL algorithms, I guess. This is a typical homework problem to teach people how to do basic string or array manipulations. Ironically, it is also a typical interview question to see if people know STL algorithms well, and can avoid implementing trivial algorithms when they are already available in standard libraries.

@OP:
Did you try anything? Post some code and we can start from there. If you just need a solution to the problem, you would be happy with Narue's code. If you need a solution to the problem without STL, then somebody wants you to learn something, we want you to learn something, you should want to learn something, and thus, you should try to solve it yourself. If you need inspiration, try to just read on how std::unique works.

Because it is a homework problem in which the prof said not to use STL algorithms, I guess.

That's my suspicion too, which is why I asked (somewhat rhetorically). ;)

//my prog_final

#include <iostream>
#include <conio.h>
using namespace std;

int main(){
  char s1[20];
 char s2[20];
 int x=0;
 //ask input for the user
 std::cout<<"maximum size is 20\n";
   std:: cout<<"string: ";
    std::cin>>s1;
   //count characters
   
   while(s1[x]!='\0')//to count string in count1
            {
            x++;
            }
    
    //delete repeated char on first element
    
  for(int i=0;i<x-1;i++){
if(s1[i]==s1[i])
std::cout<<s1[i+1];

else
std::cout<<"result"<<s1[i];
}

    
     getch();
 return 0;   
}

output:

string: aaabbdc
result: aabbdc

I wonder why my program only remove on the first duplicate.

Your program doesn't remove anything :P It just outputs the string omitting the first letter.
E.g., entering this -> aaabbbddccc will cause your program to output this -> aabbbddccc

Can you describe in english how you would remove duplicates from a string?

If I give you this string -> aaabbbddccc# and tell you that you can only access two consecutive letters at a time
and the only thing you can do after you see the letters is decide to either print or not print a letter, what will you do?

aaabbbddccc

I will decide to print the letter.

Can you describe in english how you would remove duplicates from a string?

The string I wanted to remove is the repeated strings. I want my strings occur once when there is repeated strings . I am trying to make a program without using predefined functions.

Ok, I'll try to be more specific.

First, you see these two letters -> (aa)abbbddccc#
Then, you see these two -> a(aa)bbbddccc#
Then, you see these two -> aa(ab)bbddccc#
Then, you see -> aaa(bb)bddccc#

etc ...

I want you to tell me what you would do in each
case. Will you print a letter or not? And if you
print a letter, will it be the left or the right one?

Remember, I want the output to be this -> abdc

I will not print each case.

If you don't print anything how will you get abdc???

I'll try one last time.

(aa)abbbddccc# print 'a' or don't print?

a(aa)bbbddccc# print 'a' or don't print?

aa(ab)bbddccc# print 'a', print 'b' or don't print?

aaa(bb)bddccc# print 'b' or don't print?

aaab(bb)ddccc# print 'b' or don't print?

aaabb(bd)dccc# print 'b', print 'd' or don't print?

aaabbb(dd)ccc# print 'd' or don't print?

aaabbbd(dc)cc# print 'd', print 'c' or don't print?

aaabbbdd(cc)c# print 'c' or don't print?

aaabbbddc(cc)# print 'c' or don't print?

aaabbbddcc(c#) print 'c', print '#' or don't print?

I want you to give me an answer on every question above.
And remember, I want the final output to be this -> abdc

commented: Some people are lost causes. Props for being optimistic. +17

(aa)abbbddccc# print 'a' or don't print?->don't print

a(aa)bbbddccc# print 'a' or don't print?->print

aa(ab)bbddccc# print 'a', print 'b' or don't print?-> print b

aaa(bb)bddccc# print 'b' or don't print?don't print

aaab(bb)ddccc# print 'b' or don't print?don't print

aaabb(bd)dccc# print 'b', print 'd' or don't print?print d

aaabbb(dd)ccc# print 'd' or don't print?don't print

aaabbbd(dc)cc# print 'd', print 'c' or don't print?print c

aaabbbdd(cc)c# print 'c' or don't print?don't print

aaabbbddc(cc)# print 'c' or don't print?don't print

aaabbbddcc(c#) print 'c', print '#' or don't print?don't print

Yes, it's a very good start. I like the fact that you noticed that you should print when the letters are different.
Also, even though I like the reputation I got from Narue, I'd like it even more if we could prove her wrong :P
I would do it in a slightly different way, so that I only need to print when the letters are different:

(aa)abbbddccc# don't print

a(aa)bbbddccc# don't print

aa(ab)bbddccc# print 'a'

aaa(bb)bddccc# don't print

aaab(bb)ddccc# don't print

aaabb(bd)dccc# print 'b'

aaabbb(dd)ccc# don't print

aaabbbd(dc)cc# print 'd'

aaabbbdd(cc)c# don't print

aaabbbddc(cc)# don't print

aaabbbddcc(c#) print 'c'

Now, let's try to write a C++ program that does this.

#include <iostream>
using namespace std;

int main()
{
    char my_string[] = "aaabbbddccc#";

    char current_letter;
    char next_letter;

    int i;

    for ( /* ... */ )
    {
        current_letter = //...
        next_letter = //...

        if ( /* current letter is different than next letter */ )
        {
            cout << current_letter;
        }
    }

    return 0;
}

See if you can fill in the missing code to make this work.

ok..tnx for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.