Thursday, July 30, 2009

How can we find a substring in a given string using C++?

If you have





string var = "find a substring of this string";





to find a substring, say "of this"





int position = var.find_first_of("a substring");





and position = 5

How can we find a substring in a given string using C++?
Hi I'm sree


B Tech 7th sem


try this


I 'd like to be ur friend if you like mail me


mainstr = larger string


substr= smaller string


len_1=length of large str


len_2=length of small str


modifications may be necessary [ !!!!]


--------------------------------------...


for (i=0;i%26lt;len_1-len_2;i++) //ensure mainstr large than sub


{


for(j=0;j%26lt;len_2;j++)


{


if(mainstr[j+i]!=substr[j])


break;


}


if(j==len_2)


{


printf("substring found at postion %d",i+1);


return 0;


}


}


if(i==len_1-len_2)


{


printf("substring not found");


return 0;


}

blazing star

No comments:

Post a Comment