Thread: substrings

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    Question substrings

    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...
    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();
    }
    This code is like noodles;
    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.
    Last edited by arjunajay; 06-01-2005 at 05:08 AM. Reason: nedd to append.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help, extracting substrings from a string
    By doubty in forum C Programming
    Replies: 1
    Last Post: 06-17-2009, 11:57 PM
  2. Need help on substrings and streams
    By TaiL in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2008, 06:18 PM
  3. vector of substrings
    By manav in forum C++ Programming
    Replies: 47
    Last Post: 05-10-2008, 02:05 PM
  4. Searching for a series of possible substrings inside a string
    By andrew.bolster in forum C Programming
    Replies: 7
    Last Post: 02-10-2008, 02:20 AM
  5. Searching strings - substring's
    By Vber in forum C Programming
    Replies: 4
    Last Post: 02-06-2003, 12:05 PM