I need to write function which will check if string s2 is reverse substring of string s1, and return 1 if the condition is true. Function should be made using pointer arithmetic.
For example:
char s1[] = "abcdef";
char s2[] = "edc";
Function would return 1 because string s1 contains reverse string s2.
#include <stdio.h>
int reverseSubstring(const char *s1, const char *s2) {
while (*s1 != '\0') {
const char *p = s1;
const char *q = s2;
while (*p++ == *q++)
if (*q == '\0')
return 1;
s1++;
}
return 0;
}
int main() {
char s1[] = "abcdef";
char s2[] = "edc";
printf("%d", reverseSubstring(s1, s2));
return 0;
}
This function does the opposite. It checks if the string is substring and in my case it returns 0. It should return 1. How to modify this to work?
- Note: it is not allowed to use functions from the string.h, stdlib.h libraries, nor the sprintf and sscanf functions from the stdio.h library. It is not allowed to create auxiliary strings or strings in function or globally.