24.12.2017, 03:03
Mały algorytm
Nie wiem dlaczego nie działa kod:
sprawdzałem i no musi działać a zwraca fałsz(dla podanego przykładu)
zad:
Complete the solution so that it returns true if the first argument(string) passed in ends with the 2nd argument (also a string).
Examples:
solution("abc", "bc") # returns true
solution("abc", "d") # returns false
sprawdzałem i no musi działać a zwraca fałsz(dla podanego przykładu)
zad:
Complete the solution so that it returns true if the first argument(string) passed in ends with the 2nd argument (also a string).
Examples:
solution("abc", "bc") # returns true
solution("abc", "d") # returns false
Kod PHP:
#include <iostream>
#include <stdbool.h>
bool solution(const char* string, const char* ending)
{
while (*string++ && *ending++ != 0)
;
if (*--string == 0 && *--ending != 0) {
while (*ending++ != 0)
;
ending -= 2;
--string;
if (*string == *ending)
return true;
}
if (*ending == 0 && *string != 0) {
while (*string++ != 0)
;
string -= 2;
--ending;
if (*string == *ending)
return true;
}
if (*string == *ending)
return true;
return false;
}
int main()
{
std::cout << solution("abc", "bc");
return 0;
}