How to check if a string is present completely as a prefix in another string?

  • Store the minimum of the 2 lengths of the given 2 strings in a variable.
  • Make an index variable and initiate that with 0.
  • Start traversing both the string and comparing them at each index.
  • Stop the iteration when either one reaches the end or any character does’t match.
  • If either of the string’s end is reached then one string is present completely as a prefix in another string, else not.
    @Imsbajetha lets take the example of bat and batman for this. The loop starts from b and stops at t since it ends, we can say that bat is present as a complete prefix in batman.
    Let’s take another example of ball and batman. The same process repeats, but this time the loop ends right after it reaches a in ball and batman. Since none of the string’s end is reached, we can say that **ball is not present as a prefix in batman.
    https://ide.codingblocks.com/s/149311
    Hope this helps.
1 Like