check if a string is subsequence of other string (using for loop ) in c++

 what is a subseqence ?

A subsequence is a sequence that can be derived from another sequence by removing zero or more elements, without changing the order of the remaining elements.

Program to check if a string is subsequence of another string :

bool subsequence(string str1 , string str2 , int m , int n)

{    

      int j = 0;

    for(int i = 0 ; i < m  ; i++)

    {  

        int flag =0 ;

        while(flag != 1 )

        {    if(j > n)

             {

                 return false ;

             }

            if(str1[i] == str2[j])

            {

                flag =1;

            }

            j++;

        }

        

    }

    return true ;

}


Here , m is the length of    string1 and n is the length of string 2.

Comments