Hi, this C program is to remove duplicates. HOwever it doesnt always print out a result and when it does it screws it up sometimes.

# include <stdio.h>
# include "simpio.h"
# include "genlib.h"

main()
{
      int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,word;
      
      word=getchar();
      a='a';
      b='b';
      c='c';
      d='d';
      e='e';
      f='f';
      g='g';
      h='h';
      i='i';
      j='j';
      k='k';
      l='l';
      m='m';
      n='n';
      o='o';
      p='p';
      q='q';
      r='r';
      s='s';
      t='t';
      u='u';
      v='v';
      w='w';
      x='x';
      y='y';
      z='z';
      while((word=getchar())!=EOF)
      {
      if(word==a)
      {
      printf("a");
      a=1234;
      }
else
      if(word==b)
      {
      printf("b");
      b=1234;
      }
else
      if(word==c)
      {
      printf("c");
      c=1234;
      }
else
      if(word==d)
      {
      printf("d");
      d=1234;
      }
else
      if(word==e)
      {
      printf("e");
      e=1234;
      }
else
      if(word==f)
      {
      printf("f");
      f=1234;
      }
else
      if(word==g)
      {
      printf("g");
      g=1234;
      }
else
      if(word==h)
      {
      printf("h");
      h=1234;
      }
else
      if(word==i)
      {
      printf("i");
      i=1234;
      }
else
      if(word==j)
      {
      printf("j");
      j=1234;
      }
else
      if(word==k)
      {
      printf("k");
      k=1234;
      }
else
      if(word==l)
      {
      printf("l");
      l=1234;
      }
else
      if(word==m)
      {
      printf("m");
      m=1234;
      }
else
      if(word==n)
      {
      printf("n");
      n=1234;
      }
else
      if(word==o)
      {
      printf("o");
      o=1234;
      }
else
      if(word==p)
      {
      printf("p");
      p=1234;
      }
else
      if(word==q)
      {
      printf("q");
      q=1234;
      }
else
      if(word==r)
      {
      printf("r");
      r=1234;
      }
else
      if(word==s)
      {
      printf("s");
      s=1234;
      }
else
      if(word==t)
      {
      printf("t");
      t=1234;
      }
else
      if(word==u)
      {
      printf("u");
      u=1234;
      }
else
      if(word==v)
      {
      printf("v");
      v=1234;
      }
else
      if(word==w)
      {
      printf("w");
      w=1234;
      }
else
      if(word==x)
      {
      printf("x");
      x=1234;
      }
else
      if(word==y)
      {
      printf("y");
      y=1234;
      }
else
      if(word==z)
      {
      printf("z");
      z=1234;
      }
      }
      getchar();
}

Recommended Answers

All 2 Replies

Insert at line 123 ...

else
       {
        printf("\nduplicate or unknown character\n");
       }

and see what you get.

Thank god there are only 26 alphabets.
If there were 100 u would need to write 100 if statements.

May be u can take some other approach for your problem such as:
write a function like

void removeDuplicate(char *str);

It doesn't necessary that a user wil input a-z. There are many printable characters available.
Implement the above method. If u wanted to do it in C++ u could have used a map of flags such as

map<char, bool> charMap;

When u get a new char in the input string just make an entry as

charMap[myChar] = true; /*that says u have encountered that char*/

If u want to continue with C only u can still do it using an array like

#define MAX_CHAR 100 //whatever the total no. of characters are available
int charEncountered[MAX_CHAR];
//initialize them to 0 (zero)
//use it as
if(charEncountered[myChar]==0)
{
   charEncountered[myChar] = 1;
   //put it in the new buffer
}
else
{
  //duplicate
  //ignore it
}
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.