Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdlib.h>
- #include<stdio.h>
- #include<string.h>
- char* findInnermost(char* stream);
- int main()
- {
- char* test = "hopefully (the thing returned (should (be (this))))(and not this)";
- char* result = findInnermost(test);
- printf("%s\n", result);
- free(result);
- return 0;
- }
- char* findInnermost(char* stream)
- {
- int depth = 0, maxDepth = 0;
- char *leftptr = stream, *rightptr = stream + strlen(stream), *currentptr;
- for (currentptr = leftptr; *currentptr != '\0'; currentptr++)
- {
- switch (*currentptr)
- {
- case '(':
- depth++;
- if (depth >= maxDepth)
- {
- maxDepth = depth;
- leftptr = currentptr;
- }
- break;
- case ')':
- if (depth == maxDepth) rightptr = currentptr;
- depth--;
- break;
- }
- }
- char* innermost = malloc(sizeof(char) * ((rightptr - leftptr) - 1));
- strncpy(innermost, leftptr + 1, rightptr - leftptr - 1);
- innermost[rightptr - leftptr] = '\0';
- return innermost;
- }
Advertisement
Add Comment
Please, Sign In to add comment