#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    23
    Rep Power
    0

    Repeat characters from a sequence


    I wrote this code that prints the characters that repeat when the user types a sequnce of characters that ends with '.'.
    The program works just fine but for example if I type abaa it prints 'a' 3 times because character 'a' appears 3 times in the entered sequence.
    What I would like the program to do is to print only ONE TIME all characters that appear more than once in the sequence.
    Here's the code I wrote:
    Code:
    #include <stdio.h>
    int main()
    {
     char s[100], c;
     int n, i, j, repeat=1;
     n=0; // number of characters from the sequence
     do
     {
      scanf("%c", &c);
      if(c!='.') s[n++]=c;     
     } while(c!='.');
     
     for(i=0; i<n; i++)
     {
      repeat=0;
      for(j=0; j<i && !repeat; j++)
      if(s[i]==s[j]) 
       {
       printf("%c", s[i]);
       repeat=1;   
       }      
     }
     getch();
     return 0;
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    45
    Rep Power
    8
    You've almost got it, so here's the correct code:
    Code:
    for (i = 0; i < n; ++i)
    {
        repeat = 0;
        for (j = 0; j < i; ++j)
        {
            if (s[i] == s[j]) 
            {
                repeat = 1;
                break;
           }
        }
        if (!repeat)
        {
            printf("%c", s[i]);
        }
    }

IMN logo majestic logo threadwatch logo seochat tools logo