C program to remove duplicates

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 1
Reputation: gandolf III is an unknown quantity at this point 
Solved Threads: 0
gandolf III gandolf III is offline Offline
Newbie Poster

C program to remove duplicates

 
0
  #1
Oct 30th, 2009
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.
  1. # include <stdio.h>
  2. # include "simpio.h"
  3. # include "genlib.h"
  4.  
  5. main()
  6. {
  7. 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;
  8.  
  9. word=getchar();
  10. a='a';
  11. b='b';
  12. c='c';
  13. d='d';
  14. e='e';
  15. f='f';
  16. g='g';
  17. h='h';
  18. i='i';
  19. j='j';
  20. k='k';
  21. l='l';
  22. m='m';
  23. n='n';
  24. o='o';
  25. p='p';
  26. q='q';
  27. r='r';
  28. s='s';
  29. t='t';
  30. u='u';
  31. v='v';
  32. w='w';
  33. x='x';
  34. y='y';
  35. z='z';
  36. while((word=getchar())!=EOF)
  37. {
  38. if(word==a)
  39. {
  40. printf("a");
  41. a=1234;
  42. }
  43. else
  44. if(word==b)
  45. {
  46. printf("b");
  47. b=1234;
  48. }
  49. else
  50. if(word==c)
  51. {
  52. printf("c");
  53. c=1234;
  54. }
  55. else
  56. if(word==d)
  57. {
  58. printf("d");
  59. d=1234;
  60. }
  61. else
  62. if(word==e)
  63. {
  64. printf("e");
  65. e=1234;
  66. }
  67. else
  68. if(word==f)
  69. {
  70. printf("f");
  71. f=1234;
  72. }
  73. else
  74. if(word==g)
  75. {
  76. printf("g");
  77. g=1234;
  78. }
  79. else
  80. if(word==h)
  81. {
  82. printf("h");
  83. h=1234;
  84. }
  85. else
  86. if(word==i)
  87. {
  88. printf("i");
  89. i=1234;
  90. }
  91. else
  92. if(word==j)
  93. {
  94. printf("j");
  95. j=1234;
  96. }
  97. else
  98. if(word==k)
  99. {
  100. printf("k");
  101. k=1234;
  102. }
  103. else
  104. if(word==l)
  105. {
  106. printf("l");
  107. l=1234;
  108. }
  109. else
  110. if(word==m)
  111. {
  112. printf("m");
  113. m=1234;
  114. }
  115. else
  116. if(word==n)
  117. {
  118. printf("n");
  119. n=1234;
  120. }
  121. else
  122. if(word==o)
  123. {
  124. printf("o");
  125. o=1234;
  126. }
  127. else
  128. if(word==p)
  129. {
  130. printf("p");
  131. p=1234;
  132. }
  133. else
  134. if(word==q)
  135. {
  136. printf("q");
  137. q=1234;
  138. }
  139. else
  140. if(word==r)
  141. {
  142. printf("r");
  143. r=1234;
  144. }
  145. else
  146. if(word==s)
  147. {
  148. printf("s");
  149. s=1234;
  150. }
  151. else
  152. if(word==t)
  153. {
  154. printf("t");
  155. t=1234;
  156. }
  157. else
  158. if(word==u)
  159. {
  160. printf("u");
  161. u=1234;
  162. }
  163. else
  164. if(word==v)
  165. {
  166. printf("v");
  167. v=1234;
  168. }
  169. else
  170. if(word==w)
  171. {
  172. printf("w");
  173. w=1234;
  174. }
  175. else
  176. if(word==x)
  177. {
  178. printf("x");
  179. x=1234;
  180. }
  181. else
  182. if(word==y)
  183. {
  184. printf("y");
  185. y=1234;
  186. }
  187. else
  188. if(word==z)
  189. {
  190. printf("z");
  191. z=1234;
  192. }
  193. }
  194. getchar();
  195. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 33
Reputation: SVR is an unknown quantity at this point 
Solved Threads: 4
SVR SVR is offline Offline
Light Poster
 
0
  #2
Oct 31st, 2009
Insert at line 123 ...

  1. else
  2. {
  3. printf("\nduplicate or unknown character\n");
  4. }

and see what you get.
Last edited by SVR; Oct 31st, 2009 at 1:20 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 358
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 56
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #3
Nov 2nd, 2009
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
  1. 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
  1. map<char, bool> charMap;
When u get a new char in the input string just make an entry as
  1. 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
  1. #define MAX_CHAR 100 //whatever the total no. of characters are available
  2. int charEncountered[MAX_CHAR];
  3. //initialize them to 0 (zero)
  4. //use it as
  5. if(charEncountered[myChar]==0)
  6. {
  7. charEncountered[myChar] = 1;
  8. //put it in the new buffer
  9. }
  10. else
  11. {
  12. //duplicate
  13. //ignore it
  14. }
Last edited by dkalita; Nov 2nd, 2009 at 5:46 am.
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the C Forum


Views: 329 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC