[try Beta version]
Not logged in

 
How to find large consecutive substring from a string.

Dec 5, 2011 at 1:13pm
Hi,
please tell me how i can find large consecutive substring from a string.
For Example:
Input is : ABCQWERSTUVWNOL
out put should be: RSTUVW
Dec 5, 2011 at 2:25pm
Try strstr().
http://www.cplusplus.com/reference/clibrary/cstring/strstr/

Unless you are supposed to write your own substring search. If so, to get you going in the right direction think about how you would find a string inside a string. I would begin by trying to find the first letter of the string I'm looking for inside of the string I'm searching through.
Dec 5, 2011 at 2:36pm
Just keep track of the current substring length (and start position) and if a character doesn't match, reset it to one.
Dec 5, 2011 at 2:41pm
@bradw, he's not looking for a known string, but for a part of alphabet.

@OP, There are several ways to write this. I'll give you the least efficient one:
1
2
3
4
5
const char* str = "ABCQWERSTUVWNOL";
for (int i = 0; str[i] != 0; i++) //for each char in str...
   for (int j = 1; str[i+j] - str[i] == j; j++) //the condition means 'while characters progress in a linear way'
      //if this line is executed, you've found several consecutive chars between indexes i and i+j
      //you'll want to have a "char* start" and "int length" outside the loops to remember the longest of them. 
Notice that some characters are processed several times. You really only need one loop (or one counter).
Topic archived. No new replies allowed.