Find the word from a given sentence having given word as prefix | C # program for the above approach ; Function to find the position of the string having word as prefix ; Initialize an List ; Extract words from the sentence ; Traverse each word ; Traverse the characters of word ; If prefix does not match ; Otherwise ; Return the word ; Return - 1 if not present ; Driver code
using System ; using System . Collections . Generic ; class GFG { static String isPrefixOfWord ( String sentence , String Word ) { String [ ] a = sentence . Split ( ' ▁ ' ) ; List < String > v = new List < String > ( ) ; foreach ( String e in a ) v . Add ( e ) ; for ( int i = 0 ; i < v . Count ; i ++ ) { for ( int j = 0 ; j < v [ i ] . Length ; j ++ ) { if ( v [ i ] [ j ] != Word [ j ] ) break ; else if ( j == Word . Length - 1 ) return v [ i ] ; } } return " - 1" ; } public static void Main ( String [ ] args ) { String s = " Welcome ▁ to ▁ Geeksforgeeks " ; String word = " Gee " ; Console . WriteLine ( isPrefixOfWord ( s , word ) ) ; } }