I just made a program which searches for subtrings from a char* the hard way.
does someone have an algoritm which is better than this?
here's my code...
This code is like noodles;Code:#include<iostream.h> #include<conio.h> #include<string.h> #include<stdio.h> enum Bool{false, true}; Bool search(char sub[], char sup[]){ int subl=strlen(sub), supl=strlen(sup), st=0; Bool flag = false; if(supl<subl)return false; // sub larger than superstring. else{ for(int i=0; i<subl; i++) for(int j=st; j<supl && i<subl; j++) if(sub[i]==sup[j]){ flag = true; i++; }else{ flag = false; i=-1; st++; break; } return flag; } } void main(){ clrscr(); char a[20], b[20]; do{ gets(a); gets(b); if( search(a, b) )cout<<"Substring !"; else cout<<"Not substring !"; cout<<"\n'q' to quit...\n\n\n"; }while( getch()!='q' ); getch(); }
Even I can't make out the head or tail of it now.
P.S. I dont want to use any standard 'String' class or anything.



LinkBack URL
About LinkBacks




.