Hello ,
This is a program intends to tell the number of the each word from the sentence. For example If write
naber aga naber naber aga
output will be like this:
naber 3
aga 2
I have written a program , I have a little problem I take a output like this :
naber 3
aga 2
naber 3
naber 3
aga 2
I know why I take this problem I have a solution . This is the codes by the way :
algorithm is not really hard. I am sure you will figure it out easily.Code:#include <stdio.h> #include <string.h> #include <stdlib.h> void sonuc(char *,char *,int,int *); int main() { char *s=malloc(40*sizeof(char)); gets(s); char *b,c[40]; char *p[10]; char a[40]; strcpy(a,s); b=strtok(a," "); int y=0; int x; strcpy(p[0],b); printf("%s",p[0]); int i=1; int m=0; while(b!=NULL) { x=strlen(b); for(m=0;m<i;m++) { if(strcmp(p[m],b)==0) { b=strtok(NULL," "); continue; } } strcpy(c,s); sonuc(c,b,x,&y); printf("%s kelimesinden %d tane var\n",b,y); y=0; b=strtok(NULL," "); i++; } return 0; } void sonuc(char *d,char *str,int t,int *ptr) { if(strstr(d,str)==NULL) return; else { d=strstr(d,str); *ptr+=1; sonuc(&d[t],str,t,ptr); } }
I have a solution for the problem . I can save each word into a struct or pointer string then I can compare the current word with the former words I have passed.If there is a same I can stop the loop to the beginning.
I want to know if there is another solution or algorith for tis program which is far more simpler than mine.



LinkBack URL
About LinkBacks



