| | |
C program to remove duplicates
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 0
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.
C Syntax (Toggle Plain Text)
# 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(); }
•
•
Join Date: May 2008
Posts: 33
Reputation:
Solved Threads: 4
0
#2 Oct 31st, 2009
Insert at line 123 ...
and see what you get.
C Syntax (Toggle Plain Text)
else { printf("\nduplicate or unknown character\n"); }
and see what you get.
Last edited by SVR; Oct 31st, 2009 at 1:20 am.
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
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
When u get a new char in the input string just make an entry as
If u want to continue with C only u can still do it using an array like
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
C Syntax (Toggle Plain Text)
void removeDuplicate(char *str);
Implement the above method. If u wanted to do it in C++ u could have used a map of flags such as
C Syntax (Toggle Plain Text)
map<char, bool> charMap;
C Syntax (Toggle Plain Text)
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
C Syntax (Toggle Plain Text)
#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 }
Last edited by dkalita; Nov 2nd, 2009 at 5:46 am.
![]() |
Other Threads in the C Forum
- Previous Thread: debugger help
- Next Thread: c language programe HELP ME PLEASE
| Thread Tools | Search this Thread |
#include * append array arrays asterisks bash binarysearch calculate changingto char character cm copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators input intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix meter microsoft mqqueue number oddnumber odf opensource openwebfoundation overwrite owf pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprogramming standard strchr string systemcall testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi





