Tuesday, July 14, 2009

Plz solve this programm in C++, if P & T are strings with lengths R & S repectively and are stored as arrays?

with one charecter per element , find the indedx(location) of P in T??

Plz solve this programm in C++, if P %26amp; T are strings with lengths R %26amp; S repectively and are stored as arrays?
loop through P %26amp; T until you find the character you're looking for, add 1 to "currentIndex" starting from zero each time through the loop, then return current index.
Reply:i dont think that there is any datatype say string however u can use char arrays oooh u mention arrays hope u r refering the same then








there are many cases to handle


for eg if R%26gt;S then u never find P in T however reverse is possible (hope u got my idea)





is P bigger than what T holds ??





and if all checks verify the possibility that P can be found in T then create an integer variable say index , set it to Zero and start searching char by char if the first char match incriment some pointer var say point=1 and increment till it matches the subsequent chars in T of P if it fails (means within point reaches the length of P it trace uncomman char then simply jump to index+point bcz u didnt find the match there and continue searching.....





if u reach the index %26gt; (T.length - P.length) (bcz after that u cant find P in T :-) means u dint find the string display sorry else if u finde in between make sure to toggle a flag variable so that after that loop u can trace that u find that string and u can also make a variable say counter =0 to check how many time u get that string.......





hope u get the best 4m me


in last plz rank my answer as best if u feel so


and comment plz :-) i m waiting





well as d below person use pointers and provide u d code i think it can be easily done by array indexing as i mention above and y he give d code it must let u first exercise yourself n then if cant implement then only code is given to you however, u r looking smart and i hope u can implement it by your own with my logics





good luck n great fun





http://www.geocities.com/ankur899/
Reply:Doing a homework assignment?





int (const char *p, int r, const char *t, int s)


{


int i;


for (i = 0; i %26lt;= (s-r); ++i)


{


if (strncmp(t+i, p, r) == 0) return (i);


}


return (-1); /* not found */


}





Of course, this is C, but C is a subset of C++, so this would work. I don't remember C++ has an exact replacement for C's strncpy() other than those that use the String class. But as you state your question, String probably isn't allowed.





To answer a comment by "d person above," please note that, in C, arrays and pointers are interchangeable in use. It's the declaration that's different. In particular, many people don't realize that *(a+i) is exactly the same as a[i] in C and C++. Exactly, as in by definition. That leads to the rather strange result that a[i] is exactly the same as i[a], even if 'i' is an int and 'a' is a pointer or array!





Anyway, certainly you can find harder ways to solve this problem by doing more of the work yourself. But this is the simplest way.


No comments:

Post a Comment